Showing
2 changed files
with
24 additions
and
10 deletions
| @@ -54,6 +54,12 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | @@ -54,6 +54,12 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | ||
| 54 | private let backgroundQueue = DispatchQueue(label: "camera-handling") | 54 | private let backgroundQueue = DispatchQueue(label: "camera-handling") |
| 55 | 55 | ||
| 56 | var standardZoomFactor: CGFloat = 1 | 56 | var standardZoomFactor: CGFloat = 1 |
| 57 | + | ||
| 58 | + private var nextScanTime = 0.0 | ||
| 59 | + | ||
| 60 | + private var imagesCurrentlyBeingProcessed = 0 | ||
| 61 | + | ||
| 62 | + public var timeoutSeconds: Double = 0 | ||
| 57 | 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 |
| @@ -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,8 +12,9 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { | @@ -12,8 +12,9 @@ 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 { |
| 18 | let scanwindow = SwiftMobileScannerPlugin.scanWindow! | 19 | let scanwindow = SwiftMobileScannerPlugin.scanWindow! |
| 19 | let barcodeminX = barcode.cornerPoints![0].cgPointValue.x | 20 | let barcodeminX = barcode.cornerPoints![0].cgPointValue.x |
| @@ -103,7 +104,9 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { | @@ -103,7 +104,9 @@ 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 |
| 106 | - | 107 | + let timeoutMs: Int = (call.arguments as! Dictionary<String, Any?>)["timeout"] as? Int ?? 0 |
| 108 | + self.mobileScanner.timeoutSeconds = Double(timeoutMs / 1000) | ||
| 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 |
| 109 | 112 | ||
| @@ -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!", |
-
Please register or login to post a comment