1

I have a proprietary device with a rotated camera, that doesn't report its rotation to the OS.

We use CameraX with PreviewView, and I get the image 90 deg rotated, and mirrored.

Is there an efficient way to rotate the preview?

We tried:

  1. Setting the setTargetOrientation of camera, of analyzer, of Preview. None works.
  2. Rotating the PreviewView of course doesn't work, it just modifies the ViewPort because it won't rotate the surface.
  3. ViewFinder library - outputs bitmaps but doesn't connect to Preview (or does anybody have a solution for that?). Without setSurfaceProvider, nothing will empty the frames pipeline, and it will display nothing.

Ugly "Solution":

We currently use bitmap (via Analyzer), then we display the bitmap. That's ugly, and CPU expensive.

Is Camera2 the only way to rotate previews nicely? Camera2 does bitmaps anyway, but it may do the YUV conversion on hardware.

NOTE

Solutions like here don't work. Setting target orientation of the Preview only modifies the scale to compensate for aspect ratio. It doesn't actually rotate. Also that doesn't work because it's obsolete and not valid anymore.

UnDroider
  • 11
  • 2

1 Answers1

1

The Preview#setTargetRotation API should work: https://developer.android.com/reference/androidx/camera/core/Preview#setTargetRotation(int)

For this API to work, your PreviewView has to be in the COMPATIBLE mode:

PreviewView#setImplementationMode(ImplementationMode.COMPATIBLE);

You can find a complete sample on GitHub: https://github.com/androidx/androidx/blob/androidx-main/camera/integration-tests/viewtestapp/src/main/java/androidx/camera/integration/view/PreviewViewFragment.java

I uploaded a screen record of the preview rotating. Please take look and let me know if this is not your desired result: https://github.com/xizhang/public-files/blob/main/stackoverflow74798791/preview_rotation.mp4

You can also download the APK and test it yourself: https://github.com/xizhang/public-files/blob/main/stackoverflow74798791/camera-testapp-view-debug.apk

Xi 张熹
  • 10,492
  • 18
  • 58
  • 86
  • Ah, this is one of the items pointed by the links in the question :-( Didn't work. It changes the resolution, and that's it. – UnDroider Dec 15 '22 at 14:04
  • I uploaded screen record video and APK that demonstrates preview rotation. Please let me know if this is not your desired result. – Xi 张熹 Dec 15 '22 at 16:35