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

Commit 0974c06

Browse files
Bounds property of barcode scan result not the same for iOS and Android #1288
1 parent bf6963d commit 0974c06

File tree

6 files changed

+46
-6
lines changed

6 files changed

+46
-6
lines changed

src/mlkit/barcodescanning/index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ export interface MLKitScanBarcodesResultBounds {
1717
export interface MLKitScanBarcodesResultBarcode {
1818
value: string;
1919
format: string;
20+
bounds?: MLKitScanBarcodesResultBounds;
2021
ios?: any;
2122
android?: any;
22-
bounds?: MLKitScanBarcodesResultBounds;
23-
// TODO details
2423
}
2524

2625
export interface MLKitScanBarcodesOnDeviceResult extends MLKitVisionResult {

src/mlkit/barcodescanning/index.ios.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ImageSource } from "tns-core-modules/image-source";
2+
import { ios as iosUtils } from "tns-core-modules/utils/utils";
23
import { MLKitScanBarcodesOnDeviceOptions, MLKitScanBarcodesOnDeviceResult } from "./index";
34
import { MLKitVisionOptions } from "../index";
45
import { BarcodeFormat, MLKitBarcodeScanner as MLKitBarcodeScannerBase } from "./barcodescanning-common";
@@ -44,11 +45,47 @@ export class MLKitBarcodeScanner extends MLKitBarcodeScannerBase {
4445

4546
for (let i = 0, l = barcodes.count; i < l; i++) {
4647
const barcode: FIRVisionBarcode = barcodes.objectAtIndex(i);
48+
const image: UIImage = this.lastVisionImage;
49+
50+
// the iOS image is rotated, so compensate for it when reporting these
51+
let { x, y } = barcode.frame.origin;
52+
let { width, height } = barcode.frame.size;
53+
54+
if (image) {
55+
const origX = x;
56+
const origWidth = width;
57+
58+
if (iosUtils.isLandscape()) {
59+
if (UIDevice.currentDevice.orientation === UIDeviceOrientation.LandscapeRight) {
60+
// the image is rotated 180 degrees
61+
x = image.size.width - (width + x);
62+
y = image.size.height - (height + y);
63+
}
64+
} else {
65+
// the image is rotated 90 degrees to the left
66+
x = image.size.height - (height + y);
67+
y = origX;
68+
width = height;
69+
height = origWidth;
70+
}
71+
72+
console.log("iosUtils.isLandscape(): " + iosUtils.isLandscape() + ", deviceOrientation: " + UIDevice.currentDevice.orientation);
73+
}
74+
4775
result.barcodes.push({
4876
value: barcode.rawValue,
4977
format: BarcodeFormat[barcode.format],
5078
ios: barcode,
51-
bounds: barcode.frame
79+
bounds: {
80+
origin: {
81+
x,
82+
y
83+
},
84+
size: {
85+
width,
86+
height
87+
}
88+
}
5289
});
5390
}
5491

src/mlkit/mlkit-cameraview-common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const pauseProperty = new Property<MLKitCameraView, boolean>({
2828
export abstract class MLKitCameraView extends ContentView {
2929
static scanResultEvent: string = "scanResult";
3030

31-
protected lastVisionImage;
31+
public lastVisionImage;
3232

3333
protected processEveryNthFrame: number;
3434
protected preferFrontCamera: boolean;

src/mlkit/mlkit-cameraview.android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
2626
private bytesToByteBuffer = new Map();
2727
private pendingFrameData = null;
2828
protected rotation;
29-
protected lastVisionImage;
29+
public lastVisionImage;
3030
private detector: any;
3131
private camera;
3232

src/mlkit/mlkit-cameraview.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { MLKitCameraView as MLKitCameraViewBase } from "./mlkit-cameraview-common";
22

33
export declare abstract class MLKitCameraView extends MLKitCameraViewBase {
4-
protected lastVisionImage;
4+
public lastVisionImage;
55

66
protected abstract createDetector(): any;
77

src/mlkit/mlkit-cameraview.ios.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
99
private previewLayer: AVCaptureVideoPreviewLayer;
1010
private cameraView: TNSMLKitCameraView;
1111

12+
public lastVisionImage;
13+
1214
disposeNativeView(): void {
1315
super.disposeNativeView();
1416
if (this.captureSession) {
@@ -217,6 +219,8 @@ class TNSMLKitCameraViewDelegateImpl extends NSObject implements TNSMLKitCameraV
217219
this.detectorBusy = false;
218220
};
219221

222+
this.owner.get().lastVisionImage = image;
223+
220224
if (this.detector.detectInImageCompletion) {
221225
this.detector.detectInImageCompletion(this.uiImageToFIRVisionImage(image), (result, error) => {
222226
this.onSuccessListener(result, error);

0 commit comments

Comments
 (0)