use early returns on MacOS; fix MacOS from only returning a single barcode in ea…
…ch result; update MacOS to use the new format for image data
Showing
2 changed files
with
44 additions
and
32 deletions
| @@ -80,7 +80,7 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> { | @@ -80,7 +80,7 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> { | ||
| 80 | /// | 80 | /// |
| 81 | /// If this is false, [BarcodeCapture.image] will always be null. | 81 | /// If this is false, [BarcodeCapture.image] will always be null. |
| 82 | /// | 82 | /// |
| 83 | - /// Defaults to false, and is only supported on iOS and Android. | 83 | + /// Defaults to false, and is only supported on iOS, MacOS and Android. |
| 84 | final bool returnImage; | 84 | final bool returnImage; |
| 85 | 85 | ||
| 86 | /// Whether the flashlight should be turned on when the camera is started. | 86 | /// Whether the flashlight should be turned on when the camera is started. |
| @@ -126,42 +126,54 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | @@ -126,42 +126,54 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | ||
| 126 | do { | 126 | do { |
| 127 | let barcodeRequest:VNDetectBarcodesRequest = VNDetectBarcodesRequest(completionHandler: { [weak self] (request, error) in | 127 | let barcodeRequest:VNDetectBarcodesRequest = VNDetectBarcodesRequest(completionHandler: { [weak self] (request, error) in |
| 128 | self?.imagesCurrentlyBeingProcessed = false | 128 | self?.imagesCurrentlyBeingProcessed = false |
| 129 | - if error == nil { | ||
| 130 | - if let results = request.results as? [VNBarcodeObservation] { | ||
| 131 | - for barcode in results { | ||
| 132 | - if self?.scanWindow != nil && cgImage != nil { | ||
| 133 | - let match = self?.isBarCodeInScanWindow(self!.scanWindow!, barcode, cgImage!) ?? false | ||
| 134 | - if (!match) { | ||
| 135 | - continue | ||
| 136 | - } | ||
| 137 | - } | ||
| 138 | - | ||
| 139 | - DispatchQueue.main.async { | ||
| 140 | - self?.sink?([ | ||
| 141 | - "name": "barcode", | ||
| 142 | - "data": [ | ||
| 143 | - [ | ||
| 144 | - "payload": barcode.payloadStringValue ?? "", | ||
| 145 | - "symbology": barcode.symbology.toInt ?? -1, | ||
| 146 | - ], | ||
| 147 | - ], | ||
| 148 | - ]) | ||
| 149 | - } | ||
| 150 | - // if barcodeType == "QR" { | ||
| 151 | - // let image = CIImage(image: source) | ||
| 152 | - // image?.cropping(to: barcode.boundingBox) | ||
| 153 | - // self.qrCodeDescriptor(qrCode: barcode, qrCodeImage: image!) | ||
| 154 | - // } | ||
| 155 | - } | ||
| 156 | - } | ||
| 157 | - } else { | 129 | + |
| 130 | + if error != nil { | ||
| 158 | DispatchQueue.main.async { | 131 | DispatchQueue.main.async { |
| 159 | self?.sink?(FlutterError(code: "MobileScanner", message: error?.localizedDescription, details: nil)) | 132 | self?.sink?(FlutterError(code: "MobileScanner", message: error?.localizedDescription, details: nil)) |
| 160 | } | 133 | } |
| 134 | + return | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + guard let results: [VNBarcodeObservation] = request.results as? [VNBarcodeObservation] else { | ||
| 138 | + return | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + if results.isEmpty { | ||
| 142 | + return | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + let barcodes: [VNBarcodeObservation] = results.compactMap({ barcode in | ||
| 146 | + // If there is a scan window, check if the barcode is within said scan window. | ||
| 147 | + if self?.scanWindow != nil && cgImage != nil && !(self?.isBarCodeInScanWindow(self!.scanWindow!, barcode, cgImage!) ?? false) { | ||
| 148 | + return nil | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + return barcode | ||
| 152 | + }) | ||
| 153 | + | ||
| 154 | + DispatchQueue.main.async { | ||
| 155 | + if (!MobileScannerPlugin.returnImage) { | ||
| 156 | + self?.sink?([ | ||
| 157 | + "name": "barcode", | ||
| 158 | + "data": barcodes.map({ $0.toMap() }), | ||
| 159 | + ]) | ||
| 160 | + return | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + self?.sink?([ | ||
| 164 | + "name": "barcode", | ||
| 165 | + "data": barcodes.map({ $0.toMap() }), | ||
| 166 | + "image": cgImage == nil ? nil : [ | ||
| 167 | + "bytes": FlutterStandardTypedData(bytes: cgImage!.jpegData(compressionQuality: 0.8)!), | ||
| 168 | + "width": cgImage!.width, | ||
| 169 | + "height": cgImage!.height, | ||
| 170 | + ], | ||
| 171 | + ]) | ||
| 161 | } | 172 | } |
| 162 | }) | 173 | }) |
| 163 | - if(self?.symbologies.isEmpty == false){ | ||
| 164 | - // add the symbologies the user wishes to support | 174 | + |
| 175 | + if self?.symbologies.isEmpty == false { | ||
| 176 | + // Add the symbologies the user wishes to support. | ||
| 165 | barcodeRequest.symbologies = self!.symbologies | 177 | barcodeRequest.symbologies = self!.symbologies |
| 166 | } | 178 | } |
| 167 | 179 |
-
Please register or login to post a comment