2013年3月15日金曜日

[Android TIPS] Bitmap.createBitmap ではなく Canvas を使って画像を回転する

SD カードに配置した画像 (img_src.jpg) をサイズ圧縮し、回転なし画像 (img_dst1.jpg) と
回転あり画像 (img_dst2.jpg) として保存するアプリを作ってみました。
Matrix + Bitmap.createBitmap で回転しようとすると Out of Memory で落ちてしまうので、
回転した Canvas に画像を描画することで実現してみました。

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dip" >

    <ImageView
        android:id="@+id/imgPreview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample.bitmaprotate"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.sample.bitmaprotate.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
MainActivity.java
package com.sample.bitmaprotate;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import android.os.Bundle;
import android.os.Environment;
import android.widget.ImageView;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // プレビュー用ウィジェット
    ImageView imgPreview = (ImageView) findViewById(R.id.imgPreview);

    // SD カード内の対象画像の読み込み (img_src.jpg)
    String path = Environment.getExternalStorageDirectory() + "/img_src.jpg";
    if (!new File(path).exists()) {
      Toast.makeText(this, "エラー: SDカードに img_src.jp を格納して再度実行してください。", Toast.LENGTH_LONG).show();
      return;
    }
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2; // 1/4のサイズで読み込み
    options.inPreferredConfig = Bitmap.Config.RGB_565; // 低クオリティでの読み込み
    Bitmap bmpSrc = BitmapFactory.decodeFile(path, options);
    // [圧縮済 回転'なし'] 画像を SD カード内に保存 (img_dst1.jpg)
    save(bmpSrc, "img_dst1.jpg");

    // 90度回転したキャンバスを用意
    Bitmap bmpDst = Bitmap.createBitmap(bmpSrc.getHeight(), bmpSrc.getWidth(), Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bmpDst);
    canvas.save();
    canvas.rotate(90, bmpSrc.getWidth() / 2, bmpSrc.getHeight() / 2);

    // 回転後の中心位置のずれを考慮して画像を描画
    float diff = bmpSrc.getWidth() / 2 - bmpSrc.getHeight() / 2;
    canvas.drawBitmap(bmpSrc, diff, diff, null);

    // 回転をもとに戻す → 結果的に描画した画像が回転することとなる
    canvas.restore();

    // プレビュー
    imgPreview.setImageBitmap(bmpDst);

    // [圧縮済 回転'あり'] 画像を SD カード内に保存 (img_dst2.jpg)
    save(bmpDst, "img_dst2.jpg");
  }

  /**
   * 画像の保存
   * 
   * @param bmp 対象画像
   * @param name ファイル名
   */
  private void save(Bitmap bmp, String name) {
    OutputStream out = null;
    String path = Environment.getExternalStorageDirectory() + "/" + name;

    try {
      File file = new File(path);
      if (file.createNewFile()) {
        out = new FileOutputStream(file);
        bmp.compress(CompressFormat.JPEG, 70, out);
      }

    } catch (Exception ex) {
      ex.printStackTrace();

    } finally {
      if (out != null) {
        try {
          out.close();
        } catch (Exception ex2) {}
      }
    }
  }
}
本当はオリジナル画像の解像度を落とさず無圧縮で回転したかったのですがその方法は分からず...
NDK を使わないといけなかったりするもんでしょうか?

0 件のコメント:

コメントを投稿