How to reconcile difference in aspect ratio between camera preview size and picture size

你说的曾经没有我的故事 提交于 2019-12-18 12:33:43

问题


I've got a camera app that selects a preview size based on characteristics of my layout (primarily aspect ratio) and then selects a picture size based on other constraints (minimum dimensions). This often results in a picture with a different aspect ratio than the preview.

This is not a problem in itself, but I need to map a rectangle on my preview to an equivalent rectangle on the picture so that I can extract the same area of the final photo that was visible in the preview when the photo was taken.

I understand that having different aspect ratios means that there will be portions of the rectangles that do not overlap, but I'm not working close enough to the edges that this should be an issue.

If it's not possible to know how the preview and photo map directly, is there perhaps a way to determine how they each map to the native camera? If I could programatically detect the native camera aspect ratio I might be able to determine the cropping scheme.


回答1:


After some additional trial and error, I've made the following tentative conclusions:

  1. The largest image size provided by Camera.Parameters.getSupportedPictureSizes represents the native resolution of the camera.

  2. The content of other picture sizes, as well as well as the preview sizes provided by Camera.Parameters.getSupportedPreviewSizes, will match the native content when scaled up as described for Matrix.ScaleToFit.CENTER. (basically scale from the center until either top/bottom or left/right hits the bounds of the native resolution. Cropping occurs in the dimension that doesn't reach the bounds of the native resolution if the aspect ratios don't match

So, given that, my solution was to first scale the preview selection rectangle to the native camera picture size. Then, now that I know which area of the native resolution contains the content I want, I can do a similar operation to then scale that rectangle on the native resolution to the smaller picture that was actually captured per Camera.Parameters.setPictureSize.




回答2:


I'm not too sure of what you're asking for, but the preview size actually is not determined by your layout. How it is displayed is determined by the layout. I'm assuming that you are using a SurfaceView to hold your preview/camera object.

You can get the actual preview size with this method http://developer.android.com/reference/android/hardware/Camera.Parameters.html#getPreviewSize()

And additionally, you can get the dimensions of your "displayed" preview by using the width and height you get from onSurfaceChanged() (It is part of the surfaceholder callback)

I'm not sure how important that is. But the main point is now that you know the actual preview size from getPreviewSize() you can do math and get your points accordingly.

This matches perfectly with onPreviewFrame(byte[] data, Camera camera) http://developer.android.com/reference/android/hardware/Camera.PreviewCallback.html

The byte[]data is in the YUV format, which you will probably need to convert to a bitmap before messing around with. But other than that it will have the same dimensions as what you get from calling getPreviewSize



来源:https://stackoverflow.com/questions/18132281/how-to-reconcile-difference-in-aspect-ratio-between-camera-preview-size-and-pict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!