方法一 compress
// 资源文件转成bitmap
Bitmap bitmapTest = BitmapFactory.decodeResource(getResources(), R.mipmap.test);
ByteArrayOutputStream output = new ByteArrayOutputStream();
//100%压缩
bitmapTest.compress(Bitmap.CompressFormat.JPEG, 100, output);
//图片字节程度长度
int lengthBefore = output.toByteArray().length;
L.showError("lengthBefore====" + lengthBefore);
ivBefore.setImageBitmap(bitmapTest);
//将图片保存到本地sd卡
String pathBefore = fileUtils.saveImage(bitmapTest);
//获取sd卡图片获取图片字节大小
long fileLengthBefore = fileUtils.getImagesAbsolutePathFile(pathBefore).length();
L.showError("fileLengthBefore====" + fileLengthBefore);
//重置大小
output.reset();
//1%压缩
bitmapTest.compress(Bitmap.CompressFormat.JPEG, 1, output);
//1%压缩之后图片的大小
int lengthAfter = output.toByteArray().length;
L.showError("lengthAfter====" + lengthAfter);
// 解码生成图片
Bitmap bitmapAfter = BitmapFactory.decodeByteArray(output.toByteArray(), 0, output.toByteArray().length);
ivAfter.setImageBitmap(bitmapAfter);
String pathAfter = fileUtils.saveImage(bitmapAfter);
// 重置
output.reset();
// 解码生成图片的大小
bitmapAfter.compress(Bitmap.CompressFormat.JPEG, 100, output);
L.showError("output.toByteArray().length====" + output.toByteArray().length);
//获取sd卡图片获取图片字节大小
long fileLengthAfter = fileUtils.getImagesAbsolutePathFile(pathAfter).length();
L.showError("fileLengthAfter====" + fileLengthAfter);
打印出来的日志如下。
2019-12-02 15:17:02.089 861-861/com.cc.studyall E/studydemo: lengthBefore====5219019
2019-12-02 15:17:02.474 861-861/com.cc.studyall E/studydemo: fileLengthBefore====5219019
2019-12-02 15:17:02.673 861-861/com.cc.studyall E/studydemo: lengthAfter====115186
2019-12-02 15:17:03.242 861-861/com.cc.studyall E/studydemo: output.toByteArray().length====302535
2019-12-02 15:17:03.243 861-861/com.cc.studyall E/studydemo: fileLengthAfter====302535
画质也出现很明显的差别。
缩放图片大小
// 资源文件转成bitmap
Bitmap bitmapTest = BitmapFactory.decodeResource(getResources(), R.mipmap.test);
mIvBefore.setImageBitmap(bitmapTest);
//将图片保存到本地sd卡
String filePath = mFileUtils.saveImage(bitmapTest);
// 根据路径获得突破并压缩返回bitmap用于显示
try {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, ConvertUtils.dip2px(this,120), ConvertUtils.dip2px(this,120));
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
L.showError("bitmap.getWidth()====" + bitmap.getWidth() + "bitmap.getHeight()====" + bitmap.getHeight());
mIvAfter.setImageBitmap(bitmap);
ByteArrayOutputStream output = new ByteArrayOutputStream();
//100%压缩
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
//图片字节程度长度
int lengthDecode = output.toByteArray().length;
L.showError("lengthDecode====" + lengthDecode);
File file = mFileUtils.getImagesTempFile(System.currentTimeMillis() + ".jpg");
OutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
} catch (Exception e) {
L.showError(e.toString());
}
// 计算图片的缩放值int reqWidth, int reqHeight这两个参数表示图片希望压缩到多大
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;//原图片的高度
final int width = options.outWidth;//原图片的宽度
int inSampleSize = 1;
L.showError("options.outWidth====" + options.outWidth + "options.outHeight====" + options.outHeight);
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);//四舍五入, 原图高度除以希望压缩到的高度
final int widthRatio = Math.round((float) width / (float) reqWidth);//四舍五入, 原图宽度除以以往压缩到的宽度
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;//选择较小的那个比例, 保证宽和高压缩后的大小都不会小于期望的大小
}
return inSampleSize;
}
打印日志如下:
2019-12-02 16:05:40.353 11095-11095/com.cc.studyall E/studydemo: options.outWidth====4896options.outHeight====3672
2019-12-02 16:05:40.479 11095-11095/com.cc.studyall E/studydemo: bitmap.getWidth()====489bitmap.getHeight()====367
2019-12-02 16:05:40.485 11095-11095/com.cc.studyall E/studydemo: lengthDecode====105259