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