Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android camera preview mapping to final picture

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:

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

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.

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).

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;

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).

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

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.

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?

like image 983
Fotis Hantzis Avatar asked Mar 22 '13 23:03

Fotis Hantzis


1 Answers

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'.

See also: https://stackoverflow.com/a/18159351/994834

like image 158
goto10 Avatar answered Oct 12 '22 02:10

goto10