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.     }  
相關文章
相關標籤/搜索