android 开发图片压缩的整理一基础篇
2017-09-04 16:56
246 查看
参考 : http://blog.csdn.net/u012400885/article/details/72235451
感谢 HLQ_Struggle的总结;
整理和学习一下,有时间整理一下native层对图片压缩的实现
图片存在3种状态 ,bitmap stream file ;传送的时候为stream;磁盘为文件;内存为bitmap 或者 stream
一般的压缩有三种
1,质量压缩 (将file压缩,但是内存不变),不建议使用递减的方式实现 ,就是说当递减为0的时候,如果质量还是不达标的情况下会存在问题
/**
* 1. 质量压缩
* 设置bitmap options属性,降低图片的质量,像素不会减少
* 设置options 属性0-100,来实现压缩
*
* @param bmp 需要压缩的bitmap图片对象
* @param file 压缩后图片保存的位置
*/
public static void compressImageToFile(Bitmap bmp, File file) {
// 0-100 100为不压缩
int options = 20;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 把压缩后的数据存放到baos中
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 压缩后返回bitmap
*
* @param bmp 需要压缩的bitmap图片对象
* @return
*/
public static Bitmap compressImageToBitmap(Bitmap bmp) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, os);
if (os.toByteArray().length / 1024 > 1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
os.reset();//重置baos即清空baos
bmp.compress(Bitmap.CompressFormat.JPEG, 50, os);//这里压缩50%,把压缩后的数据存放到baos中
}
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFacto
4000
ry.decodeStream(is, null, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = 240f;// 设置高度为240f时,可以明显看到图片缩小了
float ww = 120f;// 设置宽度为120f,可以明显看到图片缩小了
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0) be = 1;
newOpts.inSampleSize = be;//设置缩放比例
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
is = new ByteArrayInputStream(os.toByteArray());
bitmap = BitmapFactory.decodeStream(is, null, newOpts);
//压缩好比例大小后再进行质量压缩
// return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除
return bitmap;
}
2,尺寸压缩 (直接修改了分辨率,内存也会跟着变化)
/**
* 计算缩放比
*/
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
/**
* 根据路径获得图片信息并按比例压缩,返回bitmap
*/
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;// 只解析图片边沿,获取宽高
BitmapFactory.decodeFile(filePath, options);
// 计算缩放比
options.inSampleSize = calculateInSampleSize(options, 160, 240); // 这里160 240 随便输 不过要靠谱哦~
// 完整解析图片返回bitmap
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
3,采样率的压缩 (不怎么熟,会失真)
/**
* 设置图片的采样率,降低图片像素
*
* @param filePath
* @param file
*/
public static void compressBitmap(String filePath, File file) {
// 数值越高,图片像素越低
int inSampleSize = 8;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
// options.inJustDecodeBounds = true;//为true的时候不会真正加载图片,而是得到图片的宽高信息。
//采样率
options.inSampleSize = inSampleSize;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 把压缩后的数据存放到baos中
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
try {
if (file.exists()) {
file.delete();
} else {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置图片的采样率,降低图片像素
*
* @param filePath
*/
public static Bitmap compressBitmap(String filePath) {
// 数值越高,图片像素越低
int inSampleSize = 8;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
// options.inJustDecodeBounds = true;//为true的时候不会真正加载图片,而是得到图片的宽高信息。
//采样率
options.inSampleSize = inSampleSize;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 把压缩后的数据存放到baos中
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
return BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片;
}
还有一个现在最火的鲁班(这里也备份一下)
第一步:加入依赖
compile ‘top.zibin:Luban:1.1.2’
第二步:新建布局文件 包含对应Activity
一个选择图片按钮,俩个ImageView用于显示图片,俩个TextView用于显示图片大小。
第三步:开启Luban压缩(省略代码可直接在GitHub上查看)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_PHOTO_CODE && resultCode == RESULT_OK) {
if (data == null) {
Toast.makeText(this, "打开图片失败~", Toast.LENGTH_SHORT).show();
return;
}
try {
// 获取图片路径
oldFile = FileUtil.getTempFile(this, data.getData());
// 设置获取图片显示ImageView
((ImageView) (findViewById(R.id.id_luban_choose_pic_show))).setImageBitmap(BitmapFactory.decodeFile(oldFile.getAbsolutePath()));
// 设置获取图片大小
((TextView) (findViewById(R.id.id_luban_choose_pic_size))).setText(String.format("Size : %s", StringUtils.getReadableFileSize(oldFile.length())));
// 启动Luban进行图片压缩
Luban.with(this) // 初始化
.load(oldFile) // 要压缩的图片
.setCompressListener(new OnCompressListener() {
@Override
public void onStart() {
// 压缩开始前调用 可以在方法内启动loading UI
Toast.makeText(LubanActivity.this, "我要开着Luban浪荡了~", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(File newFile) {
// 压缩成功后调用,返回压缩后的图片文件
Toast.makeText(LubanActivity.this, "开车到达目的地~", Toast.LENGTH_SHORT).show();
// 设置获取图片显示ImageView
((ImageView) (findViewById(R.id.id_luban_compress_end_show))).setImageBitmap(BitmapFactory.decodeFile(newFile.getAbsolutePath()));
// 设置获取图片大小
((TextView) (findViewById(R.id.id_luban_compress_end_size))).setText(String.format("Size : %s", StringUtils.getReadableFileSize(newFile.length())));
}
@Override
public void onError(Throwable e) {
// 压缩过程中出现异常
Toast.makeText(LubanActivity.this, "丫的,翻车了" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}).launch(); // 启动压缩
} catch (IOException e) {
Toast.makeText(this, "读取图片失败~", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
感谢 HLQ_Struggle的总结;
整理和学习一下,有时间整理一下native层对图片压缩的实现
图片存在3种状态 ,bitmap stream file ;传送的时候为stream;磁盘为文件;内存为bitmap 或者 stream
一般的压缩有三种
1,质量压缩 (将file压缩,但是内存不变),不建议使用递减的方式实现 ,就是说当递减为0的时候,如果质量还是不达标的情况下会存在问题
/**
* 1. 质量压缩
* 设置bitmap options属性,降低图片的质量,像素不会减少
* 设置options 属性0-100,来实现压缩
*
* @param bmp 需要压缩的bitmap图片对象
* @param file 压缩后图片保存的位置
*/
public static void compressImageToFile(Bitmap bmp, File file) {
// 0-100 100为不压缩
int options = 20;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 把压缩后的数据存放到baos中
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 压缩后返回bitmap
*
* @param bmp 需要压缩的bitmap图片对象
* @return
*/
public static Bitmap compressImageToBitmap(Bitmap bmp) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, os);
if (os.toByteArray().length / 1024 > 1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出
os.reset();//重置baos即清空baos
bmp.compress(Bitmap.CompressFormat.JPEG, 50, os);//这里压缩50%,把压缩后的数据存放到baos中
}
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
BitmapFactory.Options newOpts = new BitmapFactory.Options();
//开始读入图片,此时把options.inJustDecodeBounds 设回true了
newOpts.inJustDecodeBounds = true;
newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFacto
4000
ry.decodeStream(is, null, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = 240f;// 设置高度为240f时,可以明显看到图片缩小了
float ww = 120f;// 设置宽度为120f,可以明显看到图片缩小了
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0) be = 1;
newOpts.inSampleSize = be;//设置缩放比例
//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
is = new ByteArrayInputStream(os.toByteArray());
bitmap = BitmapFactory.decodeStream(is, null, newOpts);
//压缩好比例大小后再进行质量压缩
// return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除
return bitmap;
}
2,尺寸压缩 (直接修改了分辨率,内存也会跟着变化)
/**
* 计算缩放比
*/
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
/**
* 根据路径获得图片信息并按比例压缩,返回bitmap
*/
public static Bitmap getSmallBitmap(String filePath) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;// 只解析图片边沿,获取宽高
BitmapFactory.decodeFile(filePath, options);
// 计算缩放比
options.inSampleSize = calculateInSampleSize(options, 160, 240); // 这里160 240 随便输 不过要靠谱哦~
// 完整解析图片返回bitmap
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
3,采样率的压缩 (不怎么熟,会失真)
/**
* 设置图片的采样率,降低图片像素
*
* @param filePath
* @param file
*/
public static void compressBitmap(String filePath, File file) {
// 数值越高,图片像素越低
int inSampleSize = 8;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
// options.inJustDecodeBounds = true;//为true的时候不会真正加载图片,而是得到图片的宽高信息。
//采样率
options.inSampleSize = inSampleSize;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 把压缩后的数据存放到baos中
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
try {
if (file.exists()) {
file.delete();
} else {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置图片的采样率,降低图片像素
*
* @param filePath
*/
public static Bitmap compressBitmap(String filePath) {
// 数值越高,图片像素越低
int inSampleSize = 8;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
// options.inJustDecodeBounds = true;//为true的时候不会真正加载图片,而是得到图片的宽高信息。
//采样率
options.inSampleSize = inSampleSize;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 把压缩后的数据存放到baos中
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
return BitmapFactory.decodeStream(isBm, null, null);// 把ByteArrayInputStream数据生成图片;
}
还有一个现在最火的鲁班(这里也备份一下)
第一步:加入依赖
compile ‘top.zibin:Luban:1.1.2’
第二步:新建布局文件 包含对应Activity
一个选择图片按钮,俩个ImageView用于显示图片,俩个TextView用于显示图片大小。
第三步:开启Luban压缩(省略代码可直接在GitHub上查看)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_PHOTO_CODE && resultCode == RESULT_OK) {
if (data == null) {
Toast.makeText(this, "打开图片失败~", Toast.LENGTH_SHORT).show();
return;
}
try {
// 获取图片路径
oldFile = FileUtil.getTempFile(this, data.getData());
// 设置获取图片显示ImageView
((ImageView) (findViewById(R.id.id_luban_choose_pic_show))).setImageBitmap(BitmapFactory.decodeFile(oldFile.getAbsolutePath()));
// 设置获取图片大小
((TextView) (findViewById(R.id.id_luban_choose_pic_size))).setText(String.format("Size : %s", StringUtils.getReadableFileSize(oldFile.length())));
// 启动Luban进行图片压缩
Luban.with(this) // 初始化
.load(oldFile) // 要压缩的图片
.setCompressListener(new OnCompressListener() {
@Override
public void onStart() {
// 压缩开始前调用 可以在方法内启动loading UI
Toast.makeText(LubanActivity.this, "我要开着Luban浪荡了~", Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(File newFile) {
// 压缩成功后调用,返回压缩后的图片文件
Toast.makeText(LubanActivity.this, "开车到达目的地~", Toast.LENGTH_SHORT).show();
// 设置获取图片显示ImageView
((ImageView) (findViewById(R.id.id_luban_compress_end_show))).setImageBitmap(BitmapFactory.decodeFile(newFile.getAbsolutePath()));
// 设置获取图片大小
((TextView) (findViewById(R.id.id_luban_compress_end_size))).setText(String.format("Size : %s", StringUtils.getReadableFileSize(newFile.length())));
}
@Override
public void onError(Throwable e) {
// 压缩过程中出现异常
Toast.makeText(LubanActivity.this, "丫的,翻车了" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}).launch(); // 启动压缩
} catch (IOException e) {
Toast.makeText(this, "读取图片失败~", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
相关文章推荐
- Android开发基础 调用相机 系统相册(并对图片进行压缩处理)
- Android图片开发内幕--基础篇
- Android图片压缩上传之基础篇
- 基础 Android 开发规范整理
- Android图片压缩上传系列-基础篇
- Android开发之基础----------多媒体图片加载、拍照和摄像(一)
- Android应用开发之图片(Bitmap)压缩(三)---------采样率压缩
- Android开发基础知识整理之数据存储
- Android应用开发中三种常见的图片压缩方法
- Android应用开发中三种常见的图片压缩方法
- Android 选择图片拍照裁剪压缩整理
- Android开发技术:Android对图片的压缩读取和保存
- Android开发基础知识整理之多线程与网络技术
- [Android开发]-图片压缩(根据屏幕大小进行比例压缩)
- Android应用开发之图片(Bitmap)压缩(一)---------Bitmap与BitmapFactory的认识及图片的存储形式大小的实验
- Android应用开发基础篇(15)-----URL(获取指定网址里的图片)
- Android应用开发中三种常见的图片压缩方法
- Android开发之图片处理专题(二):利用AsyncTask和回调接口实现图片的异步加载和压缩
- Android应用开发之图片(Bitmap)压缩(三)---------采样率压缩
- android开发之就本地和网络图片的压缩以及缓存详解