Android图片压缩技巧

来源:互联网 发布:淘宝店铺卖什么升级快 编辑:程序博客网 时间:2024/05/18 01:14

【原文地址 点击打开链接】


目录(?)[+]

请尊重他人的劳动成果,转载请注明出处:Android图片压缩技巧

http://blog.csdn.net/fengyuzhengfan/article/details/41759835

当需要将Android客户端的图片上传到服务器时,往往需要将图片进行压缩,关于图片的压缩方法,小编分享几种常用的方式:

第一种方式:裁切以达到压缩的目的

我曾在《Android开发之裁剪照片》一文中详细介绍过如何裁切照片,感兴趣的朋友可以去看一下。


第二种方式:将图片进行降质处理(即降低图片的质量)以达到压缩的目的

这种方式也是比较常用的方式,下面就为大家介绍如何对图片进行降质:

将图片降质我们可以使用Bitmap的这个方法:boolean android.graphics.Bitmap.compress(CompressFormat format, int quality, OutputStream stream)

其中,参数format表示压缩后的格式,quality压缩后的图片质量(0表示最低,100表示不压缩),stream表示要将压缩后的图片保存到的输出流。

下面是详细代码:

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 多线程压缩图片的质量 
  3.  * @author JPH 
  4.  * @param bitmap 内存中的图片 
  5.  * @param imgPath 图片的保存路径 
  6.  * @date 2014-12-5下午11:30:43 
  7.  */  
  8. public static void compressImageByQuality(final Bitmap bitmap,final String imgPath){  
  9.     new Thread(new Runnable() {//开启多线程进行压缩处理  
  10.         @Override  
  11.         public void run() {  
  12.             // TODO Auto-generated method stub  
  13.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  14.             options = 100;  
  15.             bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//质量压缩方法,把压缩后的数据存放到baos中 (100表示不压缩,0表示压缩到最小)  
  16.             while (baos.toByteArray().length / 1024 > 100) {//循环判断如果压缩后图片是否大于100kb,大于继续压缩           
  17.                 baos.reset();//重置baos即让下一次的写入覆盖之前的内容   
  18.                 options -= 10;//图片质量每次减少10  
  19.                 if(options<0)options=0;//如果图片质量小于10,则将图片的质量压缩到最小值  
  20.                 bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//将压缩后的图片保存到baos中  
  21.                 if(options==0)break;//如果图片的质量已降到最低则,不再进行压缩  
  22.             }  
  23.             try {  
  24.                 FileOutputStream fos = new FileOutputStream(new File(imgPath));//将压缩后的图片保存的本地上指定路径中  
  25.                 fos.write(baos.toByteArray());  
  26.                 fos.flush();  
  27.                 fos.close();  
  28.             } catch (Exception e) {  
  29.                 e.printStackTrace();  
  30.             }  
  31.         }  
  32.     }).start();  
  33. }  

方法解析:

由于此方法中包含I/O操作和递归调用比较耗时所以我采用了多线程去处理。


第三种方式:按比例缩小图片的像素以达到压缩的目的

此种方法主要是使用android.graphics.BitmapFactory.Options.Options()方法将图片以指定的采用率加载到内存然后输出到本地以达到压缩像素的目的。

详细代码:

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 按比例缩小图片的像素以达到压缩的目的 
  3.  * @author JPH 
  4.  * @param imgPath 
  5.  * @date 2014-12-5下午11:30:59 
  6.  */  
  7. public static void compressImageByPixel(String imgPath) {  
  8.     BitmapFactory.Options newOpts = new BitmapFactory.Options();  
  9.     newOpts.inJustDecodeBounds = true;//只读边,不读内容  
  10.     Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts);  
  11.     newOpts.inJustDecodeBounds = false;  
  12.     int width = newOpts.outWidth;  
  13.     int height = newOpts.outHeight;  
  14.     float maxSize = 1000f;//默认1000px  
  15.     int be = 1;  
  16.     if (width > height && width > maxSize) {//缩放比,用高或者宽其中较大的一个数据进行计算  
  17.         be = (int) (newOpts.outWidth / maxSize);  
  18.     } else if (width < height && height > maxSize) {  
  19.         be = (int) (newOpts.outHeight / maxSize);  
  20.     }  
  21.     be++;  
  22.     newOpts.inSampleSize = be;//设置采样率  
  23.     newOpts.inPreferredConfig = Config.ARGB_8888;//该模式是默认的,可不设  
  24.     newOpts.inPurgeable = true;// 同时设置才会有效  
  25.     newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收  
  26.     bitmap = BitmapFactory.decodeFile(imgPath, newOpts);  
  27.       
  28.     ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  29.     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  30.     try {  
  31.         FileOutputStream fos = new FileOutputStream(new File(imgPath));  
  32.         fos.write(baos.toByteArray());  
  33.         fos.flush();  
  34.         fos.close();  
  35.     } catch (Exception e) {  
  36.         e.printStackTrace();  
  37.     }  
  38. }  

