|
1 | 1 | import { ImageSource } from "tns-core-modules/image-source"; |
| 2 | +import { ios as iosUtils } from "tns-core-modules/utils/utils"; |
2 | 3 | import { MLKitScanBarcodesOnDeviceOptions, MLKitScanBarcodesOnDeviceResult } from "./index"; |
3 | 4 | import { MLKitVisionOptions } from "../index"; |
4 | 5 | import { BarcodeFormat, MLKitBarcodeScanner as MLKitBarcodeScannerBase } from "./barcodescanning-common"; |
@@ -44,11 +45,47 @@ export class MLKitBarcodeScanner extends MLKitBarcodeScannerBase { |
44 | 45 |
|
45 | 46 | for (let i = 0, l = barcodes.count; i < l; i++) { |
46 | 47 | 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 | + |
47 | 75 | result.barcodes.push({ |
48 | 76 | value: barcode.rawValue, |
49 | 77 | format: BarcodeFormat[barcode.format], |
50 | 78 | ios: barcode, |
51 | | - bounds: barcode.frame |
| 79 | + bounds: { |
| 80 | + origin: { |
| 81 | + x, |
| 82 | + y |
| 83 | + }, |
| 84 | + size: { |
| 85 | + width, |
| 86 | + height |
| 87 | + } |
| 88 | + } |
52 | 89 | }); |
53 | 90 | } |
54 | 91 |
|
|
0 commit comments