android 图片压缩

来源:互联网 发布:淘宝客服任职要求 编辑:程序博客网 时间:2024/06/07 08:29

Android中最占内存的肯定是图片资源了,使用大图很容易造成OOM,就算是小图,使用不当也会造成很大的资源浪费。有一点需要明确一下,图片最终占内存跟图片大小本身没什么关系,比如说有一张1k的图片,还有一张1M的图片,两张图片加载到内存中设置的长宽一样,图片质量也一样,那么他们他们占的内存是一样大的。

如何计算图片占内存大小呢?

图片加载到内存中的长*图片加载到内存中的宽*一个像素点所占内存 就是图片占内存的大小。

一个像素点占多少内存呢?

跟Bitmap的Config有关

Config取值

ALPHA_8 一个像素占一个字节,显示黑白图
RGB_565 一个像素2个字节,一般图片用这种就可以
ARGB_8888一个像素4个字节,图片最清晰

下面介绍三种图片压缩方法

第一:质量压缩方法

 private Bitmap compressImage(Bitmap image) {              ByteArrayOutputStream baos = new ByteArrayOutputStream();   //质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中           image.compress(Bitmap.CompressFormat.JPEG, 100, baos);         int options = 100;   //循环判断如果压缩后图片是否大于100kb,大于继续压缩             while ( baos.toByteArray().length / 1024>100) {                  baos.reset();//重置baos即清空baos      //这里压缩options%,把压缩后的数据存放到baos中              image.compress(Bitmap.CompressFormat.JPEG, options, baos);    //每次都减少10              options -= 10;         }  //把压缩后的数据baos存放到ByteArrayInputStream中           ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把ByteArrayInputStream数据生成图片           Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);         return bitmap;      }  
第二:图片按比例大小压缩方法(根据路径获取图片并压缩)

 private Bitmap getimage(String srcPath) {          BitmapFactory.Options newOpts = new BitmapFactory.Options();          //开始读入图片,此时把options.inJustDecodeBounds 设回true了          newOpts.inJustDecodeBounds = true;  //此时返回bm为空         Bitmap bitmap = BitmapFactory.decodeFile(srcPath,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;//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了          bitmap = BitmapFactory.decodeFile(srcPath, newOpts);          return compressImage(bitmap);//压缩好比例大小后再进行质量压缩      }  

 
第三:图片按比例大小压缩方法(根据Bitmap图片压缩)


 private Bitmap comp(Bitmap image) {              ByteArrayOutputStream baos = new ByteArrayOutputStream();              image.compress(Bitmap.CompressFormat.JPEG, 100, baos);       //判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出     if( baos.toByteArray().length / 1024>1024) {             baos.reset();//重置baos即清空baos          image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//这里压缩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;//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);//压缩好比例大小后再进行质量压缩  }

android常用图片框架有imageloader,glide,,picasso ,fresco。对图片压缩都足了很好的处理。

imageloader是比较老牌的图片处理库,glide是google推广的,对listview中加载图片做了非常好的优化,是比较好的图片框架中最早支持gif图的,picasso 是 Square 公司的,Square 的开源库都很有名,fresco是facebook的,图片缓存处理做的应该是最优的。以后对图片库源码进行分析。imageloader是最经典的,相比较而言代码结构也是比较简单的,以后会先介绍它。


欢迎扫描二维码,关注公众账号

0 2