【发布时间】:2011-08-03 23:26:56
【问题描述】:
我正在设置一个相机视图,它的顶部有一个图像视图,并使用矩阵变换在屏幕上移动图像。当我拍摄照片时,我希望能够以正确的位置和缩放比例将图像合成到照片上。照片只能是纵向的,如果是横向的,则会旋转。
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)
矩形:(359.8342,901.34326) w:654.6885 h:772.1968
图像 rect 的缩放比例似乎不够大,因为生成的合成图像中的字符离上/左太远且太小。
【问题讨论】:
标签: android matrix bitmap scale composite