Solorchid
Solorchid

Reputation: 225

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!!

Upvotes: 6

Views: 5753

Answers (3)

Squimon
Squimon

Reputation: 581

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.

Upvotes: 1

gmetax
gmetax

Reputation: 3973

You have to do some debug on setUpCameraOutputs(int width, int height) and check the part that you are selecting the size of the output

// Danger, W.R.! Attempting to use too large a preview size could  exceed the camera
// bus' bandwidth limitation, resulting in gorgeous previews but the storage of
// garbage capture data.
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedPreviewWidth, 
rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, largest);

maybe you have to hard select something there.

Upvotes: 0

Sulfkain
Sulfkain

Reputation: 5119

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

Upvotes: 1

Related Questions