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

Commit a9e84ac

Browse files
Merge pull request #1373 from alexander-mai/master
FIXED orientation bug on android
2 parents 69150a6 + 8f63a46 commit a9e84ac

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/mlkit/mlkit-cameraview.android.ts

Lines changed: 23 additions & 18 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 {
@@ -151,6 +153,9 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
151153
parameters.setPreviewSize(previewSize.width, previewSize.height);
152154
parameters.setPreviewFormat(android.graphics.ImageFormat.NV21);
153155

156+
application.off("orientationChanged");
157+
application.on("orientationChanged", () => { this.setRotation(this.camera, parameters, requestedCameraId ); });
158+
154159
this.setRotation(this.camera, parameters, requestedCameraId);
155160
this.fixStretch(previewSize.width, previewSize.height);
156161

@@ -171,12 +176,12 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
171176
const onFailureListener = this.createFailureListener();
172177

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

181186
let throttle = 0;
182187
this.camera.setPreviewCallbackWithBuffer(new android.hardware.Camera.PreviewCallback({
@@ -204,15 +209,15 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
204209
if (this.detector.processImage) {
205210
this.lastVisionImage = com.google.firebase.ml.vision.common.FirebaseVisionImage.fromByteBuffer(data, metadata);
206211
this.detector
207-
.processImage(this.lastVisionImage)
208-
.addOnSuccessListener(onSuccessListener)
209-
.addOnFailureListener(onFailureListener);
212+
.processImage(this.lastVisionImage)
213+
.addOnSuccessListener(onSuccessListener)
214+
.addOnFailureListener(onFailureListener);
210215
} else if (this.detector.detectInImage) {
211216
this.lastVisionImage = com.google.firebase.ml.vision.common.FirebaseVisionImage.fromByteBuffer(data, metadata);
212217
this.detector
213-
.detectInImage(this.lastVisionImage)
214-
.addOnSuccessListener(onSuccessListener)
215-
.addOnFailureListener(onFailureListener);
218+
.detectInImage(this.lastVisionImage)
219+
.addOnSuccessListener(onSuccessListener)
220+
.addOnFailureListener(onFailureListener);
216221
} else {
217222
this.runDetector(data, previewSize.width, previewSize.height);
218223
}

0 commit comments

Comments
 (0)