android图片压缩


安卓图片压缩有两种,一种是质量压缩,一种是像素压缩。

          质量压缩会改变图片保存到本地或网络传输过程的大小,但不会改变图片在内存中的大小。

         像素压缩不仅会改变图片保存到本地或网络传输过程的大小,也会改变图片在内存中的大小。

安卓中图片在内存中是Bitmap,bitmap的大小由图片的分辨率决定,当然会受Options的影响

如:

Options newOpts=new Options();
		newOpts.inSampleSize = 8;//设置采样率  ,像素压缩
        
        newOpts.inPreferredConfig = Config.ARGB_8888;//该模式是默认的,可不设     质量压缩
        newOpts.inPurgeable = true;// 同时设置才会有效  
        newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收  
          
        bitmap = BitmapFactory.decodeFile(srcPath, newOpts);  

如果你手机SD卡上的如果是100K,那么通过流的形式读到内存中,也一定是占100K的内存,注意是流的形式,不是Bitmap的形式,当图片以Bitmap的形式存在时,其占用的内存会瞬间变大

具体参考如下两篇文章


转自:http://my.eoe.cn/575776/archive/564.html

前言:目前一般手机的相机都能达到800万像素,像我的Galaxy Nexus才500万像素,拍摄的照片也有1.5M左右。这么大的照片上传到服务器,不仅浪费流量,同时还浪费时间。

在开发Android企业应用时,会经常上传图片到服务器,而我们公司目前维护的一个项目便是如此。该项目是通过私有apn与服务器进行交互的,联通的还好,但移动的速度实在太慢,客户在使用软件的过程中,由于上传的信息中可能包含多张图片,会经常出现上传图片失败的问题,为了解决这个问题,我们决定把照片压缩到100k以下,并且保证图片不失真(目前图片经过压缩后,大约300k左右)。于是我就重新研究了一下Android的图片压缩技术。

Android端目录结构如下图所示:

android图片压缩_第1张图片

android图片压缩_第2张图片

其中ksoap2-android-xxx.jar是Android用来调用webservice的,gson-xx.jar是把JavaBean转成Json数据格式的。 
本篇博客主要讲解图片压缩的,核心代码如下:

[java]  view plain copy
  1. //计算图片的缩放值  
  2. public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {  
  3.     final int height = options.outHeight;  
  4.     final int width = options.outWidth;  
  5.     int inSampleSize = 1;  
  6.   
  7.     if (height > reqHeight || width > reqWidth) {  
  8.              final int heightRatio = Math.round((float) height/ (float) reqHeight);  
  9.              final int widthRatio = Math.round((float) width / (float) reqWidth);  
  10.              inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
  11.     }  
  12.         return inSampleSize;  
  13. }  

[java]  view plain copy
  1. // 根据路径获得图片并压缩,返回bitmap用于显示  
  2. public static Bitmap getSmallBitmap(String filePath) {  
  3.         final BitmapFactory.Options options = new BitmapFactory.Options();  
  4.         options.inJustDecodeBounds = true;  
  5.         BitmapFactory.decodeFile(filePath, options);  
  6.   
  7.         // Calculate inSampleSize  
  8.     options.inSampleSize = calculateInSampleSize(options, 480800);  
  9.   
  10.         // Decode bitmap with inSampleSize set  
  11.     options.inJustDecodeBounds = false;  
  12.   
  13.     return BitmapFactory.decodeFile(filePath, options);  
  14.     }  

[java]  view plain copy
  1. //把bitmap转换成String  
  2. public static String bitmapToString(String filePath) {  
  3.   
  4.         Bitmap bm = getSmallBitmap(filePath);  
  5.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  6.         bm.compress(Bitmap.CompressFormat.JPEG, 40, baos);  
  7.         byte[] b = baos.toByteArray();  
  8.         return Base64.encodeToString(b, Base64.DEFAULT);  
  9.     }  

查看全部源码,请访问:
https://github.com/feicien/StudyDemo/tree/master/FileUploadDemo

压缩原理讲解:压缩一张图片。我们需要知道这张图片的原始大小,然后根据我们设定的压缩比例进行压缩。
这样我们就需要做3件事:
1.获取原始图片的长和宽

[java]  view plain copy
  1. BitmapFactory.Options options = new BitmapFactory.Options();  
  2.         options.inJustDecodeBounds = true;  
  3.         BitmapFactory.decodeFile(filePath, options);  
  4.                 int height = options.outHeight;  
  5.             int width = options.outWidth;  

