Showing
1 changed file
with
65 additions
and
1 deletions
| @@ -71,6 +71,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | @@ -71,6 +71,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | ||
| 71 | stop(result) | 71 | stop(result) |
| 72 | case "updateScanWindow": | 72 | case "updateScanWindow": |
| 73 | updateScanWindow(call, result) | 73 | updateScanWindow(call, result) |
| 74 | + case "analyzeImage": | ||
| 75 | + analyzeImage(call, result) | ||
| 74 | default: | 76 | default: |
| 75 | result(FlutterMethodNotImplemented) | 77 | result(FlutterMethodNotImplemented) |
| 76 | } | 78 | } |
| @@ -124,7 +126,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | @@ -124,7 +126,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | ||
| 124 | VTCreateCGImageFromCVPixelBuffer(self!.latestBuffer, options: nil, imageOut: &cgImage) | 126 | VTCreateCGImageFromCVPixelBuffer(self!.latestBuffer, options: nil, imageOut: &cgImage) |
| 125 | let imageRequestHandler = VNImageRequestHandler(cgImage: cgImage!) | 127 | let imageRequestHandler = VNImageRequestHandler(cgImage: cgImage!) |
| 126 | do { | 128 | do { |
| 127 | - let barcodeRequest:VNDetectBarcodesRequest = VNDetectBarcodesRequest(completionHandler: { [weak self] (request, error) in | 129 | + let barcodeRequest: VNDetectBarcodesRequest = VNDetectBarcodesRequest(completionHandler: { [weak self] (request, error) in |
| 128 | self?.imagesCurrentlyBeingProcessed = false | 130 | self?.imagesCurrentlyBeingProcessed = false |
| 129 | 131 | ||
| 130 | if error != nil { | 132 | if error != nil { |
| @@ -452,6 +454,68 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | @@ -452,6 +454,68 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | ||
| 452 | result(nil) | 454 | result(nil) |
| 453 | } | 455 | } |
| 454 | 456 | ||
| 457 | + func analyzeImage(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) { | ||
| 458 | + let argReader = MapArgumentReader(call.arguments as? [String: Any]) | ||
| 459 | + let symbologies:[VNBarcodeSymbology] = argReader.toSymbology() | ||
| 460 | + | ||
| 461 | + guard let fileUrl: URL = URL(string: argReader.string(key: "filePath") ?? "") else { | ||
| 462 | + // TODO: fix error code | ||
| 463 | + result(FlutterError(code: "MobileScanner", | ||
| 464 | + message: "No image found in analyzeImage!", | ||
| 465 | + details: nil)) | ||
| 466 | + return | ||
| 467 | + } | ||
| 468 | + | ||
| 469 | + guard let ciImage = CIImage(contentsOf: fileUrl) else { | ||
| 470 | + // TODO: fix error code | ||
| 471 | + result(FlutterError(code: "MobileScanner", | ||
| 472 | + message: "No image found in analyzeImage!", | ||
| 473 | + details: nil)) | ||
| 474 | + return | ||
| 475 | + } | ||
| 476 | + | ||
| 477 | + let imageRequestHandler = VNImageRequestHandler(ciImage: ciImage, orientation: CGImagePropertyOrientation.up, options: [:]) | ||
| 478 | + | ||
| 479 | + do { | ||
| 480 | + let barcodeRequest: VNDetectBarcodesRequest = VNDetectBarcodesRequest( | ||
| 481 | + completionHandler: { [] (request, error) in | ||
| 482 | + | ||
| 483 | + if error != nil { | ||
| 484 | + DispatchQueue.main.async { | ||
| 485 | + // TODO: fix error code | ||
| 486 | + result(FlutterError(code: "MobileScanner", message: error?.localizedDescription, details: nil)) | ||
| 487 | + } | ||
| 488 | + return | ||
| 489 | + } | ||
| 490 | + | ||
| 491 | + guard let barcodes: [VNBarcodeObservation] = request.results as? [VNBarcodeObservation] else { | ||
| 492 | + return | ||
| 493 | + } | ||
| 494 | + | ||
| 495 | + if barcodes.isEmpty { | ||
| 496 | + return | ||
| 497 | + } | ||
| 498 | + | ||
| 499 | + result([ | ||
| 500 | + "name": "barcode", | ||
| 501 | + "data": barcodes.map({ $0.toMap() }), | ||
| 502 | + ]) | ||
| 503 | + }) | ||
| 504 | + | ||
| 505 | + if !symbologies.isEmpty { | ||
| 506 | + // Add the symbologies the user wishes to support. | ||
| 507 | + barcodeRequest.symbologies = symbologies | ||
| 508 | + } | ||
| 509 | + | ||
| 510 | + try imageRequestHandler.perform([barcodeRequest]) | ||
| 511 | + } catch let e { | ||
| 512 | + // TODO: fix error code | ||
| 513 | + DispatchQueue.main.async { | ||
| 514 | + result(FlutterError(code: "MobileScanner", message: e.localizedDescription, details: nil)) | ||
| 515 | + } | ||
| 516 | + } | ||
| 517 | + } | ||
| 518 | + | ||
| 455 | // Observer for torch state | 519 | // Observer for torch state |
| 456 | public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | 520 | public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { |
| 457 | switch keyPath { | 521 | switch keyPath { |
-
Please register or login to post a comment