Android bitmap压缩方法


前言

Bitmap所占用的内存 = 图片长度 x 图片宽度 x 一个像素点占用的字节数。
3个参数,任意减少一个的值,就达到了压缩的效果。


1、质量压缩

1.1、quality压缩

bitmap.compress(Bitmap.CompressFormat.JPEG, quality, byteArrayOutputStream );
主要是通过设置quality来降低质量,0-100范围。
PS:PNG格式会忽略这个参数,所以Bitmap.CompressFormat不可以选择PNG格式

 /**
  * 压缩图片
  * 
  * @param bitmap
  *          被压缩的图片
  * @param sizeLimit
  *          大小限制
  * @return
  *          压缩后的图片
  */
 private Bitmap compressBitmap(Bitmap bitmap, long sizeLimit) {
    
    
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     int quality = 100;
     bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);

     // 循环判断压缩后图片是否超过限制大小
     while(baos.toByteArray().length / 1024 > sizeLimit) {
    
    
         // 清空baos
         baos.reset();
         bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
         quality -= 10;
     }

     Bitmap newBitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(baos.toByteArray()), null, null);

     return newBitmap;
 }
1.2、RGB_565压缩

将色彩模式换成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个字节

   BitmapFactory.Options options2 = new BitmapFactory.Options();
   options2.inPreferredConfig = Bitmap.Config.RGB_565;
   bm = BitmapFactory.decodeFile(filePath, options2);

2、宽高压缩

2.1、采样率压缩

设置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 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.2、Matrix 缩放法

bitmap的长度和宽度分别缩小了一半,图片大小缩小至四分之一。

Matrix matrix = new Matrix();
matrix.setScale(0.5f, 0.5f);
bm = Bitmap.createBitmap(bit, 0, 0, bit.getWidth(),
        bit.getHeight(), matrix, true);
2.3、bitmap.createScaledBitmap方法

内部实现其实是借助了matrix,算出给定的宽高压缩比,再用matrix.serScale压缩

  bitmap.createScaledBitmap
    bm = Bitmap.createScaledBitmap(bit, 150, 150, true);

猜你喜欢

转载自blog.csdn.net/fenglolo/article/details/131559985