1

I have implemented a Camera X preview which handle both portrait an landscape orientation. Basics google params are good for portrait mode, but when I'm rotating my device the preview in landscape mode is broken the same as this question (see pic)

          Preview preview = new Preview.Builder()
                                //.setTargetResolution(size)
                                //.setTargetAspectRatio(AspectRatio.RATIO_16_9)
                                .setTargetRotation(Surface.ROTATION_90)
                                .build();

I've tried all setter I found on SO and google camera doc like setTargetRotation and setTargetAspectRatio above, and also tried with matrix but nothing change in landscape.

How can I get more or less the same full screen preview in both orientation ?

nicover
  • 2,213
  • 10
  • 24

1 Answers1

0

Android camera preview transformation is a hard problem, that's why CameraX provides the PreviewView API for you. To make things simpler, you can also use the CameraController API which creates the Preview class for you.

You can take a look at CameraX's test app for code samples: https://github.com/androidx/androidx/blob/androidx-main/camera/integration-tests/viewtestapp/src/main/java/androidx/camera/integration/view/CameraControllerFragment.java

Xi 张熹
  • 10,492
  • 18
  • 58
  • 86
  • Could you provide a minimal sample code with the controller and the rotation handling for orientation preview ? I have already implemented all I found in API and doc but I'm not able make it work while the device is rotating – nicover Jan 24 '23 at 14:56
  • The minimal sample I have is probably the MLKit sample. You just need to remove the MLKit related code: https://github.com/android/camera-samples/blob/78270451d4b79ae7ec9ee42b4b45714b122663d4/CameraX-MLKit/app/src/main/java/com/example/camerax_mlkit/MainActivity.kt – Xi 张熹 Jan 28 '23 at 15:54