缩放矩阵变换图像以进行合成
我正在设置一个相机视图,它的顶部有一个图像视图,并使用矩阵变换在屏幕上移动图像。当我拍照时,我希望能够以正确的位置和缩放比例将图像合成到照片上。照片只能是纵向照片,如果是横向照片则进行旋转。
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
图像矩形似乎没有缩放足够大,因为生成的合成图像的字符离顶部/左侧太远并且太小。
I'm setting up a camera view where it has a image view over the top of it and uses matrix transforms to move the image around the screen. When I take the photo I want to be able to composite the image onto the photo with correct respect to location and scaling. The photo is only ever portrait and is rotated if it's landscape.
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);
}
Logs:
Preview:480x854 Photo:1536x2048
Scale:2.3981264
Matrix:(112.45,375.85) scale:(1.00,1.00)
Rect:(359.8342,901.34326) w:654.6885 h:772.1968
The image rect doesn't seem to be scaling large enough as the resulting composite image has the character too far towards top/left and too small.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 android 正在预先缩放角色图像,这会抛出我所有的缩放计算
通过将角色图像移动到drawable-nodpi文件夹中,我能够停止 android 缩放设备的图像,以便图像可以缩放以匹配相机视图。
The problem was that android was pre-scaling the character images which threw out all my scale calculations
By moving the character images into the
drawable-nodpi
folder I was able to stop android scaling the images for device so that the images can be scaled to match the camera views.