0
我正在設置一個攝像機視圖,在其上方有一個圖像視圖,並使用矩陣變換在圖像周圍移動圖像。當我拍攝照片時,我希望能夠將圖像合成到照片上,並正確尊重位置和縮放比例。這張照片只有肖像,如果是風景,它就會旋轉。爲合成縮放矩陣變換圖像
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.6885 h:772.1968
圖像矩形似乎不會縮放得足夠大,因爲生成的合成圖像的字符太靠近頂部/左側和太小。
另一種選擇是使用'getScaledWidth()'來考慮屏幕dpi – Affian