androidrotationorientationcamera2

Camera2 front camera preview rotation


i am working on camera2 and have got a problem with rotation. In some devices, when switching to front camera, preview is being rotated 180 degrees. Was looking such a "setDefaultOrientation()" method for camera2 but couldn't find it.

Thanks


Solution

  • With a GLSurfaceView, you're presumably using a SurfaceTexture to map the camera preview stream into a GL texture, and then rendering it.

    The image data in the texture is not automatically rotated to be 'upright' - for one, there's no way for the OS to know how you're going to render your texture.

    However, the SurfaceTexture has a getTransformMatrix call, which when connected to the camera2 API, will give you the necessary transform from the camera's orientation to the device's native orientation (typically portrait for phones, landscape for tablets, but not always). You'll then have to add the transform from the native orientation to your app's orientation to this matrix, and then apply the combined matrix transform to adjust the preview image.