美文网首页
Android bitmap压缩方法

Android bitmap压缩方法

作者: 粥小新 | 来源:发表于2018-12-14 16:56 被阅读0次

明人不说暗话,开门见山地说
Bitmap所占用的内存 = 图片长度 x 图片宽度 x 一个像素点占用的字节数。
3个参数,任意减少一个的值,就达到了压缩的效果。
所以压缩又可以分为两种方法,质量压缩和宽高压缩

1.质量压缩
  1. 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;
     }
    
  2. 将色彩模式换成RGB_565也会比默认的ARGB8888降低一半质量

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

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;
  }
  1. Matrix 缩放法

    Matrix matrix = new Matrix();
    matrix.setScale(0.5f, 0.5f);
    bm = Bitmap.createBitmap(bit, 0, 0, bit.getWidth(),
            bit.getHeight(), matrix, true);
    

PS:这种方法可以达到只缩放宽或高的效果

3)bitmap.createScaledBitmap方法

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

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

相关文章

  • 2019-11-05

    Bitmap Bitmap 细说Bitmap bitmap的六种压缩方式,Android图片压缩 1.先讲讲屏幕密...

  • 收集_Android源码文章

    一、Bitmap: Android bitmap压缩优化方案Android性能优化系列之Bitmap图片优化 二、...

  • Android bitmap压缩方法

    明人不说暗话,开门见山地说Bitmap所占用的内存 = 图片长度 x 图片宽度 x 一个像素点占用的字节数。3个参...

  • android图片压缩

    先生成一个长宽被缩放的bitmap 再压缩到100k以下 关于Bitmap的compress方法,android ...

  • Android 将bitmap 将图片压缩到指定的大小

    方法调用案例 压缩bitmap方法的封装

  • Bitmap图片压缩

    Android中图片是以bitmap形式存在的,这篇文章主要介绍了Android实现图片压缩(bitmap的六种压...

  • Android

    Android常用图片压缩方式 质量压缩 尺寸压缩 1. 质量压缩 质量压缩通过相应算法进行优化Bitmap的位深...

  • Android NDK编译libjpeg-turbo压缩图片

    Android开发中,我们经常要面对图片压缩,大部分人使用Android Bitmap进行压缩,还有一些使用lib...

  • 关于android 图片压缩你需要知道的事

    关键词: Bitmap,质量压缩,比例压缩,采样率压缩,微信分享 前言 android 系统的图片压缩大体上有三种...

  • Android图片压缩

    方法一:质量压缩 这种压缩方法不压缩bitmap的内存,而是在上传下载时的图片的大小变小,除了这种压缩方法,其他的...

网友评论

      本文标题:Android bitmap压缩方法

      本文链接:https://www.haomeiwen.com/subject/clighqtx.html