handle bar code types
limits the bar code types and correctly reports them back to the flutter controller. flutter controller adds the barcode format to the object.
Showing
2 changed files
with
104 additions
and
7 deletions
| @@ -384,6 +384,7 @@ class MobileScannerController { | @@ -384,6 +384,7 @@ class MobileScannerController { | ||
| 384 | barcodes: [ | 384 | barcodes: [ |
| 385 | Barcode( | 385 | Barcode( |
| 386 | rawValue: (data as Map)['payload'] as String?, | 386 | rawValue: (data as Map)['payload'] as String?, |
| 387 | + format: toFormat(data['symbology'] as int) | ||
| 387 | ) | 388 | ) |
| 388 | ], | 389 | ], |
| 389 | ), | 390 | ), |
| @@ -28,6 +28,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | @@ -28,6 +28,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | ||
| 28 | var detectionSpeed: DetectionSpeed = DetectionSpeed.noDuplicates | 28 | var detectionSpeed: DetectionSpeed = DetectionSpeed.noDuplicates |
| 29 | 29 | ||
| 30 | var timeoutSeconds: Double = 0 | 30 | var timeoutSeconds: Double = 0 |
| 31 | + | ||
| 32 | + var symbologies:[VNBarcodeSymbology] = [] | ||
| 31 | 33 | ||
| 32 | 34 | ||
| 33 | // var analyzeMode: Int = 0 | 35 | // var analyzeMode: Int = 0 |
| @@ -118,7 +120,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | @@ -118,7 +120,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | ||
| 118 | orientation: .right) | 120 | orientation: .right) |
| 119 | 121 | ||
| 120 | do { | 122 | do { |
| 121 | - try imageRequestHandler.perform([VNDetectBarcodesRequest { [self] (request, error) in | 123 | + let barcodeRequest:VNDetectBarcodesRequest = VNDetectBarcodesRequest(completionHandler: { [self] (request, error) in |
| 122 | imagesCurrentlyBeingProcessed -= 1 | 124 | imagesCurrentlyBeingProcessed -= 1 |
| 123 | if error == nil { | 125 | if error == nil { |
| 124 | if let results = request.results as? [VNBarcodeObservation] { | 126 | if let results = request.results as? [VNBarcodeObservation] { |
| @@ -131,7 +133,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | @@ -131,7 +133,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | ||
| 131 | } | 133 | } |
| 132 | 134 | ||
| 133 | let barcodeType = String(barcode.symbology.rawValue).replacingOccurrences(of: "VNBarcodeSymbology", with: "") | 135 | let barcodeType = String(barcode.symbology.rawValue).replacingOccurrences(of: "VNBarcodeSymbology", with: "") |
| 134 | - let event: [String: Any?] = ["name": "barcodeMac", "data" : ["payload": barcode.payloadStringValue, "symbology": barcodeType]] | 136 | + let event: [String: Any?] = ["name": "barcodeMac", "data" : ["payload": barcode.payloadStringValue, "symbology": barcode.symbology.toInt as Any?]] |
| 135 | self.sink?(event) | 137 | self.sink?(event) |
| 136 | 138 | ||
| 137 | // if barcodeType == "QR" { | 139 | // if barcodeType == "QR" { |
| @@ -144,7 +146,12 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | @@ -144,7 +146,12 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | ||
| 144 | } else { | 146 | } else { |
| 145 | print(error!.localizedDescription) | 147 | print(error!.localizedDescription) |
| 146 | } | 148 | } |
| 147 | - }]) | 149 | + }) |
| 150 | + if(symbologies.isEmpty == false){ | ||
| 151 | + // add the symbologies the user wishes to support | ||
| 152 | + barcodeRequest.symbologies = symbologies | ||
| 153 | + } | ||
| 154 | + try imageRequestHandler.perform([barcodeRequest]) | ||
| 148 | } catch { | 155 | } catch { |
| 149 | print(error) | 156 | print(error) |
| 150 | } | 157 | } |
| @@ -225,6 +232,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | @@ -225,6 +232,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | ||
| 225 | let facing: Int = argReader.int(key: "facing") ?? 1 | 232 | let facing: Int = argReader.int(key: "facing") ?? 1 |
| 226 | let speed: Int = (call.arguments as! Dictionary<String, Any?>)["speed"] as? Int ?? 0 | 233 | let speed: Int = (call.arguments as! Dictionary<String, Any?>)["speed"] as? Int ?? 0 |
| 227 | let timeoutMs: Int = (call.arguments as! Dictionary<String, Any?>)["timeout"] as? Int ?? 0 | 234 | let timeoutMs: Int = (call.arguments as! Dictionary<String, Any?>)["timeout"] as? Int ?? 0 |
| 235 | + symbologies = argReader.toSymbology() | ||
| 228 | 236 | ||
| 229 | timeoutSeconds = Double(timeoutMs) / 1000.0 | 237 | timeoutSeconds = Double(timeoutMs) / 1000.0 |
| 230 | detectionSpeed = DetectionSpeed(rawValue: speed)! | 238 | detectionSpeed = DetectionSpeed(rawValue: speed)! |
| @@ -365,16 +373,104 @@ class MapArgumentReader { | @@ -365,16 +373,104 @@ class MapArgumentReader { | ||
| 365 | return (args?[key] as? NSNumber)?.intValue | 373 | return (args?[key] as? NSNumber)?.intValue |
| 366 | } | 374 | } |
| 367 | 375 | ||
| 368 | - func bool(key: String) -> Bool? { | ||
| 369 | - return (args?[key] as? NSNumber)?.boolValue | ||
| 370 | - } | ||
| 371 | - | 376 | + func bool(key: String) -> Bool? { |
| 377 | + return (args?[key] as? NSNumber)?.boolValue | ||
| 378 | + } | ||
| 379 | + | ||
| 372 | func stringArray(key: String) -> [String]? { | 380 | func stringArray(key: String) -> [String]? { |
| 373 | return args?[key] as? [String] | 381 | return args?[key] as? [String] |
| 374 | } | 382 | } |
| 383 | + | ||
| 384 | + func toSymbology() -> [VNBarcodeSymbology] { | ||
| 385 | + guard let syms:[Int] = args?["formats"] as? [Int] else { | ||
| 386 | + return [] | ||
| 387 | + } | ||
| 388 | + if(syms.contains(0)){ | ||
| 389 | + return [] | ||
| 390 | + } | ||
| 391 | + var barcodeFormats:[VNBarcodeSymbology] = [] | ||
| 392 | + syms.forEach { id in | ||
| 393 | + if let bc:VNBarcodeSymbology = VNBarcodeSymbology.fromInt(id) { | ||
| 394 | + barcodeFormats.append(bc) | ||
| 395 | + } | ||
| 396 | + } | ||
| 397 | + return barcodeFormats | ||
| 398 | + } | ||
| 375 | 399 | ||
| 376 | func floatArray(key: String) -> [CGFloat]? { | 400 | func floatArray(key: String) -> [CGFloat]? { |
| 377 | return args?[key] as? [CGFloat] | 401 | return args?[key] as? [CGFloat] |
| 378 | } | 402 | } |
| 379 | 403 | ||
| 380 | } | 404 | } |
| 405 | + | ||
| 406 | +extension VNBarcodeSymbology { | ||
| 407 | + | ||
| 408 | + static func fromInt(_ mapValue:Int) -> VNBarcodeSymbology? { | ||
| 409 | + if #available(macOS 12.0, *) { | ||
| 410 | + if(mapValue == 8){ | ||
| 411 | + return VNBarcodeSymbology.codabar | ||
| 412 | + } | ||
| 413 | + } | ||
| 414 | + switch(mapValue){ | ||
| 415 | + case 1: | ||
| 416 | + return VNBarcodeSymbology.code128 | ||
| 417 | + case 2: | ||
| 418 | + return VNBarcodeSymbology.code39 | ||
| 419 | + case 4: | ||
| 420 | + return VNBarcodeSymbology.code93 | ||
| 421 | + case 16: | ||
| 422 | + return VNBarcodeSymbology.dataMatrix | ||
| 423 | + case 32: | ||
| 424 | + return VNBarcodeSymbology.ean13 | ||
| 425 | + case 64: | ||
| 426 | + return VNBarcodeSymbology.ean8 | ||
| 427 | + case 128: | ||
| 428 | + return VNBarcodeSymbology.itf14 | ||
| 429 | + case 256: | ||
| 430 | + return VNBarcodeSymbology.qr | ||
| 431 | + case 1024: | ||
| 432 | + return VNBarcodeSymbology.upce | ||
| 433 | + case 2048: | ||
| 434 | + return VNBarcodeSymbology.pdf417 | ||
| 435 | + case 4096: | ||
| 436 | + return VNBarcodeSymbology.aztec | ||
| 437 | + default: | ||
| 438 | + return nil | ||
| 439 | + } | ||
| 440 | + } | ||
| 441 | + | ||
| 442 | + var toInt:Int? { | ||
| 443 | + if #available(macOS 12.0, *) { | ||
| 444 | + if(self == VNBarcodeSymbology.codabar){ | ||
| 445 | + return 8 | ||
| 446 | + } | ||
| 447 | + } | ||
| 448 | + switch(self){ | ||
| 449 | + case VNBarcodeSymbology.code128: | ||
| 450 | + return 1 | ||
| 451 | + case VNBarcodeSymbology.code39: | ||
| 452 | + return 2 | ||
| 453 | + case VNBarcodeSymbology.code93: | ||
| 454 | + return 4 | ||
| 455 | + case VNBarcodeSymbology.dataMatrix: | ||
| 456 | + return 16 | ||
| 457 | + case VNBarcodeSymbology.ean13: | ||
| 458 | + return 32 | ||
| 459 | + case VNBarcodeSymbology.ean8: | ||
| 460 | + return 64 | ||
| 461 | + case VNBarcodeSymbology.itf14: | ||
| 462 | + return 128 | ||
| 463 | + case VNBarcodeSymbology.qr: | ||
| 464 | + return 256 | ||
| 465 | + case VNBarcodeSymbology.upce: | ||
| 466 | + return 1024 | ||
| 467 | + case VNBarcodeSymbology.pdf417: | ||
| 468 | + return 2048 | ||
| 469 | + case VNBarcodeSymbology.aztec: | ||
| 470 | + return 4096 | ||
| 471 | + default: | ||
| 472 | + return -1; | ||
| 473 | + } | ||
| 474 | + } | ||
| 475 | + | ||
| 476 | +} |
-
Please register or login to post a comment