カメラ・ビューの上にイメージ・ビューを設定し、マトリックス変換を使用して画面上でイメージを移動します。私が写真を撮るとき、私は、位置とスケーリングを正しく尊重して画像を合成することができるようにしたいと考えています。写真はこれまでの肖像画であり、風景の場合は回転しています。合成用スケーリング・マトリクス変換画像
public void onPictureTaken(byte[] data, Camera camera) {
BitmapFactory.Options opt = new BitmapFactory.Options();
this.newPhoto = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
int photoWidth = newPhoto.getWidth() > newPhoto.getHeight()?newPhoto.getHeight():newPhoto.getWidth();
int photoHeight = newPhoto.getWidth() > newPhoto.getHeight()?newPhoto.getWidth():newPhoto.getHeight();
//Preview is fullscreen
Display display = getWindowManager().getDefaultDisplay();
int previewWidth = display.getWidth();
int previewHeight = display.getHeight();
float scale = (float)photoHeight/(float)previewHeight;
float scaleX = (float)photoWidth/(float)previewWidth;
Bitmap finished = Bitmap.createBitmap(photoWidth, photoHeight, newPhoto.getConfig());
//Create canvas and draw photo into it
//[snip]
//Draw character onto canvas
int imageResource = getResources().getIdentifier(character + "large", "drawable", getPackageName());
if (imageResource != 0) {
Bitmap character = BitmapFactory.decodeResource(this.getResources(), imageResource);
RectF rect = new RectF();
float[] values = new float[9];
matrix.getValues(values);
rect.left = values[Matrix.MTRANS_X] * scaleX;
rect.top = values[Matrix.MTRANS_Y] * scale;
//273x322 WidthxHeight of preview character image
rect.right = rect.left + (273 * values[Matrix.MSCALE_X]) * scale;
rect.bottom = rect.top + (322 * values[Matrix.MSCALE_Y]) * scale;
//Logging here
canvas.drawBitmap(character, null, rect, null);
character.recycle();
}
System.gc();
newPhoto = finished;
showDialog(DIALOG_PHOTO_RESULT);
}
ログ:
プレビュー:480x854写真:1536x2048
スケール:2.3981264
マトリックス:(112.45,375.85)規模:(1.00,1.00)
Rect:(359.8342,901.34326)w:654.6885h:772.1968
画像rectは、結果の合成画像の文字が上/左に向かなくなり過ぎたり、小さすぎたりするため、十分に拡大縮小されていないようです。
もう1つの方法は、スクリーンのdpiを考慮に入れて 'getScaledWidth()'を使うことです – Affian