画像変換を行うには?|Android開発

Androidでの画像変換系処理のメモ。

Bitmap を JPEG で保存

private void saveBitmapToJpeg(Bitmap bmp) {
    String filename = "image001";
    String filePath = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
                        + String.format("/DCIM/%s.jpg", filename);
    java.io.ByteArrayOutputStream ostream = new java.io.ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, ostream);

    java.io.FileOutputStream fos = new java.io.FileOutputStream(filePath);
    fos.write(ostream.toByteArray());
    fos.close();
}

Bitmap から RAW データ取得

RGB565形式の取得。

private byte[] bmpToRGB565RAW(Bitmap srcBmp) {
    byte[] rgb565Bytes = null;

    width = srcBmp.getWidth();
    height = srcBmp.getHeight();

    ByteBuffer buff = ByteBuffer.allocate(width * height * 2);
    srcBmp.copyPixelsToBuffer(buff);
    rgb565Bytes = buff.array();

    buff.clear();
    return rgb565Bytes;
}

Canvas を利用した Bitmap の拡大・縮小

private Bitmap changeBitmapSize(Bitmap srcBmp) {
    // 320x240に縮小.
    Bitmap destBmp = Bitmap.createBitmap(320, 240, Bitmap.Config.RGB_565);
    android.graphics.Rect srcRect = new android.graphics.Rect(0, 0, srcBmp.getWidth(), srcBmp.getHeight());
    android.graphics.Rect destRect = new android.graphics.Rect(0, 0, destBmp.getWidth(), destBmp.getHeight());
    android.graphics.Canvas canvas = new android.graphics.Canvas(destBmp);
    canvas.drawBitmap(srcBmp, srcRect, destRect, null);
    return destBmp;
}

RAW データから Bitmap 作成

RGB565形式からの作成。

private Bitmap rgb565RAWToBitmap(byte[] rgb565Bytes) {
    Bitmap destBmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    destBmp.copyPixelsFromBuffer(ByteBuffer.wrap(rgb565Bytes));
    return destBmp;
}
このエントリーをはてなブックマークに追加
にほんブログ村 IT技術ブログへ

スポンサードリンク

関連コンテンツ

コメント

メールアドレスが公開されることはありません。 が付いている欄は必須項目です