zoukankan      html  css  js  c++  java
  • android bitmap compress

    android的照相功能随着手机硬件的发展,变得越来越强大,能够找出很高分辨率的图片。
    有些场景中,需要照相并且上传到服务,但是由于图片的大小太大,那么就上传就会很慢(在有些网络情况下),而且很耗流量,要想速度快,那么就需要减小图片的大小。减少图片的大小有两种方法,1. 照小图片; 2. 压缩大图片。 照相时获取小图片一般不太符合要求,因为,图片的清晰度会很差,但是这种情况有个好处就是应用速度会快些; 压缩图片,就是把大图片压缩小,降低图片的质量,在一定范围内,降低图片的大小,并且满足需求(图片仍就清晰)。下面组要是介绍图片的压缩:
     
    1. 照相请查看http://blog.csdn.net/luhuajcdd/article/details/8826587 ->想要保存图片到制定目录,启动Camera应用时,需要指定文件
    2. 压缩过程:
        2.1 从图片路径中读取图片(图片很大,不能全部加在到内存中处理,要是全部加载到内存中会内存溢出)
    [java] view plain
    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, 480, 800);  
    7.   
    8.     // Decode bitmap with inSampleSize set  
    9.     options.inJustDecodeBounds = false;  
    10.       
    11.     Bitmap bm = BitmapFactory.decodeFile(filePath, options);  
        2.2 处理图片旋转   
    [java] view plain
    1. int degree = readPictureDegree(filePath);  
    2.         bm = rotateBitmap(bm,degree) ;  
    [java] view plain
    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 plain
    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, 0, 0, w, h, mtx, true);  
    12.     }  
        2.3压缩图片        
    [java] view plain
    1. bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 是压缩率,表示压缩70%; 如果不压缩是100,表示压缩率为0  
     
    完整的方法代码:
    [java] view plain
    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, 480, 800);  
    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 plain
    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.     }  
  • 相关阅读:
    AngularJs 1.x和AngularJs2的区别
    AngularJS ui-router (嵌套路由)
    $scope.$apply
    AngularJS中的$watch(),$digest()和$apply()区分
    对比jQuery和AngularJS的不同思维模式
    angular.element 动态添加和删除元素
    [Leetcode] Integer to Roman
    [Leetcode] Longest Common Prefix
    [Leetcode] Plus One
    [Leetcode] Palindrome Number
  • 原文地址:https://www.cnblogs.com/to-creat/p/5185362.html
Copyright © 2011-2022 走看看