参考:https://juejin.cn/post/6952429810207424526#heading-7
Bitmap所占用的内存 = 图片长度 x 图片宽度 x 一个像素点占用的字节数。
3个参数,任意减少一个的值,就达到了压缩的效果。
所以压缩又可以分为两种方法,质量压缩和宽高压缩
1.质量压缩(对内存没有影响)
-
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, byteArrayOutputStream );
主要是通过设置quality来降低质量,0-100范围。经过质量压缩的图片大小不会改变因为质量压缩不会减少图片的像素 ,它是在保持像素的前提下改变图片的位深及透明度等属性来达到压缩图片的目的,所以说这个方法叫质量压缩,既然图片的长宽和像素都不变,那么bitmap占用的内存大小也是不会改变的。 -
如果图片是png模式那么长度就不会变化,因为png是无损的,不能进行压缩。将色彩模式换成RGB_565也会比默认的ARGB8888降低一半质量
ALPHA_8 :表示8位Alpha位图,即A=8,一个像素点占用1个字节,它没有颜色,只有透明度ARGB_4444 :表示16位ARGB位图,即A=4,R=4,G=4,B=4,一个像素点占 4+4+4+4=16位,2个字节
ARGB_8888 :表示32位ARGB位图,即A=8,R=8,G=8,B=8,一个像素点占8+8+8+8=32位,4个字节
RGB_565 :表示16位RGB位图,即R=5,G=6,B=5,它没有透明度,一个像素点占5+6+5=16位,2个字节
A(alpha)透明 ,R(red)红色 ,G(green)绿色,B(blue)蓝色
BitmapFactory.Options options2 = new BitmapFactory.Options();
options2.inPreferredConfig = Bitmap.Config.RGB_565;
bm = BitmapFactory.decodeFile(filePath, options2);
2.尺寸压缩
1)利用采样率压缩
当图片宽高大于设定宽高时进行缩放。减小bitmap内存占用大小,设置inSampleSize的值(int类型)后,假如设为2,则宽和高都为原来的1/2,宽高都减少了,大小则减少了1/4。
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
// 先将inJustDecodeBounds设置为true不会申请内存去创建Bitmap,返回的是一个空的Bitmap,但是可以获取
//图片的一些属性,例如图片宽高,图片类型等等。
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// 计算inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// 加载压缩版图片
options.inJustDecodeBounds = false;
// 根据具体情况选择具体的解码方法
return BitmapFactory.decodeResource(res, resId, options);
}
public static Bitmap decodeSampledBitmapFromBitmap(Bitmap bitmap,
int reqWidth, int reqHeight) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(data, 0, data.length, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeByteArray(data, 0, data.length, options);
}
public 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;
// 计算inSampleSize值
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
2) Matrix 缩放法(放大或缩小图片尺寸,增大或减少内存占用)
public static Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) {
// 获取这个图片的宽和高
int width = bgimage.getWidth();
int height = bgimage.getHeight();
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 缩放图片动作
float scale = Math.min(scaleWidth, scaleHeight);
matrix.postScale(scale, scale);
Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,
matrix, true);
return bitmap;
}
PS:使用的是通过矩阵对图片进行裁剪,也是通过缩放图片尺寸,来达到压缩图片的效果,和采样率的原理一样。
3)bitmap.createScaledBitmap方法
bitmap.createScaledBitmap
bm = Bitmap.createScaledBitmap(bit, 150, 150, true);
内部实现其实是借助了matrix,算出给定的宽高压缩比,再用matrix.serScale压缩