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

首頁 > 系統 > Android > 正文

Android圖片壓縮幾種方式總結

2019-12-12 02:39:46
字體:
來源:轉載
供稿:網友

Android圖片壓縮幾種方式總結

圖片壓縮在Android開發中很常見也很重要,防止圖片的OOM也是壓縮的重要原因。

首先看下Bitmap圖片文件的大小的決定因素:

Bitmap所占用的內存 = 圖片長度 x 圖片寬度 x 一個像素點占用的字節數。3個參數,任意減少一個的值,就達到了壓縮的效果。

接下來看下Bitmap圖片的幾種格式的特點:

ALPHA_8
 表示8位Alpha位圖,即A=8,一個像素點占用1個字節,它沒有顏色,只有透明度
 ARGB_4444
表示16位ARGB位圖,即A=4,R=4,G=4,B=4,一個像素點占4+4+4+4=16位,2個字節
ARGB_8888
表示32位ARGB位圖,即A=8,R=8,G=8,B=8,一個像素點占8+8+8+8=32位,4個字節
RGB_565
表示16位RGB位圖,即R=5,G=6,B=5,它沒有透明度,一個像素點占5+6+5=16位,2個字節

如果進行圖片格式的壓縮的話,一般情況下都是ARGB_8888轉為RGB565進行壓縮。

寫了一個工具類,基本上列舉了android上圖片的幾種基本壓縮方式:

1.質量壓縮

2.采樣率壓縮

3.尺寸壓縮

4.Matrix壓縮

5.圖片格式的壓縮,例如PNG和JPG保存后的圖片大小是不同的

