Reputation: 2691
I am trying to achieve a custom size camera view (preview and capture) with Camera2 api. In particular a 4:3 ratio but landscape preview (width > height).
So far I am able to get correct TextureView size but the preview is squeezed vertically, or correct preview but TextureView size goes beyond required container size.
Tried setting a SCALER_CROP_REGION
with below code but no effect.
val sensorRect = cameraCharacteristics
.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE)
?: return@run
val sensorWidth = sensorRect.width()
val sensorToViewRatio = sensorWidth / preview.width.toFloat()
val sensorBottom = (preview.height * sensorViewRatio).roundToInt() + sensorRect.top
previewRequestBuilder.set(
CaptureRequest.SCALER_CROP_REGION,
Rect(sensorRect.left, sensorRect.top, sensorRect.right, sensorBottom)
)
Also tried textureView.setTransform(matrix)
with setRectToRect
and postScale
but no luck. That cropped the preview surface itself but the actual preview is still squeezed.
What is the correct and recommended way of achieving this kind of custom preview and capture size?
I am woking on an enhanced version of google's CameraView library.
The library CameraViewEx codebase that I am working on can be found here.
Below are the classes that might be relevant to achieve intended behaviour.
Upvotes: 1
Views: 2383
Reputation: 18117
SCALER_CROP_REGION will not change the aspect ratio of output. The main developer docs don't make it as clear as possible, but there are a few diagrams here that show how the cropping happens.
Image sensors are required to line up with the long side of the device, so a device held in landscape will easily display a landscape preview. So are you asking about a portrait UI layout with a landscape-aspect ratio View showing camera preview?
In general, you can't do that without either adding black bars to preview (in this case, on the left and right), or you crop and zoom (removing a large percentage of the field of view; about 45% for 4:3 aspect ratio).
The latter should be possible via TextureView's transform, but you have to make sure you start with the transform already set on the TextureView, or you have to take into account more transforms involving the relative orientations of the sensor, device, and UI.
Upvotes: 2