指定サイズで画像トリミング
AndroidのBitmap処理は
android.graphics パッケージの
BitmapFactoryクラスを使う
BitmapFactory.decodeStream()で
Bitmapを読み込み
BitmapFactory.Optionsのインスタンスに読み込み時の振る舞いを設定する
指定サイズでのトリミングをするには
一旦画像をBitmapとして読み込んで
その後でトリミング領域を設定
そして
Bitmap.createBitmap()で新しいBitmapを作成する
excelとかで、ファイルを開いて
編集したら、別ファイルで保存するようなかんじ
まず、Bitmap画像の読み込み
例外発生に備え、try catchで行う
InputStream input = null;
try{
//振る舞いの設定
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
//画像の読み込み
input = getContentResolver().openInputStream(imageUri);
BitmapFactory.decodeStream(input, null, options);
int inSampleSize = calculateInSampleSize(options, MAX_IMAGE_SIZE, MAX_IMAGE_SIZE);
input.close();
input = null;
options.inJustDecodeBounds= false;
options.inSampleSize = inSampleSize;
input = getContentResolver().openInputStream(imageUri);
mSourceBitmap = BitmapFactory.decodeStream(input, null, options);
mImageView.setImageBitmap(mSourceBitmap);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
if(input != null){
try{
input.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
次に、画像をトリミングしてからBitmapを作成
//幅と高さの取得 int imageWidth = mSourceBitmap.getWidth(); int imageHeight = mSourceBitmap.getHeight(); //トリミングする幅、高さ、座標の設定 int nWidth = (int)(imageWidth * 0.7f); int nHeight = (int)(imageHeight * 0.7f); int startX = (imageWidth - nWidth) /2; int startY = (imageHeight - nHeight)/2; //トリミングしたBitmapの作成 mSourceBitmap = Bitmap.createBitmap(mSourceBitmap, startX, startY, nWidth, nHeight, null, true);
Bitmap.createBitmap()の引数は
第一引数のmSourceBitmapがコピー元のBitmap
第2 の startX
第3 の startY
第4 の nWidth
第5 の nHeight
はトリミングの位置と大きさを指定している