Skip to content

Commit 69b645f

Browse files
authored
fix(android): optimize cameraX rotation after camera is active (#14139)
* fix(android): optimize cameraX rotation after camera is active * update
1 parent 6fc9dcb commit 69b645f

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

android/modules/media/src/java/ti/modules/titanium/media/TiCameraXActivity.java

+44-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import android.os.Environment;
2020
import android.provider.MediaStore;
2121
import android.util.Size;
22+
import android.view.OrientationEventListener;
2223
import android.view.ScaleGestureDetector;
24+
import android.view.Surface;
2325
import android.view.View;
2426
import android.view.ViewGroup;
2527
import android.view.WindowManager;
@@ -112,6 +114,8 @@ public class TiCameraXActivity extends TiBaseActivity implements CameraXConfig.P
112114
private ProcessCameraProvider cameraProvider;
113115
static int targetResolutionWidth = -1;
114116
static int targetResolutionHeight = -1;
117+
private OrientationEventListener orientationEventListener;
118+
private int lastDisplayOrientation = 0;
115119

116120
public static void takePicture()
117121
{
@@ -417,6 +421,30 @@ private void startCamera()
417421
int rotation = getWindowManager().getDefaultDisplay().getRotation();
418422

419423
Activity activity = TiApplication.getAppCurrentActivity();
424+
425+
orientationEventListener = new OrientationEventListener(activity)
426+
{
427+
@Override
428+
public void onOrientationChanged(int orientation)
429+
{
430+
if (orientationEventListener == null || orientation == ORIENTATION_UNKNOWN) {
431+
return;
432+
}
433+
int rotation = getWindowManager().getDefaultDisplay().getRotation();
434+
if (lastDisplayOrientation != rotation) {
435+
imageCapture.setTargetRotation(rotation);
436+
lastDisplayOrientation = rotation;
437+
}
438+
439+
}
440+
};
441+
if (orientationEventListener.canDetectOrientation()) {
442+
orientationEventListener.enable();
443+
} else {
444+
orientationEventListener.disable();
445+
orientationEventListener = null;
446+
}
447+
420448
ListenableFuture cameraProviderFuture = ProcessCameraProvider.getInstance(activity);
421449
cameraProviderFuture.addListener(() -> {
422450
try {
@@ -493,8 +521,13 @@ private void startCamera()
493521
}
494522

495523
if (targetResolutionWidth != -1 && targetResolutionHeight != -1) {
496-
imageCaptureBuilder.setTargetResolution(
497-
new Size(targetResolutionWidth, targetResolutionHeight));
524+
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
525+
imageCaptureBuilder.setTargetResolution(
526+
new Size(targetResolutionWidth, targetResolutionHeight));
527+
} else {
528+
imageCaptureBuilder.setTargetResolution(
529+
new Size(targetResolutionHeight, targetResolutionWidth));
530+
}
498531
}
499532

500533
imageCapture = imageCaptureBuilder.build();
@@ -573,6 +606,11 @@ protected void onDestroy()
573606
if (camera != null) {
574607
camera = null;
575608
}
609+
if (orientationEventListener != null) {
610+
orientationEventListener.disable();
611+
orientationEventListener = null;
612+
}
613+
576614
// Destroy this activity.
577615
super.onDestroy();
578616
}
@@ -581,6 +619,10 @@ protected void onDestroy()
581619
public void finish()
582620
{
583621
overlayProxy = null;
622+
if (orientationEventListener != null) {
623+
orientationEventListener.disable();
624+
orientationEventListener = null;
625+
}
584626
super.finish();
585627
}
586628

0 commit comments

Comments
 (0)