Android中的图片的压缩

压缩图片

1.根据bitmap压缩

 1 /** 
 2  * 根据bitmap压缩 
 3  * @param image 
 4  * @return 
 5  */  
 6 private Bitmap comp(Bitmap image) {    
 7         
 8     ByteArrayOutputStream baos = new ByteArrayOutputStream();           
 9     image.compress(Bitmap.CompressFormat.JPEG, 100, baos);    
10     if( baos.toByteArray().length / 1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出      
11         baos.reset();//重置baos即清空baos    
12         image.compress(Bitmap.CompressFormat.JPEG, 50, baos);//这里压缩50%,把压缩后的数据存放到baos中    
13     }    
14     ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());    
15     BitmapFactory.Options newOpts = new BitmapFactory.Options();    
16     //开始读入图片,此时把options.inJustDecodeBounds 设回true了    
17     newOpts.inJustDecodeBounds = true;    
18     Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);    
19     newOpts.inJustDecodeBounds = false;    
20     int w = newOpts.outWidth;    
21     int h = newOpts.outHeight;    
22     //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为    
23     float hh = 800f;//这里设置高度为800f    
24     float ww = 480f;//这里设置宽度为480f    
25     //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可    
26     int be = 1;//be=1表示不缩放    
27     if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放    
28         be = (int) (newOpts.outWidth / ww);    
29     } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放    
30         be = (int) (newOpts.outHeight / hh);    
31     }    
32     if (be <= 0)    
33         be = 1;    
34     newOpts.inSampleSize = be;//设置缩放比例    
35     //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了    
36     isBm = new ByteArrayInputStream(baos.toByteArray());    
37     bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);    
38     return compressImage(bitmap);//压缩好比例大小后再进行质量压缩    
39 }    
40   
41   
42   
43   
44 private Bitmap compressImage(Bitmap image) {    
45         
46        ByteArrayOutputStream baos = new ByteArrayOutputStream();    
47        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中    
48        int options = 100;    
49        while ( baos.toByteArray().length / 1024>100) {  //循环判断如果压缩后图片是否大于100kb,大于继续压缩           
50            baos.reset();//重置baos即清空baos    
51            image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中    
52            options -= 10;//每次都减少10    
53        }    
54        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中    
55        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片    
56        return bitmap;    
57    }   

2、根据图片地址压缩

 1 private Bitmap getimage(String srcPath) {    
 2         BitmapFactory.Options newOpts = new BitmapFactory.Options();    
 3         //开始读入图片,此时把options.inJustDecodeBounds 设回true了    
 4         newOpts.inJustDecodeBounds = true;    
 5         Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此时返回bm为空    
 6             
 7         newOpts.inJustDecodeBounds = false;    
 8         int w = newOpts.outWidth;    
 9         int h = newOpts.outHeight;    
10         //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为    
11         float hh = 800f;//这里设置高度为800f    
12         float ww = 480f;//这里设置宽度为480f    
13         //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可    
14         int be = 1;//be=1表示不缩放    
15         if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放    
16             be = (int) (newOpts.outWidth / ww);    
17         } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放    
18             be = (int) (newOpts.outHeight / hh);    
19         }    
20         if (be <= 0)    
21             be = 1;    
22         newOpts.inSampleSize = be;//设置缩放比例    
23         //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了    
24         bitmap = BitmapFactory.decodeFile(srcPath, newOpts);    
25         return compressImage(bitmap);//压缩好比例大小后再进行质量压缩    
26     }   

 

posted @ 2016-03-04 17:06  一抹火焰  阅读(228)  评论(0)    收藏  举报