|
1 | 1 | import { ImageSource } from "tns-core-modules/image-source"; |
2 | | -import { BarcodeFormat } from "./barcodescanning-common"; |
3 | | -import { MLKitCameraView as MLKitBarcodeScannerBase } from "../mlkit-cameraview"; |
4 | 2 | import { MLKitScanBarcodesOptions, MLKitScanBarcodesResult } from "./index"; |
5 | 3 | import { MLKitOptions } from "../index"; |
| 4 | +import { BarcodeFormat, MLKitBarcodeScanner as MLKitBarcodeScannerBase } from "./barcodescanning-common"; |
6 | 5 |
|
7 | 6 | export { BarcodeFormat }; |
8 | 7 |
|
9 | 8 | export class MLKitBarcodeScanner extends MLKitBarcodeScannerBase { |
10 | | - |
11 | 9 | protected createDetector(): any { |
12 | | - } |
13 | | - |
14 | | - protected createFailureListener(): any { |
| 10 | + let formats: Array<BarcodeFormat>; |
| 11 | + if (this.formats) { |
| 12 | + formats = []; |
| 13 | + const requestedFormats = this.formats.split(","); |
| 14 | + requestedFormats.forEach(format => formats.push(BarcodeFormat[format.trim().toUpperCase()])) |
| 15 | + } |
| 16 | + return getBarcodeDetector(formats); |
15 | 17 | } |
16 | 18 |
|
17 | 19 | protected createSuccessListener(): any { |
| 20 | + return (barcodes: NSArray<FIRVisionBarcode>, error: NSError) => { |
| 21 | + if (error !== null) { |
| 22 | + console.log(error.localizedDescription); |
| 23 | + |
| 24 | + } else if (barcodes !== null) { |
| 25 | + const result = <MLKitScanBarcodesResult>{ |
| 26 | + barcodes: [] |
| 27 | + }; |
| 28 | + |
| 29 | + for (let i = 0, l = barcodes.count; i < l; i++) { |
| 30 | + const barcode: FIRVisionBarcode = barcodes.objectAtIndex(i); |
| 31 | + result.barcodes.push({ |
| 32 | + value: barcode.rawValue, |
| 33 | + format: BarcodeFormat[barcode.format] |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + this.notify({ |
| 38 | + eventName: MLKitBarcodeScanner.scanResultEvent, |
| 39 | + object: this, |
| 40 | + value: result |
| 41 | + }); |
| 42 | + } |
| 43 | + } |
18 | 44 | } |
| 45 | +} |
19 | 46 |
|
20 | | - // public onLayout(left: number, top: number, right: number, bottom: number): void { |
21 | | - // super.onLayout(left, top, right, bottom); |
22 | | - // if (this._hasSupport && this.ios) { |
23 | | - // this._reader.previewLayer.frame = this.ios.layer.bounds; |
24 | | - // } |
25 | | - // } |
| 47 | +function getBarcodeDetector(formats?: Array<BarcodeFormat>): any { |
| 48 | + if (formats && formats.length > 0) { |
| 49 | + // TODO |
| 50 | + const barcodeDetector: FIRVisionBarcodeDetector = FIRVision.vision().barcodeDetector(); |
| 51 | + return barcodeDetector; |
| 52 | + // const firebaseVisionBarcodeDetectorOptions = |
| 53 | + // new com.google.firebase.ml.vision.barcode.FirebaseVisionBarcodeDetectorOptions.Builder() |
| 54 | + // .setBarcodeFormats(formats[0], formats) // the seconds argument is a varargs.. let's make it easy and just do it like this |
| 55 | + // .build(); |
| 56 | + // return com.google.firebase.ml.vision.FirebaseVision.getInstance().getVisionBarcodeDetector(firebaseVisionBarcodeDetectorOptions); |
| 57 | + } else { |
| 58 | + return FIRVision.vision().barcodeDetector(); |
| 59 | + } |
26 | 60 | } |
27 | 61 |
|
28 | 62 | export function scanBarcodes(options: MLKitScanBarcodesOptions): Promise<MLKitScanBarcodesResult> { |
29 | 63 | return new Promise((resolve, reject) => { |
30 | 64 | try { |
31 | | - const firVision: FIRVision = FIRVision.vision(); |
32 | | - // TODO pass in formats |
33 | | - const barcodeDetector: FIRVisionBarcodeDetector = firVision.barcodeDetector(); |
34 | | - // const textDetector: FIRVisionBarcodeDetector = firVision.barcodeDetectorWithOptions(); |
| 65 | + const barcodeDetector = getBarcodeDetector(options.formats); |
35 | 66 |
|
36 | 67 | barcodeDetector.detectInImageCompletion(getImage(options), (barcodes: NSArray<FIRVisionBarcode>, error: NSError) => { |
37 | 68 | if (error !== null) { |
|
0 commit comments