Hi everyone,
I’m working with the Camera2 API to capture still images and handle device rotation properly for JPEG orientation. I’m using an OrientationEventListener to track device rotation (in degrees: 0, 90, 180, 270) and mapping these to Surface.ROTATION_* constants for orientation correction.
My current approach for JPEG orientation is:
val rotationConstant =currentDeviceOrientation
val jpegOrientation = (sensorOrientation + ORIENTATIONS.get(rotationConstant) + 270) % 360
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, jpegOrientation)
where currentDeviceOrientation is
orientationEventListener = object : OrientationEventListener(requireContext()) {
override fun onOrientationChanged(orientation: Int) {
if (orientation != ORIENTATION_UNKNOWN) {
currentDeviceOrientation = (orientation + 45) / 90 * 90/*when (orientation) {
in 45..134 -> 90 // Landscape left
in 135..224 -> 180 // Upside down
in 225..314 -> 270 // Landscape right
else -> 0 // Portrait
}*/
}
Log.e(TAG, "onOrientationChanged:$orientation---- $currentDeviceOrientation")
}
}
orientationEventListener?.enable()
and my ORIENTATIONS mapping is:
ORIENTATIONS.append(Surface.ROTATION_0, 90)
ORIENTATIONS.append(Surface.ROTATION_90, 0)
ORIENTATIONS.append(Surface.ROTATION_180, 270)
ORIENTATIONS.append(Surface.ROTATION_270, 180)
Problem:
Works fine in portrait and landscape left.
When the device is rotated to landscape right (clockwise 90°), the captured image is upside down.
Tried variants adding/subtracting 90°, 270°, or 360°, but can’t get consistent results across all orientations.
Sensor orientation is consistently 90°.