Skip to content

Commit

Permalink
Merge pull request #789 from navaronbracke/ios_scan_timeout
Browse files Browse the repository at this point in the history
fix: Port scan timeout to iOS
  • Loading branch information
navaronbracke authored Oct 20, 2023
2 parents 48e4a83 + f884acb commit 4de6f4a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
25 changes: 18 additions & 7 deletions ios/Classes/MobileScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
private let backgroundQueue = DispatchQueue(label: "camera-handling")

var standardZoomFactor: CGFloat = 1

private var nextScanTime = 0.0

private var imagesCurrentlyBeingProcessed = false

public var timeoutSeconds: Double = 0

init(registry: FlutterTextureRegistry?, mobileScannerCallback: @escaping MobileScannerCallback, torchModeChangeCallback: @escaping TorchModeChangeCallback, zoomScaleChangeCallback: @escaping ZoomScaleChangeCallback) {
self.registry = registry
Expand Down Expand Up @@ -89,8 +95,15 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
}
latestBuffer = imageBuffer
registry?.textureFrameAvailable(textureId)
if ((detectionSpeed == DetectionSpeed.normal || detectionSpeed == DetectionSpeed.noDuplicates) && i > 10 || detectionSpeed == DetectionSpeed.unrestricted) {
i = 0

let currentTime = Date().timeIntervalSince1970
let eligibleForScan = currentTime > nextScanTime && !imagesCurrentlyBeingProcessed

if ((detectionSpeed == DetectionSpeed.normal || detectionSpeed == DetectionSpeed.noDuplicates) && eligibleForScan || detectionSpeed == DetectionSpeed.unrestricted) {

nextScanTime = currentTime + timeoutSeconds
imagesCurrentlyBeingProcessed = true

let ciImage = latestBuffer.image

let image = VisionImage(image: ciImage)
Expand All @@ -101,6 +114,8 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
)

scanner.process(image) { [self] barcodes, error in
imagesCurrentlyBeingProcessed = false

if (detectionSpeed == DetectionSpeed.noDuplicates) {
let newScannedBarcodes = barcodes?.map { barcode in
return barcode.rawValue
Expand All @@ -114,8 +129,6 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega

mobileScannerCallback(barcodes, error, ciImage)
}
} else {
i+=1
}
}

Expand Down Expand Up @@ -301,7 +314,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega

do {
try device.lockForConfiguration()
var maxZoomFactor = device.activeFormat.videoMaxZoomFactor
let maxZoomFactor = device.activeFormat.videoMaxZoomFactor

var actualScale = (scale * 4) + 1

Expand Down Expand Up @@ -348,8 +361,6 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
scanner.process(image, completion: callback)
}

var i = 0

var barcodesString: Array<String?>?


Expand Down
9 changes: 6 additions & 3 deletions ios/Classes/SwiftMobileScannerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
/// The handler sends all information via an event channel back to Flutter
private let barcodeHandler: BarcodeHandler

/// The points for the scan window.
static var scanWindow: [CGFloat]?

private static func isBarcodeInScanWindow(barcode: Barcode, imageSize: CGSize) -> Bool {
let scanwindow = SwiftMobileScannerPlugin.scanWindow!
let barcodeminX = barcode.cornerPoints![0].cgPointValue.x
Expand Down Expand Up @@ -103,7 +104,9 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {
let formats: Array<Int> = (call.arguments as! Dictionary<String, Any?>)["formats"] as? Array ?? []
let returnImage: Bool = (call.arguments as! Dictionary<String, Any?>)["returnImage"] as? Bool ?? false
let speed: Int = (call.arguments as! Dictionary<String, Any?>)["speed"] as? Int ?? 0

let timeoutMs: Int = (call.arguments as! Dictionary<String, Any?>)["timeout"] as? Int ?? 0
self.mobileScanner.timeoutSeconds = Double(timeoutMs / 1000)

let formatList = formats.map { format in return BarcodeFormat(rawValue: format)}
var barcodeOptions: BarcodeScannerOptions? = nil

Expand Down Expand Up @@ -168,7 +171,7 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin {

/// Toggles the zoomScale
private func setScale(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {
var scale = call.arguments as? CGFloat
let scale = call.arguments as? CGFloat
if (scale == nil) {
result(FlutterError(code: "MobileScanner",
message: "You must provide a scale when calling setScale!",
Expand Down

0 comments on commit 4de6f4a

Please sign in to comment.