Android中处理大图片时图片压缩

来源:互联网 发布:软件测试柠檬班靠谱吗 编辑:程序博客网 时间:2024/05/21 05:38

1、BitmapFactory.Options中的属性

在进行图片压缩时,是通过设置BitmapFactory.Options的一些值来改变图片的属性的,下面我们来看看BitmapFactory.Options中常用的属性意思:

  • options.inPreferredConfig - 设置Bitmap的偏好配置,值有Bitmap.Config.ARGB_8888,Bitmap.Config.ARGB_4444,Bitmap.Config.ARGB_8888,Bitmap.Config.RGB_565。默认为ARGB_8888,顾名思义,这是设置Bitmap的显示质量的。
  • options.outHeight - 得到该Bitmap的高。
  • options.outWidth - 得到该Bitmap的宽。
  • options.outMimeType - 得到该Bitmap的MIME值。
  • options.inSampleSize - 压缩比例,如果options.inSampleSize = 4;那么压缩后的图片的宽和高都为原来图片的1/4,压缩后的图片的像素为原来图片的1/16。
  • options.inJustDecodeBounds  - 官方文档上是这样介绍的:
    • If set to true, the decoder will return null (no bitmap), but
      the out... fields will still be set, allowing the caller to query
      the bitmap without having to allocate the memory for its pixels.
    • 意思就是:如果设置为true,那么使用BitmapFactory.decodeStream(InputStream is, Rect outPadding, 
      Options opts)或BitmapFactory.decodeXXX(....,Options opts)方法并不会真的返回一个Bitmap对象,而是返回null,虽然返回null,但是我们却可以通过options来获得该Bitmap的一些值,如它的宽、高、MimeType等值。这样就不必为Bitmap对象分配内存空间也可以获得它的Width和Height,从而节约内存。所以这个属性对我们压缩图片前进行检查大有帮助。常用的技巧就是为了避免图片过大而导致OOM,所以在我们加载图片之前就通过设置它为true,获取到图片的宽、高等值,然后进行一些判断检查等,决定图片是否压缩。我们来看看加载一张405x579图片的例子:
    • [java] view plain copy
       print?
      1. final BitmapFactory.Options options = new BitmapFactory.Options();  
      2. options.inJustDecodeBounds = true;  
      3. Bitmap bitmap = BitmapFactory.decodeResource(getResource(), R.mipmap.two, options);  
      4. Log.v("zxy","bitmap="+bitmap);  
      5. int height = options.outHeight;  
      6. int width = options.outWidth;  
      7. String mimeType = options.outMimeType;  
      8. Log.v("zxy","mimeType="+mimeType+",height="+height+",width="+width);  
      上述代码输出的是:可以知道确实是返回null了,而且还获得了该Bitmap的宽高等值。

2、通过实例来看看怎么压缩图片

[java] view plain copy
 print?
  1. public class MainActivity extends ActionBarActivity {  
  2.     private ImageView mImageView, mResizeImageView;  
  3.   
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_main);  
  8.         mImageView = (ImageView) findViewById(R.id.imageView);  
  9.         mResizeImageView = (ImageView) findViewById(R.id.resize_imageView);  
  10.         mImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.two));  
  11.   
  12.         Bitmap bitmap = compressBitmap(getResources(), R.mipmap.two, 100100);  
  13.         Log.v("zxy""compressBitmap,width=" + bitmap.getWidth() + ",height=" + bitmap.getHeight());  
  14.         mResizeImageView.setImageBitmap(bitmap);  
  15.     }  
  16.   
  17.     /** 
  18.      * @param res Resource 
  19.      * @param resId 资源id 
  20.      * @param targetWidth 目标图片的宽,单位px 
  21.      * @param targetHeight 目标图片的高,单位px 
  22.      * @return 返回压缩后的图片的Bitmap 
  23.      */  
  24.     public Bitmap compressBitmap(Resources res, int resId, int targetWidth, int targetHeight) {  
  25.         final BitmapFactory.Options options = new BitmapFactory.Options();  
  26.         options.inJustDecodeBounds = true;//设为true,节约内存  
  27.         BitmapFactory.decodeResource(res, resId, options);//返回null  
  28.         int height = options.outHeight;//得到源图片height,单位px  
  29.         int width = options.outWidth;//得到源图片的width,单位px  
  30.         //计算inSampleSize  
  31.         options.inSampleSize = calculateInSampleSize(width,height,targetWidth,targetHeight);  
  32.         options.inJustDecodeBounds = false;//设为false,可以返回Bitmap  
  33.         return BitmapFactory.decodeResource(res,resId,options);  
  34.     }  
  35.   
  36.     /** 
  37.      * 计算压缩比例 
  38.      * @param width  源图片的宽 
  39.      * @param height 源图片的高 
  40.      * @param targetWidth  目标图片的宽 
  41.      * @param targetHeight 目标图片的高 
  42.      * @return inSampleSize 压缩比例 
  43.      */  
  44.     public int calculateInSampleSize(int width,int height, int targetWidth, int targetHeight) {  
  45.         int inSampleSize = 1;  
  46.         if (height > targetHeight || width > targetWidth) {  
  47.             //计算图片实际的宽高和目标图片宽高的比率  
  48.             final int heightRate = Math.round((float) height / (float) targetHeight);  
  49.             final int widthRate = Math.round((float) width / (float) targetWidth);  
  50.             //选取最小的比率作为inSampleSize  
  51.             inSampleSize = heightRate < widthRate ? heightRate : widthRate;  
  52.         }  
  53.         return inSampleSize;  
  54.     }  
  55. }  
我们通过看Log可以知道压缩后图片的宽高:

0 0
原创粉丝点击