Android压缩图片到100K以下并保持不失真的高效方法

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

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





使用的第三方库jar包,如下图所示:

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

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  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 plaincopy在CODE上查看代码片派生到我的代码片
 
  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, 480, 800);  
  9.   
  10.         // Decode bitmap with inSampleSize set  
  11.     options.inJustDecodeBounds = false;  
  12.   
  13.     return BitmapFactory.decodeFile(filePath, options);  
  14.     }  

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  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 plaincopy在CODE上查看代码片派生到我的代码片
 
  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 plaincopy在CODE上查看代码片派生到我的代码片
 
  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 plaincopy在CODE上查看代码片派生到我的代码片
 
  1. //在内存中创建bitmap对象,这个对象按照缩放大小创建的  
  2.             options.inSampleSize = calculateInSampleSize(options, 480, 800);  
  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左右,并且没有失真。
效果图如下:
demo.jpg

更新:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. /* 
  2. 压缩图片,处理某些手机拍照角度旋转的问题 
  3. */  
  4. public static String compressImage(Context context,String filePath,String fileName,int q) throws FileNotFoundException {  
  5.   
  6.         Bitmap bm = getSmallBitmap(filePath);  
  7.   
  8.         int degree = readPictureDegree(filePath);  
  9.   
  10.         if(degree!=0){//旋转照片角度  
  11.             bm=rotateBitmap(bm,degree);  
  12.         }  
  13.   
  14.         File imageDir = SDCardUtils.getImageDir(context);  
  15.   
  16.         File outputFile=new File(imageDir,fileName);  
  17.   
  18.         FileOutputStream out = new FileOutputStream(outputFile);  
  19.   
  20.         bm.compress(Bitmap.CompressFormat.JPEG, q, out);  
  21.   
  22.         return outputFile.getPath();  
  23.     }  


判断照片角度

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. public static int readPictureDegree(String path) {  
  2.         int degree = 0;  
  3.         try {  
  4.             ExifInterface exifInterface = new ExifInterface(path);  
  5.             int orientation = exifInterface.getAttributeInt(  
  6.                     ExifInterface.TAG_ORIENTATION,  
  7.                     ExifInterface.ORIENTATION_NORMAL);  
  8.             switch (orientation) {  
  9.             case ExifInterface.ORIENTATION_ROTATE_90:  
  10.                 degree = 90;  
  11.                 break;  
  12.             case ExifInterface.ORIENTATION_ROTATE_180:  
  13.                 degree = 180;  
  14.                 break;  
  15.             case ExifInterface.ORIENTATION_ROTATE_270:  
  16.                 degree = 270;  
  17.                 break;  
  18.             }  
  19.         } catch (IOException e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.         return degree;  
  23.     }  


旋转照片

 

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
    1. public static Bitmap rotateBitmap(Bitmap bitmap,int degress) {  
    2.         if (bitmap != null) {  
    3.             Matrix m = new Matrix();  
    4.             m.postRotate(degress);   
    5.             bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),  
    6.                     bitmap.getHeight(), m, true);  
    7.             return bitmap;  
    8.         }  
    9.         return bitmap;  
    10.     }  
posted @ 2015-09-23 19:08  brave-sailor  阅读(532)  评论(0编辑  收藏  举报