第四种方式:将图片先按比例压缩然后再降质

此种方式主要结合第二种和第三种方法以下是详细代码:

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * 多线程压缩图片的质量 
  3.  * @author JPH 
  4.  * @param bitmap 内存中的图片 
  5.  * @param imgPath 图片的保存路径 
  6.  * @date 2014-12-5下午11:30:43 
  7.  */  
  8. public static void compressImageByQuality(final Bitmap bitmap,final String imgPath){  
  9.     new Thread(new Runnable() {//开启多线程进行压缩处理  
  10.         @Override  
  11.         public void run() {  
  12.             // TODO Auto-generated method stub  
  13.             ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  14.             options = 100;  
  15.             bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//质量压缩方法,把压缩后的数据存放到baos中 (100表示不压缩,0表示压缩到最小)  
  16.             while (baos.toByteArray().length / 1024 > 100) {//循环判断如果压缩后图片是否大于100kb,大于继续压缩           
  17.                 baos.reset();//重置baos即让下一次的写入覆盖之前的内容   
  18.                 options -= 10;//图片质量每次减少10  
  19.                 if(options<0)options=0;//如果图片质量小于10,则将图片的质量压缩到最小值  
  20.                 bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos);//将压缩后的图片保存到baos中  
  21.                 if(options==0)break;//如果图片的质量已降到最低则,不再进行压缩  
  22.             }  
  23.             try {  
  24.                 FileOutputStream fos = new FileOutputStream(new File(imgPath));//将压缩后的图片保存的本地上指定路径中  
  25.                 fos.write(baos.toByteArray());  
  26.                 fos.flush();  
  27.                 fos.close();  
  28.             } catch (Exception e) {  
  29.                 e.printStackTrace();  
  30.             }  
  31.         }  
  32.     }).start();  
  33. }  
  34. /** 
  35.  * 按比例缩小图片的像素以达到压缩的目的 
  36.  * @author JPH 
  37.  * @param imgPath 
  38.  * @date 2014-12-5下午11:30:59 
  39.  */  
  40. public static void compressImageByPixel(String imgPath) {  
  41.     BitmapFactory.Options newOpts = new BitmapFactory.Options();  
  42.     newOpts.inJustDecodeBounds = true;//只读边,不读内容  
  43.     Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts);  
  44.     newOpts.inJustDecodeBounds = false;  
  45.     int width = newOpts.outWidth;  
  46.     int height = newOpts.outHeight;  
  47.     float maxSize = 1000f;//默认1000px  
  48.     int be = 1;  
  49.     if (width > height && width > maxSize) {//缩放比,用高或者宽其中较大的一个数据进行计算  
  50.         be = (int) (newOpts.outWidth / maxSize);  
  51.     } else if (width < height && height > maxSize) {  
  52.         be = (int) (newOpts.outHeight / maxSize);  
  53.     }  
  54.     be++;  
  55.     newOpts.inSampleSize = be;//设置采样率  
  56.     newOpts.inPreferredConfig = Config.ARGB_8888;//该模式是默认的,可不设  
  57.     newOpts.inPurgeable = true;// 同时设置才会有效  
  58.     newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收  
  59.     bitmap = BitmapFactory.decodeFile(imgPath, newOpts);  
  60.     compressImageByQuality(bitmap,imgPath);//压缩好比例大小后再进行质量压缩    
  61. }  

0 0
原创粉丝点击