`
chriszeng87
  • 浏览: 717286 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

android bitmap compress(图片压缩)

阅读更多

转自:http://blog.csdn.net/luhuajcdd/article/details/8948261

 

android bitmap compress

android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片。
有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些网络情况下),而且很耗流量,要想速度快,那么就需要减小图片的大小。减少图片的大小有两种方法,1. 照小图片; 2. 压缩大图片。 照相时获取小图片一般不太符合要求,因为,图片的清晰度会很差,但是这种情况有个好处就是应用速度会快些; 压缩图片,就是把大图片压缩小,降低图片的质量,在一定范围内,降低图片的大小,并且满足需求(图片仍就清晰)。下面组要是介绍图片的压缩:
 
1. 照相请查看http://blog.csdn.net/luhuajcdd/article/details/8826587 ->想要保存图片到制定目录,启动Camera应用时,需要指定文件
2. 压缩过程:
    2.1 从图片路径中读取图片(图片很大,不能全部加在到内存中处理,要是全部加载到内存中会内存溢出)
[java] view plaincopy
 
  1. final BitmapFactory.Options options = new BitmapFactory.Options();  
  2.     options.inJustDecodeBounds = true;  
  3.     BitmapFactory.decodeFile(filePath, options);  
  4.   
  5.     // Calculate inSampleSize  
  6.     options.inSampleSize = calculateInSampleSize(options, 480800);  
  7.   
  8.     // Decode bitmap with inSampleSize set  
  9.     options.inJustDecodeBounds = false;  
  10.       
  11.     Bitmap bm = BitmapFactory.decodeFile(filePath, options);  
    2.2 处理图片旋转   
[java] view plaincopy
 
  1. int degree = readPictureDegree(filePath);  
  2.         bm = rotateBitmap(bm,degree) ;  
[java] view plaincopy
 
  1. private static int readPictureDegree(String path) {    
  2.            int degree  = 0;    
  3.            try {    
  4.                    ExifInterface exifInterface = new ExifInterface(path);    
  5.                    int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);    
  6.                    switch (orientation) {    
  7.                    case ExifInterface.ORIENTATION_ROTATE_90:    
  8.                            degree = 90;    
  9.                            break;    
  10.                    case ExifInterface.ORIENTATION_ROTATE_180:    
  11.                            degree = 180;    
  12.                            break;    
  13.                    case ExifInterface.ORIENTATION_ROTATE_270:    
  14.                            degree = 270;    
  15.                            break;    
  16.                    }    
  17.            } catch (IOException e) {    
  18.                    e.printStackTrace();    
  19.            }    
  20.            return degree;    
  21.        }   
[java] view plaincopy
 
  1. private static Bitmap rotateBitmap(Bitmap bitmap, int rotate){  
  2.         if(bitmap == null)  
  3.             return null ;  
  4.           
  5.         int w = bitmap.getWidth();  
  6.         int h = bitmap.getHeight();  
  7.   
  8.         // Setting post rotate to 90  
  9.         Matrix mtx = new Matrix();  
  10.         mtx.postRotate(rotate);  
  11.         return Bitmap.createBitmap(bitmap, 00, w, h, mtx, true);  
  12.     }  
    2.3压缩图片        
[java] view plaincopy
 
  1. bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0  
 
完整的方法代码:
[java] view plaincopy
 
  1. public static Bitmap getSmallBitmap(String filePath) {  
  2.               
  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.         Bitmap bm = BitmapFactory.decodeFile(filePath, options);  
  14.         if(bm == null){  
  15.             return  null;  
  16.         }  
  17.         int degree = readPictureDegree(filePath);  
  18.         bm = rotateBitmap(bm,degree) ;  
  19.         ByteArrayOutputStream baos = null ;  
  20.         try{  
  21.             baos = new ByteArrayOutputStream();  
  22.             bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);  
  23.               
  24.         }finally{  
  25.             try {  
  26.                 if(baos != null)  
  27.                     baos.close() ;  
  28.             } catch (IOException e) {  
  29.                 e.printStackTrace();  
  30.             }  
  31.         }  
  32.         return bm ;  
  33.   
  34.     }  
[java] view plaincopy
 
  1. private static int calculateInSampleSize(BitmapFactory.Options options,  
  2.             int reqWidth, int reqHeight) {  
  3.         // Raw height and width of image  
  4.         final int height = options.outHeight;  
  5.         final int width = options.outWidth;  
  6.         int inSampleSize = 1;  
  7.   
  8.         if (height > reqHeight || width > reqWidth) {  
  9.   
  10.             // Calculate ratios of height and width to requested height and  
  11.             // width  
  12.             final int heightRatio = Math.round((float) height  
  13.                     / (float) reqHeight);  
  14.             final int widthRatio = Math.round((float) width / (float) reqWidth);  
  15.   
  16.             // Choose the smallest ratio as inSampleSize value, this will  
  17.             // guarantee  
  18.             // a final image with both dimensions larger than or equal to the  
  19.             // requested height and width.  
  20.             inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;  
  21.         }  
  22.   
  23.         return inSampleSize;  
  24.     }  
分享到:
评论

相关推荐

    android bitmap compress(图片压缩)代码

    android bitmap compress(图片压缩)代码,需要的朋友可以参考一下

    Android Bitmap压缩方式分析

    在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩);质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的...

    Bitmap压缩原理和理论分析.jpg

    1:介绍图片耗用内存资源原理,以及计算图片内存方法 2:Bitmap压缩图片基本手段和思路

    Android-CompressHelper压缩文件压缩图片压缩Bitmap

    CompressHelper 压缩文件,压缩图片,压缩Bitmap,Compress,CompressImage,CompressFile,CompressBitmap

    Android图片压缩方法并压缩到指定大小

    一、图片质量压缩 /** * 质量压缩方法 * @param image * @return */ public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress...

    Android代码-压缩库,主要通过尺寸压缩和质量压缩,以达到清晰度最优

    压缩,图片压缩,压缩Bitmap,Compress,CompressImage,CompressFile,CompressBitmap 主要通过尺寸压缩和质量压缩,以达到清晰度最优,该项目参考了 https://github.com/zetbaitsu/Compressor 的部分代码,且在基础上...

    android图片压缩的3种方法实例

    android 图片压缩方法: 第一:质量压缩法: 代码如下:private Bitmap compressImage(Bitmap image) {  ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG,...

    图片压缩工具Android-BitherCompress.zip

    Android图片压缩结合多种压缩方式,常用的有尺寸压缩、质量压缩以及通过JNI调用libjpeg库来进行压缩,三种方式结合使用实现指定图片内存大小,清晰度达到最优。使用:导入lib-bither-compress NativeUtil....

    Android代码-CompressTools-Android

    CompressTools-Android 压缩,图片压缩,压缩Bitmap 特性: *这是和微信压缩效果类似的压缩方式,采用底层压缩。 *尽量无损压缩图片,保持清晰度最优。可以对比原生方法bitmap.compress(CompressFormat.JPEG, ...

    c#实现图片压缩与解压缩

    是一种游长的压缩和解压缩算法,是针对bitmap 图像rgb有损的压缩方法,特点是速度快,使用资源少,图像损失小,压缩比较高,适合图像在局域网内传输, 可以将c#语言改成verilog,做实时压缩和传输

    安卓图片压缩

    可以对比原生方法bitmap.compress(CompressFormat.JPEG, quality, fileOutputStream); *占用内存少,支持压缩生成原图分辨率图片; *支持批量压缩,采用线程池提高性能 *支持压缩监听回调 *支持返回Bitmap和File

    安卓高清无损压缩图片.zip

    可以对比原生方法bitmap.compress(CompressFormat.JPEG, quality, fileOutputStream); *占用内存少,支持压缩生成原图分辨率图片; *支持批量压缩,采用线程池提高性能 *支持压缩监听回调 *支持返回Bitmap和File...

    CompressImage

    android 完美图片压缩处理工具类demo

    Tiny:图像压缩框架。(一个高保真,高压缩比的图片压缩框架)

    微小的图像压缩框架。 或压缩效果图像信息微小的微信6.66MB(3500x2156) 151KB(1280x788) 135KB(1280x788) 4.28MB(4160x3120) 219KB(1280x960) 195KB(1280x960) 2.60MB(4032x3024) 193KB(1280x960) ...

Global site tag (gtag.js) - Google Analytics