With android.hardware.Camera
, in order to have the camera output appropriately track the device movement, we need to hook up an OrientationEventListener
to the setDisplayOrientation()
method on Camera
. That way, as the user twists the device around, we can keep what is "up" on the camera preview in sync with what is "up" in the real world. This is particularly true for rotating the device around the Y axis (i.e., flipping from landscape to reverse-landscape or back).
For most orientation changes, the android.hardware.camera2
API seems to "just work". However, for that Y-axis rotation (landscape -> reverse-landscape or vice versa), the camera preview winds up being inverted.
What is the equivalent of calling setDisplayOrientation()
on Camera
for the Camera2 API, so we can handle this scenario?
As you've noticed, this is supposed to "just work" for SurfaceView
on the camera2 API.
As such, there's no equivalent of setDisplayOrientation()
.
We haven't heard of this issue before now, so a few questions:
- Do you have your app locked to landscape, or are you allowing orientation changes?
- Do you handle your own orientation changes, or are you having the framework tear down and restart your app when orientation changes?
- Do you trigger any sort of re-layout when the 180-degree rotation occurs, for example to adjust where the shutter button is?
use the formula below to get device rotation.
private int getOrientation(int rotation) {
return (ORIENTATIONS.get(rotation) + mSensorOrientation + 270) % 360;
}
SensorOrientation is a field assigned from
characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION)
Solution suggested by google engineer: yaraki
https://github.com/googlesamples/android-Camera2Basic/issues/24
use this CaptureRequest JPEG_ORIENTATION to set your display orientation
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, 90)
There is none.
It does not "just work" if display orientation is swapped compared to sensor orientation (on most devices this is the case in portrait mode) because you have to set an aspect ratio < 1 for your preview surface that camera2 cannot map to a fitting output size. Solution:
- In the first API, you use camera.setDisplayOrientation()
- In camera2, I could not find a way to use a SurfaceHolder, but only a SurfaceTexture with surfaceTexture.setTransform(android.graphics.Matrix) call in portrait mode.