Bitmapの縮小|Android開発

      2016/11/08

AndroidでのBitmap縮小処理メモ。

import

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

サイズを取得してから decodeFile で直接リサイズする方式.

inSampleSizeは2のべき乗のみ有効のため、調整する。
2のべき乗以外は低い値と同じになる。(6指定→4扱い)
そのため、指定したサイズ通りに出力されない場合もある。

private Bitmap resizeRead(String filePath, int outputWidth, int outputHeight) {
    BitmapFactory.Options ops = new BitmapFactory.Options();
    ops.inPreferredConfig = Bitmap.Config.RGB_565; /* Bitmap.Config.ARGB_8888; */
    ops.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, ops);

    if (ops.outWidth < ops.outHeight) {
        int scaleWidth = calculateInSampleSize(ops.outWidth, outputHeight);
        int scaleHeight = calculateInSampleSize(ops.outHeight, outputWidth);
        ops.inSampleSize = Math.min(scaleWidth, scaleHeight);
    } else {
        int scaleWidth = calculateInSampleSize(ops.outWidth, outputWidth);
        int scaleHeight = calculateInSampleSize(ops.outHeight, outputHeight);
        ops.inSampleSize = Math.min(scaleWidth, scaleHeight);
    }
    ops.inJustDecodeBounds = false;

    Bitmap destBmp = BitmapFactory.decodeFile(filePath, ops);
    if (destBmp == null) {
        return destBmp;
    }
    if (Bitmap.Config.RGB_565 != destBmp.getConfig()) {
        destBmp.recycle();
        destBmp = null;
    }
    return destBmp;
}
private static int calculateInSampleSize(float src, float dest) {
    for (int i = 2; i < 256; i *= 2) {
        if (src / i <= dest) {
            return i;
        }
    }

    return 256;
}

Matrix でリサイズする方式.

private Bitmap resizeReadMatrix(String filePath, int outputWidth, int outputHeight) {
    BitmapFactory.Options ops = new BitmapFactory.Options();
    ops.inPreferredConfig = Bitmap.Config.RGB_565;
    Bitmap srcBmp = BitmapFactory.decodeFile(filePath, ops);
    try {
        if (Bitmap.Config.RGB_565 != srcBmp.getConfig()) {
            return null;
        }
        int height = srcBmp.getHeight();
        int width = srcBmp.getWidth();

        Matrix matrix = new Matrix();
        if (height > width) {
            float scaleWidth = outputHeight  / (float)width;
            float scaleHeight = outputWidth / (float)height;
            matrix.postScale(Math.min(scaleWidth, scaleHeight), Math.min(scaleWidth, scaleHeight));
        } else {
            float scaleWidth = outputWidth / (float)width;
            float scaleHeight = outputHeight / (float)height;
            matrix.postScale(Math.min(scaleWidth, scaleHeight), Math.min(scaleWidth, scaleHeight));
        }
        Bitmap destBmp = Bitmap.createBitmap(srcBmp, 0, 0, width, height, matrix, true);
        return destBmp;
    } finally {
        srcBmp.recycle();
        srcBmp = null;
    }
}

オマケ

Bitmap を Jpeg で保存

private static void writeJpeg(String path, Bitmap bitmap) {
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(path);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.close();
    } catch (Exception exp) {
        Log.e("MyDemo", "write jpeg failed.", exp);
    }
}

 - Android