国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android圖片壓縮的實例詳解

2019-10-22 18:29:52
字體:
來源:轉載
供稿:網友

Android圖片壓縮的實例詳解

在做微信分享的時候,由于分享的縮略圖要求不得大于32K,否則不能調起微信,所以總結了一下Android圖片的壓縮問題,大部分資料都是來自網上各位的分享,自己只是完善或修改了一下,本著繼續分享的精神,也方便自己記憶,于是總結如下。

android圖片壓縮主要有兩種方式:1.壓縮圖片分辨率 2.壓縮圖片質量

一、先看壓縮圖片分辨率,很好理解,如本來1280*768的圖片壓縮為640*384大小。廢話不說,直接上代碼:

/**    * 按比例壓縮圖片分辨率    * @param inBitmap    * @param outHeight 輸出圖片高度,可據此保持比例計算輸出寬度    * @param needRecycled 是否回收inBitmap    * @return    */   public static Bitmap createScaledBitmapByOutHeight(Bitmap inBitmap, int outHeight, boolean needRecycled) {          int bitmapHeight = inBitmap.getHeight();     int bitmapWidth = inBitmap.getWidth();     int outWidth = bitmapWidth * outHeight / bitmapHeight;          return createScaledBitmap(inBitmap, outWidth, outHeight, needRecycled);   }       /**    * 按比例壓縮圖片分辨率    * @param inBitmap    * @param outHeight 輸出圖片寬度,可據此保持比例計算輸出高度    * @param needRecycled 是否回收inBitmap    * @return    */   public static Bitmap createScaledBitmapByOutWidth(Bitmap inBitmap, int outWidth, boolean needRecycled) {         int bitmapHeight = inBitmap.getHeight();     int bitmapWidth = inBitmap.getWidth();     int outHeight = bitmapHeight * outWidth / bitmapWidth;          return createScaledBitmap(inBitmap, outWidth, outHeight, needRecycled);   }      /**    * 指定輸出的寬高縮放圖片    * @param inBitmap    * @param outWidth    * @param outHeight    * @param needRecycled    * @return    */   public static Bitmap createScaledBitmap(Bitmap inBitmap, int outWidth, int outHeight, boolean needRecycled) {                  Bitmap thumbBmp = Bitmap.createScaledBitmap(inBitmap, outWidth, outHeight, true);     if (needRecycled) {       inBitmap.recycle();     }          return thumbBmp;   } 

前兩個方法可以指定期望的寬度或高度,并按比例縮放圖片的分辨率,第3個方法可以隨意指定期望的寬高,縮放圖片。

上面代碼是對輸入的bitmap進行縮放,還可以從資源或文件中加載圖片并縮放,具體如下:

/**    * 從資源加載并壓縮圖片    * @param res    * @param resId    * @param outWidth 目標寬度    * @param outHeight 目標高度    * @return    */   public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,        int outWidth, int outHeight) {      final BitmapFactory.Options options = new BitmapFactory.Options();      options.inJustDecodeBounds = true; // 假解,來獲取圖片大小      BitmapFactory.decodeResource(res, resId, options);      options.inSampleSize = calculateInSampleSize(options, outWidth, outHeight);      // 使用獲取到的inSampleSize值再次解析圖片      options.inJustDecodeBounds = false;     //options.inPreferredConfig = Config.RGB_565;     return BitmapFactory.decodeResource(res, resId, options);    }       /**    * 從文件中加載并壓縮圖片    * @param res    * @param resId    * @param outWidth 目標寬度    * @param outHeight 目標高度    * @return    */   public static Bitmap decodeSampledBitmapFromFile(String pathName, int outWidth, int outHeight) {     final BitmapFactory.Options options = new BitmapFactory.Options();      options.inJustDecodeBounds = true; // 假解,來獲取圖片大小      BitmapFactory.decodeFile(pathName, options);     options.inSampleSize = calculateInSampleSize(options, outWidth, outHeight);      // 使用獲取到的inSampleSize值再次解析圖片      options.inJustDecodeBounds = false;     //options.inPreferredConfig = Config.RGB_565;     return BitmapFactory.decodeFile(pathName, options);   }       /**    * 計算options.inSampleSize    * @param options    * @param reqWidth    * @param reqHeight    * @return    */   public static int calculateInSampleSize(BitmapFactory.Options options,        int reqWidth, int reqHeight) {      // 源圖片的高度和寬度      final int height = options.outHeight;      final int width = options.outWidth;      int inSampleSize = 1;      if (height > reqHeight || width > reqWidth) {        // 計算出實際寬高和目標寬高的比率        final int heightRatio = Math.round((float) height / (float) reqHeight);        final int widthRatio = Math.round((float) width / (float) reqWidth);        // 選擇寬和高中最小的比率作為inSampleSize的值,這樣可以保證最終圖片的寬和高        // 一定都會大于等于目標的寬和高。        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;      }      return inSampleSize;    }  

二、壓縮圖片的質量

/**    * 壓縮圖片質量,把圖片壓縮到outSize以內    * @param inBitmap 原始bitmap    * @param outSize 壓縮到的大小    * @param needRecycled 是否回收bitmap    * @return    */   public static Bitmap compressImage(Bitmap inBitmap, int outSize, boolean needRecycled) {            ByteArrayOutputStream baos = new ByteArrayOutputStream();      inBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);     int quality = 100;      while (baos.toByteArray().length / 1024 > outSize) {       if (quality <= 0) {         ByteArrayInputStream outBais = new ByteArrayInputStream(baos.toByteArray());         return BitmapFactory.decodeStream(outBais, null, null);// 如果quaLity為0時還未達到32k以內,返回得到的最小值;如需要可結合分辨率壓縮       }       baos.reset();       //PNG格式下,這種壓縮不起作用(quality:0-100,如果目標大小太小,有時候質量壓縮不一定能達到效果,需結合分辨率壓縮)       inBitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);       Log.e("AN", "bitmap size:"+ baos.toByteArray().length / 1024 + "k");       quality -= 10;     }     if (needRecycled) {       inBitmap.recycle();     }               ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());     Bitmap outBitmap= BitmapFactory.decodeStream(bais, null, null);//ByteArrayInputStream轉成bitmap          return outBitmap;    } 

 

需要注意的是compress方法,該壓縮方法只對JPEG格式有效,對于PNG格式,則會忽略第二個參數quality,即壓縮不起作用。這種壓縮只是對圖片質量有影響,并不會改變圖片大小。

當然,如有需要,以上兩種壓縮方法可以結合使用。

以上就是Android 圖片壓縮的實現方法的詳解,如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陈巴尔虎旗| 静海县| 淳安县| 宜兰县| 北宁市| 明水县| 布尔津县| 涟水县| 太湖县| 天镇县| 通辽市| 三穗县| 玉林市| 宝坻区| 习水县| 临海市| 白河县| 龙泉市| 长垣县| 贵定县| 丹寨县| 石泉县| 甘孜县| 南部县| 弥渡县| 会同县| 垫江县| 信阳市| 庆元县| 贞丰县| 普安县| 邹城市| 平潭县| 图木舒克市| 沙湾县| 靖江市| 和平县| 兴仁县| 开原市| 沈阳市| 北票市|