Android摄像头preVIEW映射到最后的画面 [英] Android camera preview mapping to final picture

查看:263
本文介绍了Android摄像头preVIEW映射到最后的画面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直在建立一个定制的Andr​​oid摄像头的活动,其中一个矩形 重叠里面的摄像头preVIEW和照片被捕获时(在 $ P $按钮PSS),我们要裁剪相应的矩形区域 最终图象的。问题是,我们还没有想出如何机器人 映射preVIEW屏幕到最终图像。

We have been building a custom Android camera Activity where a rectangle is overlayed inside the camera preview and when the photo is captured (at the press of a button), we want to crop the corresponding rectangle area of the final image. The problem is that we haven't yet figured out how android maps the preview screen to the final image.

我们已经尝试下面的公式,但仅适用于某些情况下:

We have tried the following formula but only works in some cases:

让x和y是矩形的左上角作为坐标 在相机preVIEW所示。

Let x and y be the coordinates of the top left corner of the rectangle as shown in the camera preview.

让preview_width和preview_height是摄像机的宽度和高度 preVIEW。这些都得到使用getBest previewSize()函数,它循环 通过支持preVIEW尺寸,并选择较大的一个。 让我们假设他们是1280和720。

Let preview_width and preview_height be the width and height of the camera preview. These are get using a getBestPreviewSize() function, which loops through the supported Preview sizes and selects the larger one. Let's suppose they are 1280 and 720.

让picture_width和picture_height是最终的宽度和高度 图片相机所支持。为了我们的目的,假设这是1632 * 1224(在索尼爱立信XPERIA这相当于200万像素)。

Let picture_width and picture_height be the width and height of the final picture as supported by the camera. For our purpose, suppose this is 1632 * 1224 (on a Sony Ericsson XPERIA this corresponds to the 2 MP).

我们想找到x_map,y_map它们的作物矩形的x和y 最终的图像。

We want to find x_map, y_map which are the x and y of the crop rectangle of the final image.

通过类比的规则。然后,我们使用的公式是:

Then by the rule of analogy, the formula we used was:

x_map = picture_width * X / preview_width; y_map = picture_height * Y / preview_height;

x_map = picture_width * x / preview_width; y_map = picture_height * y / preview_height;

不过,这似乎并不适用于所有情况。我们已发现,当 这款手机的屏幕尺寸类似preVIEW大小,那么这 公式几乎工程(大约与一个小错误)。

However, this doesn't seem to work in all cases. We have found out that when the phone's screen dimensions are similar to the preview size, then this formula almost works (approximately, with a small error).

不过,对于其他情况下,像索尼的Xperia手机,在屏幕尺寸:宽度:480 和高度:854,公式失败

However for other cases like the Sony XPERIA phone, where the screen dimensions are: width: 480 and height: 854, the formula fails.

例如,对于x = 50,则x_map = 1632 * 50/1280 = 63.75 然而这个数,如由矩形区域裁剪从最终 画面,并没有真正对应于preVIEW区域的矩形。它 是的路要走。我们的公式不考虑屏幕尺寸, 虽然实验与修改它要占他们,也没有 产生的任何期望的结果。

For example, for x=50, then x_map = 1632 * 50 / 1280 = 63.75 However this number, as shown by the rectangle area cropped from the final picture, doesn't really correspond to the rectangle in the preview area. It is way off. Our formula doesn't take into account the screen dimensions, though experimentation with modifying it to account for them, hasn't produced any desirable results.

我们怀疑preVIEW大小并以某种方式最终图像之间的映射 考虑到了屏幕的尺寸,但还没有想出到底如何, 因为有它,并在广泛寻找任何文件 相关网页没有回答上来。我们认为,有一个 我们需要考虑某些比例因子和高宽比 前申请类比的规则,对于公式正常工作。

We suspect that the mapping between the preview size and the final picture somehow takes into account the screen dimensions but haven't figured out how exactly, since there is no documentation on it and after extensively searching for answers on the web nothing relevant came up. We believe that there is a certain scale factor and aspect ratio that we need to take into account before applying the rule of analogy, for the formula to work fine.

你知道正确的公式上面?

Do you know a correct formula for the above?

推荐答案

首先,获得由 Camera.getSupportedPictureSizes 返回的最大画面尺寸 - 这是本机的分辨率您相机,从中可以判断原始宽高比。任何图片的大小或preVIEW大小不同的纵横比将根据'Matrix.ScaleToFit.CENTER'被裁剪。

First, get the largest picture size returned by Camera.getSupportedPictureSizes - this is the native resolution of your camera from which you can determine the native aspect ratio. Any picture size or preview size with a different aspect ratio will be cropped according to `Matrix.ScaleToFit.CENTER'.

另请参阅: http://stackoverflow.com/a/18159351/994834

这篇关于Android摄像头preVIEW映射到最后的画面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