Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): optimize cameraX rotation after camera is active #14139

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading