Navaron Bracke

port scan timeout to iOS

@@ -55,6 +55,12 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -55,6 +55,12 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
55 55
56 var standardZoomFactor: CGFloat = 1 56 var standardZoomFactor: CGFloat = 1
57 57
  58 + private var nextScanTime = 0.0
  59 +
  60 + private var imagesCurrentlyBeingProcessed = 0
  61 +
  62 + public var timeoutSeconds: Double = 0
  63 +
58 init(registry: FlutterTextureRegistry?, mobileScannerCallback: @escaping MobileScannerCallback, torchModeChangeCallback: @escaping TorchModeChangeCallback, zoomScaleChangeCallback: @escaping ZoomScaleChangeCallback) { 64 init(registry: FlutterTextureRegistry?, mobileScannerCallback: @escaping MobileScannerCallback, torchModeChangeCallback: @escaping TorchModeChangeCallback, zoomScaleChangeCallback: @escaping ZoomScaleChangeCallback) {
59 self.registry = registry 65 self.registry = registry
60 self.mobileScannerCallback = mobileScannerCallback 66 self.mobileScannerCallback = mobileScannerCallback
@@ -89,8 +95,15 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -89,8 +95,15 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
89 } 95 }
90 latestBuffer = imageBuffer 96 latestBuffer = imageBuffer
91 registry?.textureFrameAvailable(textureId) 97 registry?.textureFrameAvailable(textureId)
92 - if ((detectionSpeed == DetectionSpeed.normal || detectionSpeed == DetectionSpeed.noDuplicates) && i > 10 || detectionSpeed == DetectionSpeed.unrestricted) {  
93 - i = 0 98 +
  99 + let currentTime = Date().timeIntervalSince1970
  100 + let eligibleForScan = currentTime > nextScanTime && imagesCurrentlyBeingProcessed == 0
  101 +
  102 + if ((detectionSpeed == DetectionSpeed.normal || detectionSpeed == DetectionSpeed.noDuplicates) && eligibleForScan || detectionSpeed == DetectionSpeed.unrestricted) {
  103 +
  104 + nextScanTime = currentTime + timeoutSeconds
  105 + imagesCurrentlyBeingProcessed += 1
  106 +
94 let ciImage = latestBuffer.image 107 let ciImage = latestBuffer.image
95 108
96 let image = VisionImage(image: ciImage) 109 let image = VisionImage(image: ciImage)
@@ -101,6 +114,8 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -101,6 +114,8 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
101 ) 114 )
102 115
103 scanner.process(image) { [self] barcodes, error in 116 scanner.process(image) { [self] barcodes, error in
  117 + imagesCurrentlyBeingProcessed -= 1
  118 +
104 if (detectionSpeed == DetectionSpeed.noDuplicates) { 119 if (detectionSpeed == DetectionSpeed.noDuplicates) {
105 let newScannedBarcodes = barcodes?.map { barcode in 120 let newScannedBarcodes = barcodes?.map { barcode in
106 return barcode.rawValue 121 return barcode.rawValue
@@ -114,8 +129,6 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -114,8 +129,6 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
114 129
115 mobileScannerCallback(barcodes, error, ciImage) 130 mobileScannerCallback(barcodes, error, ciImage)
116 } 131 }
117 - } else {  
118 - i+=1  
119 } 132 }
120 } 133 }
121 134
@@ -301,7 +314,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -301,7 +314,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
301 314
302 do { 315 do {
303 try device.lockForConfiguration() 316 try device.lockForConfiguration()
304 - var maxZoomFactor = device.activeFormat.videoMaxZoomFactor 317 + let maxZoomFactor = device.activeFormat.videoMaxZoomFactor
305 318
306 var actualScale = (scale * 4) + 1 319 var actualScale = (scale * 4) + 1
307 320
@@ -348,8 +361,6 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -348,8 +361,6 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
348 scanner.process(image, completion: callback) 361 scanner.process(image, completion: callback)
349 } 362 }
350 363
351 - var i = 0  
352 -  
353 var barcodesString: Array<String?>? 364 var barcodesString: Array<String?>?
354 365
355 366
@@ -12,6 +12,7 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { @@ -12,6 +12,7 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
12 /// The handler sends all information via an event channel back to Flutter 12 /// The handler sends all information via an event channel back to Flutter
13 private let barcodeHandler: BarcodeHandler 13 private let barcodeHandler: BarcodeHandler
14 14
  15 + /// The points for the scan window.
15 static var scanWindow: [CGFloat]? 16 static var scanWindow: [CGFloat]?
16 17
17 private static func isBarcodeInScanWindow(barcode: Barcode, imageSize: CGSize) -> Bool { 18 private static func isBarcodeInScanWindow(barcode: Barcode, imageSize: CGSize) -> Bool {
@@ -103,6 +104,8 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { @@ -103,6 +104,8 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
103 let formats: Array<Int> = (call.arguments as! Dictionary<String, Any?>)["formats"] as? Array ?? [] 104 let formats: Array<Int> = (call.arguments as! Dictionary<String, Any?>)["formats"] as? Array ?? []
104 let returnImage: Bool = (call.arguments as! Dictionary<String, Any?>)["returnImage"] as? Bool ?? false 105 let returnImage: Bool = (call.arguments as! Dictionary<String, Any?>)["returnImage"] as? Bool ?? false
105 let speed: Int = (call.arguments as! Dictionary<String, Any?>)["speed"] as? Int ?? 0 106 let speed: Int = (call.arguments as! Dictionary<String, Any?>)["speed"] as? Int ?? 0
  107 + let timeoutMs: Int = (call.arguments as! Dictionary<String, Any?>)["timeout"] as? Int ?? 0
  108 + self.mobileScanner.timeoutSeconds = Double(timeoutMs / 1000)
106 109
107 let formatList = formats.map { format in return BarcodeFormat(rawValue: format)} 110 let formatList = formats.map { format in return BarcodeFormat(rawValue: format)}
108 var barcodeOptions: BarcodeScannerOptions? = nil 111 var barcodeOptions: BarcodeScannerOptions? = nil
@@ -168,7 +171,7 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { @@ -168,7 +171,7 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
168 171
169 /// Toggles the zoomScale 172 /// Toggles the zoomScale
170 private func setScale(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) { 173 private func setScale(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
171 - var scale = call.arguments as? CGFloat 174 + let scale = call.arguments as? CGFloat
172 if (scale == nil) { 175 if (scale == nil) {
173 result(FlutterError(code: "MobileScanner", 176 result(FlutterError(code: "MobileScanner",
174 message: "You must provide a scale when calling setScale!", 177 message: "You must provide a scale when calling setScale!",