以上代码是对图片进行解码,inJustDecodeBounds设置为true,可以不把图片读到内存中,但依然可以计算出图片的大小,这正好可以满足我们第一步的需要。
2.计算压缩比例

[java]  view plain copy
  1. int height = options.outHeight;  
  2.      int width = options.outWidth;   
  3.      int inSampleSize = 1;  
  4.      int reqHeight=800;  
  5.      int reqWidth=480;  
  6.      if (height > reqHeight || width > reqWidth) {  
  7.     final int heightRatio = Math.round((float) height/ (float) reqHeight);  
  8.     final int widthRatio = Math.round((float) width / (float) reqWidth);              
  9.     inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
  10.      }  

一般手机的分辨率为 480*800 ,所以我们压缩后图片期望的宽带定为480,高度设为800,这2个值只是期望的宽度与高度,实际上压缩后的实际宽度也高度会比期望的要大。如果图片的原始高度或者宽带大约我们期望的宽带和高度,我们需要计算出缩放比例的数值。否则就不缩放。heightRatio是图片原始高度与压缩后高度的倍数,widthRatio是图片原始宽度与压缩后宽度的倍数。inSampleSize为heightRatio与widthRatio中最小的那个,inSampleSize就是缩放值。 inSampleSize为1表示宽度和高度不缩放,为2表示压缩后的宽度与高度为原来的1/2
3.缩放并压缩图片

[java]  view plain copy
  1. //在内存中创建bitmap对象,这个对象按照缩放大小创建的  
  2.              options.inSampleSize = calculateInSampleSize(options, 480800);  
  3.         options.inJustDecodeBounds = false;  
  4.         Bitmap bitmap= BitmapFactory.decodeFile(filePath, options);  
  5.   
  6.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  7.         bm.compress(Bitmap.CompressFormat.JPEG, 60, baos);  
  8.         byte[] b = baos.toByteArray();  

前3行的代码其实已经得到了一个缩放的bitmap对象,如果你在应用中显示图片,就可以使用这个bitmap对象了。由于考虑到网络流量的问题。我们好需要牺牲图片的质量来换取一部分空间,这里调用bm.compress()方法进行压缩,这个方法的第二个参数,如果是100,表示不压缩,我这里设置的是60,你也可以更加你的需要进行设置,在实验的过程中我设置为30,图片都不会失真。
压缩效果:本demo可以把1.5M左右的图片压缩到100K左右,并且没有失真。
效果图如下:

android图片压缩_第3张图片



、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、


转自  http://blog.csdn.net/cherry609195946/article/details/9264409

首先该文章是总结, 不是原创, 是通过看网上其他大神的文章和自己的一些实践总结出来的. 

一.图片的存在形式

1.文件形式(即以二进制形式存在于硬盘上)
2.流的形式(即以二进制形式存在于内存中)
3.Bitmap形式
这三种形式的区别: 文件形式和流的形式对图片体积大小并没有影响,也就是说,如果你手机SD卡上的如果是100K,那么通过流的形式读到内存中,也一定是占100K的内存,注意是流的形式,不是Bitmap的形式,当图片以Bitmap的形式存在时,其占用的内存会瞬间变大, 我试过500K文件形式的图片加载到内存,以Bitmap形式存在时,占用内存将近10M,当然这个增大的倍数并不是固定的

检测图片三种形式大小的方法:
文件形式: file.length()
流的形式: 讲图片文件读到内存输入流中,看它的byte数
Bitmap:    bitmap.getByteCount()

二.常见的压缩方式

1. 将图片保存到本地时进行压缩, 即将图片从Bitmap形式变为File形式时进行压缩,
    特点是:  File形式的图片确实被压缩了, 但是当你重新读取压缩后的file为 Bitmap是,它占用的内存并没有改变   
[java]  view plain copy
  1. public static void compressBmpToFile(Bitmap bmp,File file){  
  2.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  3.         int options = 80;//个人喜欢从80开始,  
  4.         bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);  
  5.         while (baos.toByteArray().length / 1024 > 100) {   
  6.             baos.reset();  
  7.             options -= 10;  
  8.             bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);  
  9.         }  
  10.         try {  
  11.             FileOutputStream fos = new FileOutputStream(file);  
  12.             fos.write(baos.toByteArray());  
  13.             fos.flush();  
  14.             fos.close();  
  15.         } catch (Exception e) {  
  16.             e.printStackTrace();  
  17.         }  
  18.     }  
