5

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

Puja
  • 192
  • 9
Solorchid
  • 215
  • 5
  • 18
  • Answer to this question is provided in the link [Android 5.0 Wrong crop regions on preview surface and captured still image](http://stackoverflow.com/questions/26583489/android-5-0-wrong-crop-regions-on-preview-surface-and-captured-still-image) – Chandana Jun 29 '16 at 08:19

3 Answers3

1

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

Sulfkain
  • 5,082
  • 1
  • 20
  • 38
1

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.

Squimon
  • 581
  • 5
  • 7
0

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.

gmetax
  • 3,853
  • 2
  • 31
  • 45