/var/log/messages

Jul 16, 2015 - 1 minute read - Comments - android

画像リサイズ

Bitmap.createBitmap て始点の座標と幅、高さを渡す、が前提になっていないドキュメントを参考にしててハマルなど。

例えば中央から縦横 1024 なサイズのナニを取得するのであれば

int width = in_bitmap.getWidth();
int height = in_bitmap.getHeight();
Bitmap bitmap = Bitmap.createBitmap(in_bitmap, width / 2, height / 2, 
        1024, 1024, new Matrix(), true);

ではなくて

Bitmap bitmap = Bitmap.createBitmap(in_bitmap, width / 2 - 512, height / 2 - 512,
        1024, 1024, new Matrix(), true);

なんスけど、色々な意味で事前にチェックすることがあったりなかったり。

もう少し

Gallary から取得した画像を同様にリサイズする実装を書いたんですが手順てきに

  • 戻った Uri で ContentResolver から InputStream 取得
  • データは読みこまず (BitmapFactory.Options の inJustDecodeBounds を true に)
  • BitmapFactory.decodeStream して Options のみ入手
  • Options.outWidth および Options.outHeight の値チェック
  • 必要なら Options.inSampleSize に 2 のべき乗な値設定
  • 再度 decodeStream して Bitmap なオブジェクト入手
  • それを元に Bitmap.createBitmap でトリミング
  • 適当なテンポラリファイルを作って FileOutputStream を作る
  • Bitmap#compress で出力

て長い。

まてりあるでざいんなあいこん 助かった

comments powered by Disqus