diff --git a/android/modules/media/src/java/ti/modules/titanium/media/TiCameraXActivity.java b/android/modules/media/src/java/ti/modules/titanium/media/TiCameraXActivity.java index 10e0d13224e..16b7f20e74b 100644 --- a/android/modules/media/src/java/ti/modules/titanium/media/TiCameraXActivity.java +++ b/android/modules/media/src/java/ti/modules/titanium/media/TiCameraXActivity.java @@ -19,7 +19,9 @@ import android.os.Environment; import android.provider.MediaStore; import android.util.Size; +import android.view.OrientationEventListener; import android.view.ScaleGestureDetector; +import android.view.Surface; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; @@ -112,6 +114,8 @@ public class TiCameraXActivity extends TiBaseActivity implements CameraXConfig.P private ProcessCameraProvider cameraProvider; static int targetResolutionWidth = -1; static int targetResolutionHeight = -1; + private OrientationEventListener orientationEventListener; + private int lastDisplayOrientation = 0; public static void takePicture() { @@ -417,6 +421,30 @@ private void startCamera() int rotation = getWindowManager().getDefaultDisplay().getRotation(); Activity activity = TiApplication.getAppCurrentActivity(); + + orientationEventListener = new OrientationEventListener(activity) + { + @Override + public void onOrientationChanged(int orientation) + { + if (orientationEventListener == null || orientation == ORIENTATION_UNKNOWN) { + return; + } + int rotation = getWindowManager().getDefaultDisplay().getRotation(); + if (lastDisplayOrientation != rotation) { + imageCapture.setTargetRotation(rotation); + lastDisplayOrientation = rotation; + } + + } + }; + if (orientationEventListener.canDetectOrientation()) { + orientationEventListener.enable(); + } else { + orientationEventListener.disable(); + orientationEventListener = null; + } + ListenableFuture cameraProviderFuture = ProcessCameraProvider.getInstance(activity); cameraProviderFuture.addListener(() -> { try { @@ -493,8 +521,13 @@ private void startCamera() } if (targetResolutionWidth != -1 && targetResolutionHeight != -1) { - imageCaptureBuilder.setTargetResolution( - new Size(targetResolutionWidth, targetResolutionHeight)); + if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) { + imageCaptureBuilder.setTargetResolution( + new Size(targetResolutionWidth, targetResolutionHeight)); + } else { + imageCaptureBuilder.setTargetResolution( + new Size(targetResolutionHeight, targetResolutionWidth)); + } } imageCapture = imageCaptureBuilder.build(); @@ -573,6 +606,11 @@ protected void onDestroy() if (camera != null) { camera = null; } + if (orientationEventListener != null) { + orientationEventListener.disable(); + orientationEventListener = null; + } + // Destroy this activity. super.onDestroy(); } @@ -581,6 +619,10 @@ protected void onDestroy() public void finish() { overlayProxy = null; + if (orientationEventListener != null) { + orientationEventListener.disable(); + orientationEventListener = null; + } super.finish(); }