public class Utils {    /**    * 采樣率壓縮    *    * @param bitmap    * @param sampleSize 采樣率為2的整數倍,非整數倍四舍五入,如4的話,就是原圖的1/4    * @return 尺寸變化    */   public static Bitmap getBitmap(Bitmap bitmap, int sampleSize) {     BitmapFactory.Options options = new BitmapFactory.Options();     options.inSampleSize = sampleSize;     ByteArrayOutputStream baos = new ByteArrayOutputStream();     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);     byte[] bytes = baos.toByteArray();     Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);     Log.i("info", "圖片大小:" + bit.getByteCount());//2665296  10661184     return bit;   }    /**    * 圖片質量壓縮    *    * @param bitmap    * @param quality    * @return 尺寸不變,質量變小    */   public static Bitmap compressByQuality(Bitmap bitmap, int quality) {     ByteArrayOutputStream baos = new ByteArrayOutputStream();     bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);     byte[] bytes = baos.toByteArray();     Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);     Log.i("info", "圖片大小:" + bit.getByteCount());//10661184     return bit;   }    /**    * 圖片質量壓縮    *    * @param src    * @param maxByteSize    * @return    */   public static Bitmap compressByQuality(Bitmap src, long maxByteSize) {     ByteArrayOutputStream baos = new ByteArrayOutputStream();     int quality = 100;     src.compress(Bitmap.CompressFormat.JPEG, quality, baos);     while (baos.toByteArray().length > maxByteSize && quality > 0) {       baos.reset();       src.compress(Bitmap.CompressFormat.JPEG, quality -= 5, baos);     }     if (quality < 0) return null;     byte[] bytes = baos.toByteArray();     Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);     return bit;   }    public static Bitmap compressByFormat(Bitmap bitmap, int format) {     ByteArrayOutputStream baos = new ByteArrayOutputStream();     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);     byte[] bytes = baos.toByteArray();     Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);     Log.i("info", "圖片大小:" + bit.getByteCount());//10661184     return bit;   }    /**    * Matrix縮放    *    * @param bitmap    * @param scaleWidth    * @param scaleHeight    * @return 尺寸和大小變化    */   public static Bitmap getBitmapBySize(Bitmap bitmap, float scaleWidth, float scaleHeight) {     Matrix matrix = new Matrix();     matrix.postScale(scaleWidth, scaleHeight);     Bitmap bit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);     Log.i("info", "圖片大小:" + bit.getByteCount());     return bit;   }    /**    * 按照圖片格式配置壓縮    *    * @param path    * @param config ALPHA_8,ARGB_4444,ARGB_8888,RGB_565;    * @return RGB_565比ARGB_8888節省一半內存    */   public static Bitmap getBitmapByFormatConfig(String path, Bitmap.Config config) {     BitmapFactory.Options options = new BitmapFactory.Options();     options.inPreferredConfig = config;     Bitmap bitmap = BitmapFactory.decodeFile(path, options);     Log.i("info", "圖片大小:" + bitmap.getByteCount());     return bitmap;   }    /**    * 指定大小縮放    *    * @param bitmap    * @param width    * @param height    * @return    */   public static Bitmap getBitmapByScaleSize(Bitmap bitmap, int width, int height) {     Bitmap bit = Bitmap.createScaledBitmap(bitmap, width, height, true);     Log.i("info", "圖片大小:" + bit.getByteCount());     return bit;   }    /**    * 通過保存格式壓縮    *    * @param bitmap    * @param format JPEG,PNG,WEBP    * @return    */   public static Bitmap getBitmapByFormat(Bitmap bitmap, Bitmap.CompressFormat format) {     ByteArrayOutputStream baos = new ByteArrayOutputStream();     bitmap.compress(format, 100, baos);     byte[] bytes = baos.toByteArray();     Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);     Log.i("info", "圖片大小:" + bit.getByteCount());     return bit;   }    /**    * 文件加載壓縮    *    * @param filePath    * @param inSampleSize    * @return    */   public static Bitmap getBitmap(String filePath, int inSampleSize) {     BitmapFactory.Options options = new BitmapFactory.Options();     options.inJustDecodeBounds = true;     BitmapFactory.decodeFile(filePath, options);//此時不耗費和占用內存     options.inSampleSize = inSampleSize;     options.inJustDecodeBounds = false;     return BitmapFactory.decodeFile(filePath, options);   }    public static Bitmap getBitmap(String filePath) {     return BitmapFactory.decodeFile(filePath);   }    public static Bitmap view2Bitmap(View view) {     if (view == null) return null;     Bitmap ret = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);     Canvas canvas = new Canvas(ret);     Drawable bgDrawable = view.getBackground();     if (bgDrawable != null) {       bgDrawable.draw(canvas);     } else {       canvas.drawColor(Color.WHITE);     }     view.draw(canvas);     return ret;   }    public static void saveBitmap(Bitmap bitmap) {     File file = new File(Environment.getExternalStorageDirectory() + "/img.jpg");     try {       FileOutputStream fileOutputStream = new FileOutputStream(file);       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);       fileOutputStream.flush();       fileOutputStream.close();     } catch (FileNotFoundException e) {       e.printStackTrace();     } catch (IOException e) {       e.printStackTrace();     }   }    public static void saveBitmap(Bitmap bitmap,Bitmap.CompressFormat format) {     File file = new File(Environment.getExternalStorageDirectory() + "/img.jpg");     try {       FileOutputStream fileOutputStream = new FileOutputStream(file);       bitmap.compress(format, 100, fileOutputStream);       fileOutputStream.flush();       fileOutputStream.close();     } catch (FileNotFoundException e) {       e.printStackTrace();     } catch (IOException e) {       e.printStackTrace();     }   } } 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浏阳市| 福海县| 巩留县| 朝阳区| 涞源县| 临汾市| 周宁县| 徐水县| 子长县| 茶陵县| 夏津县| 昂仁县| 南投市| 玉门市| 永清县| 克什克腾旗| 若羌县| 金阳县| 泰和县| 运城市| 郑州市| 建昌县| 遂溪县| 达拉特旗| 牙克石市| 宜昌市| 贵定县| 章丘市| 旬邑县| 定南县| 凤庆县| 景泰县| 裕民县| 泽库县| 会泽县| 浦东新区| 忻城县| 瓦房店市| 闽清县| 泸水县| 安乡县|