Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 8f63a46

Browse files
author
alexander.mai
committed
FIXED orientation bug on android
* #1341
1 parent 4c60759 commit 8f63a46

1 file changed

Lines changed: 24 additions & 19 deletions

File tree

src/mlkit/mlkit-cameraview.android.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
3535
this.surfaceView = null;
3636

3737
if (this.camera != null) {
38+
application.off("orientationChanged");
39+
3840
this.camera.stopPreview();
3941
this.camera.setPreviewCallbackWithBuffer(null);
4042
try {
@@ -83,9 +85,9 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
8385

8486
// invoke the permission dialog
8587
ActivityCompatClass.requestPermissions(
86-
application.android.foregroundActivity || application.android.startActivity,
87-
[android.Manifest.permission.CAMERA],
88-
CAMERA_PERMISSION_REQUEST_CODE);
88+
application.android.foregroundActivity || application.android.startActivity,
89+
[android.Manifest.permission.CAMERA],
90+
CAMERA_PERMISSION_REQUEST_CODE);
8991
}
9092
} else {
9193
console.log("There's no Camera on this device :(");
@@ -100,9 +102,9 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
100102

101103
private hasCamera(): boolean {
102104
return !!utils.ad
103-
.getApplicationContext()
104-
.getPackageManager()
105-
.hasSystemFeature("android.hardware.camera");
105+
.getApplicationContext()
106+
.getPackageManager()
107+
.hasSystemFeature("android.hardware.camera");
106108
}
107109

108110
private initView(nativeView): void {
@@ -154,6 +156,9 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
154156
parameters.setPreviewSize(previewSize.width, previewSize.height);
155157
parameters.setPreviewFormat(android.graphics.ImageFormat.NV21);
156158

159+
application.off("orientationChanged");
160+
application.on("orientationChanged", () => { this.setRotation(this.camera, parameters, requestedCameraId ); });
161+
157162
this.setRotation(this.camera, parameters, requestedCameraId);
158163

159164
if (parameters.getSupportedFocusModes().contains(android.hardware.Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
@@ -173,12 +178,12 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
173178
const onFailureListener = this.createFailureListener();
174179

175180
let metadata =
176-
new com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata.Builder()
177-
.setFormat(com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
178-
.setWidth(previewSize.width)
179-
.setHeight(previewSize.height)
180-
.setRotation(this.rotation)
181-
.build();
181+
new com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata.Builder()
182+
.setFormat(com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
183+
.setWidth(previewSize.width)
184+
.setHeight(previewSize.height)
185+
.setRotation(this.rotation)
186+
.build();
182187

183188
let throttle = 0;
184189
this.camera.setPreviewCallbackWithBuffer(new android.hardware.Camera.PreviewCallback({
@@ -206,15 +211,15 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
206211
if (this.detector.processImage) {
207212
this.lastVisionImage = com.google.firebase.ml.vision.common.FirebaseVisionImage.fromByteBuffer(data, metadata);
208213
this.detector
209-
.processImage(this.lastVisionImage)
210-
.addOnSuccessListener(onSuccessListener)
211-
.addOnFailureListener(onFailureListener);
214+
.processImage(this.lastVisionImage)
215+
.addOnSuccessListener(onSuccessListener)
216+
.addOnFailureListener(onFailureListener);
212217
} else if (this.detector.detectInImage) {
213218
this.lastVisionImage = com.google.firebase.ml.vision.common.FirebaseVisionImage.fromByteBuffer(data, metadata);
214219
this.detector
215-
.detectInImage(this.lastVisionImage)
216-
.addOnSuccessListener(onSuccessListener)
217-
.addOnFailureListener(onFailureListener);
220+
.detectInImage(this.lastVisionImage)
221+
.addOnSuccessListener(onSuccessListener)
222+
.addOnFailureListener(onFailureListener);
218223
} else {
219224
this.runDetector(data, previewSize.width, previewSize.height);
220225
}
@@ -250,7 +255,7 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
250255
if (this.camera != null) {
251256
this.camera.stopPreview();
252257
}
253-
};
258+
}
254259

255260
protected resumeScanning(): void {
256261
this.runCamera();

0 commit comments

Comments
 (0)