Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 圖片壓縮的兩種可靠思路:質量壓縮與按比例縮放

編輯:關於 Android 程式設計

在 Android 開發裡,圖片往往是最佔用記憶體的資源。無論是相機拍攝還是從網路下載,原始圖片尺寸龐大,直接載入容易造成 OOM。實務上常見的處理方式有兩大類:一類是「質量壓縮」,在不改變像素尺寸的情況下降低 JPEG 編碼品質;另一類是「按比例縮放」,透過 inSampleSize 直接縮小解碼後的位圖尺寸。下面分別說明。

一、質量壓縮

核心思路是迴圈呼叫 compress(),每次降低 quality 參數,直到檔案大小符合目標(例如 100KB)為止。

private Bitmap compressImage(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 100 表示不壓縮
    int options = 100;
    while (baos.toByteArray().length / 1024 > 100) { // 大於 100kb 就繼續壓
        baos.reset();       // 清空緩衝區
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);
        options -= 10;      // 每次遞減 10
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
    return bitmap;
}

二、依螢幕比例縮放(以檔案路徑為輸入)

關鍵在於先用 inJustDecodeBounds = true 只讀取圖片的寬高資訊而不真正分配像素記憶體,計算出縮放比例後再正式解碼。

private Bitmap getimage(String srcPath) {
    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    newOpts.inJustDecodeBounds = true;   // 只讀邊界,不載入像素
    Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);// 此時 bitmap 為空
    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;

    float hh = 800f; // 目標高度
    float ww = 480f; // 目標寬度
    int be = 1;      // be=1 表示不縮放
    if (w > h && w > ww) {
        be = (int) (newOpts.outWidth / ww);
    } else if (w < h && h > hh) {
        be = (int) (newOpts.outHeight / hh);
    }
    if (be <= 0) be = 1;
    newOpts.inSampleSize = be;           // 設定縮放比例
    bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
    return compressImage(bitmap);        // 縮放後再做一次質量壓縮
}

三、以 Bitmap 為輸入的壓縮

這個版本先判斷位圖是否超過 1MB,若是則先降質避免後續 decodeStream 時記憶體溢出,接著同樣透過邊界讀取計算比例並縮放。

private Bitmap comp(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    if (baos.toByteArray().length / 1024 > 1024) { // 大於 1M 先降質防爆
        baos.reset();
        image.compress(Bitmap.CompressFormat.JPEG, 50, baos);
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    newOpts.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;

    float hh = 800f;
    float ww = 480f;
    int be = 1;
    if (w > h && w > ww) {
        be = (int) (newOpts.outWidth / ww);
    } else if (w < h && h > hh) {
        be = (int) (newOpts.outHeight / hh);
    }
    if (be <= 0) be = 1;
    newOpts.inSampleSize = be;
    isBm = new ByteArrayInputStream(baos.toByteArray());
    bitmap = BitmapFactory.decodeStream(isBm, null, newOpts);
    return compressImage(bitmap);
}

小提醒:質量壓縮只對 JPEG 有效(PNG 無損),而真正能大幅降低記憶體佔用的是縮放解碼尺寸。兩者搭配使用,才能在畫質與效能之間取得平衡。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved