Android 用于合成的缩放矩阵变换图像
我正在设置一个摄像头视图,上面有一个图像视图,并使用矩阵变换在屏幕上移动图像。当我拍摄照片时,我希望能够以正确的位置和比例将图像合成到照片上。照片是唯一的纵向照片,如果是横向照片,则会旋转Android 用于合成的缩放矩阵变换图像,android,matrix,bitmap,scale,composite,Android,Matrix,Bitmap,Scale,Composite,我正在设置一个摄像头视图,上面有一个图像视图,并使用矩阵变换在屏幕上移动图像。当我拍摄照片时,我希望能够以正确的位置和比例将图像合成到照片上。照片是唯一的纵向照片,如果是横向照片,则会旋转 public void onPictureTaken(byte[] data, Camera camera) { BitmapFactory.Options opt = new BitmapFactory.Options(); this.newPhoto = BitmapFactor
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.45375.85)比例:(1.00,1.00)
矩形:(359.8342901.34326)宽:654.6885高:772.1968
图像矩形的缩放比例似乎不够大,因为生成的合成图像中的角色离上/左太远,而且太小。问题是安卓预先缩放了角色图像,这导致了我所有的缩放计算
通过将角色图像移动到
drawable nodpi
文件夹中,我可以停止android缩放设备的图像,以便可以缩放图像以匹配相机视图。另一个选项是使用getScaledWidth()
将屏幕dpi考虑在内