圖片壓縮方法

Android不錯的圖片壓縮方法


一、圖片質量壓縮

  1. /** 
  2.      * 質量壓縮方法 
  3.      * 
  4.      * @param image 
  5.      * @return 
  6.      */  
  7.     public static Bitmap compressImage(Bitmap image) {  
  8.   
  9.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  10.         image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 質量壓縮方法,這裏100表示不壓縮,把壓縮後的數據存放到baos中  
  11.         int options = 90;  
  12.   
  13.         while (baos.toByteArray().length / 1024 > 100) { // 循環判斷如果壓縮後圖片是否大於100kb,大於繼續壓縮  
  14.             baos.reset(); // 重置baos即清空baos  
  15.             image.compress(Bitmap.CompressFormat.JPEG, options, baos);// 這裏壓縮options%,把壓縮後的數據存放到baos中  
  16.             options -= 10;// 每次都減少10  
  17.         }  
  18.         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把壓縮後的數據baos存放到ByteArrayInputStream中  
  19.         Bitmap bitmap = BitmapFactory.decodeStream(isBm, nullnull);// 把ByteArrayInputStream數據生成圖片  
  20.         return bitmap;  
  21.     }  



二、按比例大小壓縮 (路徑獲取圖片)

  1. /** 
  2.      * 圖片按比例大小壓縮方法 
  3.      * 
  4.      * @param srcPath (根據路徑獲取圖片並壓縮) 
  5.      * @return 
  6.      */  
  7.     public static Bitmap getimage(String srcPath) {  
  8.   
  9.         BitmapFactory.Options newOpts = new BitmapFactory.Options();  
  10.         // 開始讀入圖片,此時把options.inJustDecodeBounds 設回true了  
  11.         newOpts.inJustDecodeBounds = true;  
  12.         Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此時返回bm爲空  
  13.   
  14.         newOpts.inJustDecodeBounds = false;  
  15.         int w = newOpts.outWidth;  
  16.         int h = newOpts.outHeight;  
  17.         // 現在主流手機比較多是800*480分辨率,所以高和寬我們設置爲  
  18.         float hh = 800f;// 這裏設置高度爲800f  
  19.         float ww = 480f;// 這裏設置寬度爲480f  
  20.         // 縮放比。由於是固定比例縮放,只用高或者寬其中一個數據進行計算即可  
  21.         int be = 1;// be=1表示不縮放  
  22.         if (w > h && w > ww) {// 如果寬度大的話根據寬度固定大小縮放  
  23.             be = (int) (newOpts.outWidth / ww);  
  24.         } else if (w < h && h > hh) {// 如果高度高的話根據寬度固定大小縮放  
  25.             be = (int) (newOpts.outHeight / hh);  
  26.         }  
  27.         if (be <= 0)  
  28.             be = 1;  
  29.         newOpts.inSampleSize = be;// 設置縮放比例  
  30.         // 重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了  
  31.         bitmap = BitmapFactory.decodeFile(srcPath, newOpts);  
  32.         return compressImage(bitmap);// 壓縮好比例大小後再進行質量壓縮  
  33.     }  



三、按比例大小壓縮 (Bitmap)

  1. /** 
  2.      * 圖片按比例大小壓縮方法 
  3.      * 
  4.      * @param image (根據Bitmap圖片壓縮) 
  5.      * @return 
  6.      */  
  7.     public static Bitmap compressScale(Bitmap image) {  
  8.   
  9.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  10.         image.compress(Bitmap.CompressFormat.JPEG, 100, baos);  
  11.   
  12.         // 判斷如果圖片大於1M,進行壓縮避免在生成圖片(BitmapFactory.decodeStream)時溢出  
  13.         if (baos.toByteArray().length / 1024 > 1024) {  
  14.             baos.reset();// 重置baos即清空baos  
  15.             image.compress(Bitmap.CompressFormat.JPEG, 80, baos);// 這裏壓縮50%,把壓縮後的數據存放到baos中  
  16.         }  
  17.         ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());  
  18.         BitmapFactory.Options newOpts = new BitmapFactory.Options();  
  19.         // 開始讀入圖片,此時把options.inJustDecodeBounds 設回true了  
  20.         newOpts.inJustDecodeBounds = true;  
  21.         Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);  
  22.         newOpts.inJustDecodeBounds = false;  
  23.         int w = newOpts.outWidth;  
  24.         int h = newOpts.outHeight;  
  25.         Log.i(TAG, w + "---------------" + h);  
  26.         // 現在主流手機比較多是800*480分辨率,所以高和寬我們設置爲  
  27.         // float hh = 800f;// 這裏設置高度爲800f  
  28.         // float ww = 480f;// 這裏設置寬度爲480f  
  29.         float hh = 512f;  
  30.         float ww = 512f;  
  31.         // 縮放比。由於是固定比例縮放,只用高或者寬其中一個數據進行計算即可  
  32.         int be = 1;// be=1表示不縮放  
  33.         if (w > h && w > ww) {// 如果寬度大的話根據寬度固定大小縮放  
  34.             be = (int) (newOpts.outWidth / ww);  
  35.         } else if (w < h && h > hh) { // 如果高度高的話根據高度固定大小縮放  
  36.             be = (int) (newOpts.outHeight / hh);  
  37.         }  
  38.         if (be <= 0)  
  39.             be = 1;  
  40.         newOpts.inSampleSize = be; // 設置縮放比例  
  41.         // newOpts.inPreferredConfig = Config.RGB_565;//降低圖片從ARGB888到RGB565  
  42.   
  43.         // 重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了  
  44.         isBm = new ByteArrayInputStream(baos.toByteArray());  
  45.         bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);  
  46.   
  47.         return compressImage(bitmap);// 壓縮好比例大小後再進行質量壓縮  
  48.   
  49.         //return bitmap;  
  50.     }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章