Skip to content

Commit

Permalink
fix(android): optimize cameraX rotation after camera is active (#14139)
Browse files Browse the repository at this point in the history
* fix(android): optimize cameraX rotation after camera is active

* update
  • Loading branch information
m1ga authored Nov 29, 2024
1 parent 6fc9dcb commit 69b645f
Showing 1 changed file with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -573,6 +606,11 @@ protected void onDestroy()
if (camera != null) {
camera = null;
}
if (orientationEventListener != null) {
orientationEventListener.disable();
orientationEventListener = null;
}

// Destroy this activity.
super.onDestroy();
}
Expand All @@ -581,6 +619,10 @@ protected void onDestroy()
public void finish()
{
overlayProxy = null;
if (orientationEventListener != null) {
orientationEventListener.disable();
orientationEventListener = null;
}
super.finish();
}

Expand Down

0 comments on commit 69b645f

Please sign in to comment.