Bitmap 压缩笔记

  • 质量压缩
  • 采样率压缩
  • 缩放压缩
  • 存储在文件中

质量压缩

顾名思义,保持质量的同时减少文件的大小。

ByteArrayOutputStream baos = new ByteArrayOutputStream();  
//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到 baos 中
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);

int options = 100;  //压缩质量为 100,保留 100% 的大小
while ( baos.toByteArray().length / 1024 > maxSize) {  
    //如果内存的大小超过了 maxSize (kb) 就将图片的压缩质量调低
    baos.reset();//清空 baos
    image.compress(Bitmap.CompressFormat.JPEG, options, baos);
    options -= 10;
}  

//把压缩后的数据 baos 存放到 ByteArrayInputStream 中
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  

//把 ByteArrayInputStream 数据生成图片
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);

采样率压缩

有效的缩小图片的大小,但会影响质量

public static final int IMAGE_MAX_HEIGH = 854;
public static final int IMAGE_MAX_WIDTH = 480;

public static Bitmap getBitmapByBytes(byte[] bytes){

    //对于图片的二次采样,主要得到图片的宽与高
    int width = 0;
    int height = 0;
    int sampleSize = 1;//默认缩放为1

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//仅仅解码边缘区域

    //如果指定了inJustDecodeBounds,decodeByteArray将返回为空
    BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);

    //得到宽与高
    height = options.outHeight;
    width = options.outWidth;

    //图片实际的宽与高,根据默认最大大小值,得到图片实际的缩放比例
    while ((height/sampleSize>Cache.IMAGE_MAX_HEIGHT)
         ||(width/sampleSize>Cache.IMAGE_MAX_WIDTH)) {

        sampleSize *= 2;
    }

    //不再只加载图片实际边缘
    options.inJustDecodeBounds = false;

    //并且制定缩放比例
    options.inSampleSize = sampleSize;

    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
}

按比例大小压缩

private Bitmap comp(Bitmap image) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    if( baos.toByteArray().length / 1024 > 1024) {
        //判断如果图片大于1M,进行压缩避免在生成图片
        //(BitmapFactory.decodeStream)时溢出
        baos.reset();//重置baos即清空baos
        //这里压缩50%,把压缩后的数据存放到baos中
        image.compress(Bitmap.CompressFormat.JPEG, 50, baos);
    }

    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    BitmapFactory.Options newOpts = new BitmapFactory.Options();

    //开始读入图片,此时把options.inJustDecodeBounds 设回true了
    newOpts.inJustDecodeBounds = true;

    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);

    newOpts.inJustDecodeBounds = false;

    int w = newOpts.outWidth;
    int h = newOpts.outHeight;
    //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
    float hh = 800f;//这里设置高度为800f
    float ww = 480f;//这里设置宽度为480f

    //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
    int 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了 
    isBm = new ByteArrayInputStream(baos.toByteArray());
    bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);

    return compressImage(bitmap);//压缩好比例大小后再进行质量压缩

}

存储在文件中

String fileName=path.substring(path.lastIndexOf("/")+1);
String filePath=context.getExternalFilesDir(null)+"/"+fileName;

FileOutputStream fos = new FileOutputStream(filePath);

//将压缩后的数据存到了 fos 指向的地址中
bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);

fos.close();

你可能感兴趣的:(Bitmap 压缩笔记)