Showing
2 changed files
with
29 additions
and
1 deletions
| @@ -50,7 +50,6 @@ class MobileScannerController { | @@ -50,7 +50,6 @@ class MobileScannerController { | ||
| 50 | final List<BarcodeFormat>? formats; | 50 | final List<BarcodeFormat>? formats; |
| 51 | 51 | ||
| 52 | /// can be used to limit the scan area to a portion of the screen | 52 | /// can be used to limit the scan area to a portion of the screen |
| 53 | - /// only for Android and IOS! | ||
| 54 | final Rect? scanWindow; | 53 | final Rect? scanWindow; |
| 55 | 54 | ||
| 56 | CameraFacing facing; | 55 | CameraFacing facing; |
| 1 | import AVFoundation | 1 | import AVFoundation |
| 2 | import FlutterMacOS | 2 | import FlutterMacOS |
| 3 | import Vision | 3 | import Vision |
| 4 | +import UIKit | ||
| 4 | 5 | ||
| 5 | public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, FlutterTexture, AVCaptureVideoDataOutputSampleBufferDelegate { | 6 | public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, FlutterTexture, AVCaptureVideoDataOutputSampleBufferDelegate { |
| 6 | 7 | ||
| @@ -21,6 +22,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | @@ -21,6 +22,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | ||
| 21 | // Image to be sent to the texture | 22 | // Image to be sent to the texture |
| 22 | var latestBuffer: CVImageBuffer! | 23 | var latestBuffer: CVImageBuffer! |
| 23 | 24 | ||
| 25 | + // optional window to limit scan search | ||
| 26 | + var scanWindow: CGRect? | ||
| 27 | + | ||
| 24 | 28 | ||
| 25 | // var analyzeMode: Int = 0 | 29 | // var analyzeMode: Int = 0 |
| 26 | var analyzing: Bool = false | 30 | var analyzing: Bool = false |
| @@ -110,6 +114,14 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | @@ -110,6 +114,14 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | ||
| 110 | if error == nil { | 114 | if error == nil { |
| 111 | if let results = request.results as? [VNBarcodeObservation] { | 115 | if let results = request.results as? [VNBarcodeObservation] { |
| 112 | for barcode in results { | 116 | for barcode in results { |
| 117 | + if scanWindow != nil { | ||
| 118 | + let boundingBox = barcode.frame | ||
| 119 | + if !scanWindow!.contains(boundingBox) { | ||
| 120 | + continue | ||
| 121 | + } | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + | ||
| 113 | let barcodeType = String(barcode.symbology.rawValue).replacingOccurrences(of: "VNBarcodeSymbology", with: "") | 125 | let barcodeType = String(barcode.symbology.rawValue).replacingOccurrences(of: "VNBarcodeSymbology", with: "") |
| 114 | let event: [String: Any?] = ["name": "barcodeMac", "data" : ["payload": barcode.payloadStringValue, "symbology": barcodeType]] | 126 | let event: [String: Any?] = ["name": "barcodeMac", "data" : ["payload": barcode.payloadStringValue, "symbology": barcodeType]] |
| 115 | self.sink?(event) | 127 | self.sink?(event) |
| @@ -175,6 +187,19 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | @@ -175,6 +187,19 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | ||
| 175 | let torch: Bool = argReader.bool(key: "torch") ?? false | 187 | let torch: Bool = argReader.bool(key: "torch") ?? false |
| 176 | let facing: Int = argReader.int(key: "facing") ?? 1 | 188 | let facing: Int = argReader.int(key: "facing") ?? 1 |
| 177 | 189 | ||
| 190 | + let scanWindowData: Array? = argReader.floatArray(key: "scanWindow") | ||
| 191 | + | ||
| 192 | + if(scanWindowData != nil) { | ||
| 193 | + | ||
| 194 | + let minX = scanWindowData![0] | ||
| 195 | + let minY = scanWindowData![1] | ||
| 196 | + | ||
| 197 | + let width = scanWindowData![2] - minX | ||
| 198 | + let height = scanWindowData![3] - minY | ||
| 199 | + | ||
| 200 | + scanWindow = CGRect(x: minX, y: minY, width: width, height: height) | ||
| 201 | + } | ||
| 202 | + | ||
| 178 | // Set the camera to use | 203 | // Set the camera to use |
| 179 | position = facing == 0 ? AVCaptureDevice.Position.front : .back | 204 | position = facing == 0 ? AVCaptureDevice.Position.front : .back |
| 180 | 205 | ||
| @@ -322,4 +347,8 @@ class MapArgumentReader { | @@ -322,4 +347,8 @@ class MapArgumentReader { | ||
| 322 | return args?[key] as? [String] | 347 | return args?[key] as? [String] |
| 323 | } | 348 | } |
| 324 | 349 | ||
| 350 | + func floatArray(key: String) -> [CGFloat]? { | ||
| 351 | + return args?[key] as? [CGFloat] | ||
| 352 | + } | ||
| 353 | + | ||
| 325 | } | 354 | } |
-
Please register or login to post a comment