Skip to content Skip to sidebar Skip to footer

How To Reconcile Difference In Aspect Ratio Between Camera Preview Size And Picture Size

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

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

Solution 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

Post a Comment for "How To Reconcile Difference In Aspect Ratio Between Camera Preview Size And Picture Size"