Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Camera 2, how to crop preview size

I'm working on Camera 2 API recently and my device has a 16:9 screen ratio but my camera sensor is 4:3. So all the preview size I got is 4:3. I' wondering is there any way I get a crop the size and only display the 16:9 part? I tried a long time and didn't find any help for camera 2.

My current camera code is similar to the camera 2 basic sample.

So how should I crop the preview and only display the 16:9 part on the texture view?

Thanks!!

like image 736
Solorchid Avatar asked Nov 10 '22 01:11

Solorchid


2 Answers

You could create a SurfaceTexture with your ratio 4:3 instead of 16:9.

You could use the custom view from that sample project or create your own with the new ConstraintLayout and setting the ratio of height from 16:9 of your width (or inverse).

Then when you set the size of your texture:

texture.setDefaultBufferSize(width, height)

You will get not problems streching, because your texture is the same ratio as your camera output.

Hope this helps

like image 61
Sulfkain Avatar answered Nov 14 '22 21:11

Sulfkain


I answered a similar question after having this same problem, and not being able to change the aspect ratio of the actual view to match the camera output (as Sulfkain's answer suggests).

Tl;dr: the answer lies in the configureTransform in Camera2BasicFragment. Although their function is mostly implemented for orientation, the scaling matrix can resolve scaling/aspect ratio issues with views that are the wrong shape for the camera output.

like image 37
Squimon Avatar answered Nov 14 '22 21:11

Squimon