android圖片壓縮總結

jopen 8年前發布 | 9K 次閱讀 Android開發 移動開發

android圖片壓縮無非兩種,一種質量壓縮,一種像素壓縮,前者多用于圖片上傳時,后者多用于本地圖片展示縮略圖時。

對于質量壓縮,主要用到的一個方法就是:

public boolean compress(CompressFormat format, int quality, OutputStream stream) {}

這是Bitmap類里的一個方法,第一個參數表示圖片壓縮的格式,android中提供了以下格式:

public enum CompressFormat {
    JPEG    (0),
    PNG     (1),
    WEBP    (2);

    CompressFormat(int nativeInt) {
        this.nativeInt = nativeInt;
    }
    final int nativeInt;
}

第二個參數表示壓縮的質量,注意這個是壓縮的關鍵,它的取值是0到100,越小表示壓縮的越厲害,第三個參數表示把壓縮的數據寫入了outputstream流中。

OK,來看一個例子:

public byte [] compressBitmap(Bitmap bitmap,int max){
    int quality = 100;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,quality,byteArrayOutputStream);
    while (byteArrayOutputStream.toByteArray().length / 1024 > max){
        byteArrayOutputStream.reset();
        quality = quality -10;
        bitmap.compress(Bitmap.CompressFormat.JPEG,quality,byteArrayOutputStream);
    }
    return byteArrayOutputStream.toByteArray();
}

這個方法的第一個參數不必解釋,第二個參數表示你要求的壓縮后圖片最大可以是多少。最后可以拿到一個byte數組。我們有了這個byte數組就可以轉化為file或者bitmap。

注意這種質量壓縮后,像素本身沒有改變。

對于像素壓縮,顧名思義就是壓縮像素。這里用到的一個主要的方法就是:

public static Bitmap decodeFile(String pathName, Options opts) {
    Bitmap bm = null;
    InputStream stream = null;
    try {
        stream = new FileInputStream(pathName);
        bm = decodeStream(stream, null, opts);
    } catch (Exception e) {
        /*  do nothing.
            If the exception happened on open, bm will be null.
        */
        Log.e("BitmapFactory", "Unable to decode stream: " + e);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // do nothing here
            }
        }
    }
    return bm;
}

這是BitmapFactory中的一個靜態方法,第一個參數表示file的全路徑,第二個參數是關鍵,Options是BitmapFactory類中的一個靜態內部類,它有兩個非常重要的屬性:

/**
 * If set to true, the decoder will return null (no bitmap), but
 * the out... fields will still be set, allowing the caller to query
 * the bitmap without having to allocate the memory for its pixels.
 */
public boolean inJustDecodeBounds;

/**
 * If set to a value > 1, requests the decoder to subsample the original
 * image, returning a smaller image to save memory. The sample size is
 * the number of pixels in either dimension that correspond to a single
 * pixel in the decoded bitmap. For example, inSampleSize == 4 returns
 * an image that is 1/4 the width/height of the original, and 1/16 the
 * number of pixels. Any value <= 1 is treated the same as 1. Note: the
 * decoder uses a final value based on powers of 2, any other value will
 * be rounded down to the nearest power of 2.
 */
public int inSampleSize;

第一個屬性inJustDecodeBounds,如果設置為ture,則返回null。

第二個屬性inSampleSize表示縮放比例,大于1表示縮小了原來的多少,比如inSampleSize == 4,就表示縮小了原來的四分之一,如果小于1則和1相同。

好了來看看這個網上遍地都是的一個像素壓縮的方法:

private Bitmap getimage(String srcPath) {
    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    //開始讀入圖片,此時把options.inJustDecodeBounds 設回true了
    newOpts.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此時返回bm為空

    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;
    //現在主流手機比較多是800*480分辨率,所以高和寬我們設置為
    float hh = 800f;//這里設置高度為800f
    float ww = 480f;//這里設置寬度為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;//設置縮放比例
    //重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了
    bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
    return bitmap;
}

是不是就很好理解了。

來自: http://my.oschina.net/gef/blog/597278

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!