方法说明: 该方法是压缩图片的质量, 注意它不会减少图片的像素,比方说, 你的图片是300K的, 1280*700像素的, 经过该方法压缩后, File形式的图片是在100以下, 以方便上传服务器, 但是你BitmapFactory.decodeFile到内存中,变成Bitmap时,它的像素仍然是1280*700, 计算图片像素的方法是 bitmap.getWidth()和bitmap.getHeight(), 图片是由像素组成的, 每个像素又包含什么呢? 熟悉PS的人知道, 图片是有色相,明度和饱和度构成的. 

该方法的官方文档也解释说, 它会让图片重新构造, 但是有可能图像的位深(即色深)和每个像素的透明度会变化,JPEG onlysupports opaque(不透明), 也就是说以jpeg格式压缩后, 原来图片中透明的元素将消失.所以这种格式很可能造成失真

既然它是改变了图片的显示质量, 达到了对File形式的图片进行压缩, 图片的像素没有改变的话, 那重新读取经过压缩 的file为Bitmap时, 它占用的内存并不会少.(不相信的可以试试)

因为: bitmap.getByteCount() 是计算它的像素所占用的内存, 请看官方解释: Returns the number of bytes used to  store this bitmap's pixels.

2.    将图片从本地读到内存时,进行压缩 ,即图片从File形式变为Bitmap形式
       特点: 通过设置采样率, 减少图片的像素, 达到对内存中的Bitmap进行压缩
       先看一个方法: 该方法是对内存中的Bitmap进行质量上的压缩, 由上面的理论可以得出该方法是无效的, 而且也是没有必要的, 因为你已经将它读到内存中了,再压缩多此一举, 尽管在获取系统相册图片时,某些手机会直接返回一个Bitmap, 但是这种情况下, 返回的Bitmap都是经过压缩的, 它不可能直接返回一个原声的Bitmap形式的图片, 后果可想而知
[java]  view plain copy
  1. private Bitmap compressBmpFromBmp(Bitmap image) {  
  2.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  3.         int options = 100;  
  4.         image.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  5.         while (baos.toByteArray().length / 1024 > 100) {   
  6.             baos.reset();  
  7.             options -= 10;  
  8.             image.compress(Bitmap.CompressFormat.JPEG, options, baos);  
  9.         }  
  10.         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  
  11.         Bitmap bitmap = BitmapFactory.decodeStream(isBm, nullnull);  
  12.         return bitmap;  
  13.     }  
  再看一个方法:
[java]  view plain copy
  1.     private Bitmap compressImageFromFile(String srcPath) {  
  2.         BitmapFactory.Options newOpts = new BitmapFactory.Options();  
  3.         newOpts.inJustDecodeBounds = true;//只读边,不读内容  
  4.         Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);  
  5.   
  6.         newOpts.inJustDecodeBounds = false;  
  7.         int w = newOpts.outWidth;  
  8.         int h = newOpts.outHeight;  
  9.         float hh = 800f;//  
  10.         float ww = 480f;//  
  11.         int be = 1;  
  12.         if (w > h && w > ww) {  
  13.             be = (int) (newOpts.outWidth / ww);  
  14.         } else if (w < h && h > hh) {  
  15.             be = (int) (newOpts.outHeight / hh);  
  16.         }  
  17.         if (be <= 0)  
  18.             be = 1;  
  19.         newOpts.inSampleSize = be;//设置采样率  
  20.           
  21.         newOpts.inPreferredConfig = Config.ARGB_8888;//该模式是默认的,可不设  
  22.         newOpts.inPurgeable = true;// 同时设置才会有效  
  23.         newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收  
  24.           
  25.         bitmap = BitmapFactory.decodeFile(srcPath, newOpts);  
  26. //      return compressBmpFromBmp(bitmap);//原来的方法调用了这个方法企图进行二次压缩  
  27.                                     //其实是无效的,大家尽管尝试  
  28.         return bitmap;  
  29.     }  


方法说明: 该方法就是对Bitmap形式的图片进行压缩, 也就是通过设置采样率, 减少Bitmap的像素, 从而减少了它所占用的内存


你可能感兴趣的:(android)