Showing
2 changed files
with
20 additions
and
13 deletions
| @@ -12,6 +12,7 @@ import MLKitVision | @@ -12,6 +12,7 @@ import MLKitVision | ||
| 12 | import MLKitBarcodeScanning | 12 | import MLKitBarcodeScanning |
| 13 | 13 | ||
| 14 | typealias MobileScannerCallback = ((Array<Barcode>?, Error?, UIImage) -> ()) | 14 | typealias MobileScannerCallback = ((Array<Barcode>?, Error?, UIImage) -> ()) |
| 15 | +typealias TorchModeChangeCallback = ((Int?) -> ()) | ||
| 15 | 16 | ||
| 16 | public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate, FlutterTexture { | 17 | public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate, FlutterTexture { |
| 17 | /// Capture session of the camera | 18 | /// Capture session of the camera |
| @@ -32,6 +33,9 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | @@ -32,6 +33,9 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | ||
| 32 | /// When results are found, this callback will be called | 33 | /// When results are found, this callback will be called |
| 33 | let mobileScannerCallback: MobileScannerCallback | 34 | let mobileScannerCallback: MobileScannerCallback |
| 34 | 35 | ||
| 36 | + /// When torch mode is changes, this callback will be called | ||
| 37 | + let torchModeChangeCallback: TorchModeChangeCallback | ||
| 38 | + | ||
| 35 | /// If provided, the Flutter registry will be used to send the output of the CaptureOutput to a Flutter texture. | 39 | /// If provided, the Flutter registry will be used to send the output of the CaptureOutput to a Flutter texture. |
| 36 | private let registry: FlutterTextureRegistry? | 40 | private let registry: FlutterTextureRegistry? |
| 37 | 41 | ||
| @@ -43,9 +47,10 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | @@ -43,9 +47,10 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | ||
| 43 | 47 | ||
| 44 | var detectionSpeed: DetectionSpeed = DetectionSpeed.noDuplicates | 48 | var detectionSpeed: DetectionSpeed = DetectionSpeed.noDuplicates |
| 45 | 49 | ||
| 46 | - init(registry: FlutterTextureRegistry?, mobileScannerCallback: @escaping MobileScannerCallback) { | 50 | + init(registry: FlutterTextureRegistry?, mobileScannerCallback: @escaping MobileScannerCallback, torchModeChangeCallback: @escaping TorchModeChangeCallback) { |
| 47 | self.registry = registry | 51 | self.registry = registry |
| 48 | self.mobileScannerCallback = mobileScannerCallback | 52 | self.mobileScannerCallback = mobileScannerCallback |
| 53 | + self.torchModeChangeCallback = torchModeChangeCallback | ||
| 49 | super.init() | 54 | super.init() |
| 50 | } | 55 | } |
| 51 | 56 | ||
| @@ -207,6 +212,18 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | @@ -207,6 +212,18 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | ||
| 207 | } | 212 | } |
| 208 | } | 213 | } |
| 209 | 214 | ||
| 215 | + // Observer for torch state | ||
| 216 | + public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | ||
| 217 | + switch keyPath { | ||
| 218 | + case "torchMode": | ||
| 219 | + // off = 0; on = 1; auto = 2; | ||
| 220 | + let state = change?[.newKey] as? Int | ||
| 221 | + torchModeChangeCallback(state) | ||
| 222 | + default: | ||
| 223 | + break | ||
| 224 | + } | ||
| 225 | + } | ||
| 226 | + | ||
| 210 | /// Analyze a single image | 227 | /// Analyze a single image |
| 211 | func analyzeImage(image: UIImage, position: AVCaptureDevice.Position, callback: @escaping BarcodeScanningCallback) { | 228 | func analyzeImage(image: UIImage, position: AVCaptureDevice.Position, callback: @escaping BarcodeScanningCallback) { |
| 212 | let image = VisionImage(image: image) | 229 | let image = VisionImage(image: image) |
| @@ -23,6 +23,8 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { | @@ -23,6 +23,8 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { | ||
| 23 | } else if (error != nil){ | 23 | } else if (error != nil){ |
| 24 | barcodeHandler.publishEvent(["name": "error", "data": error!.localizedDescription]) | 24 | barcodeHandler.publishEvent(["name": "error", "data": error!.localizedDescription]) |
| 25 | } | 25 | } |
| 26 | + }, torchModeChangeCallback: { torchState in | ||
| 27 | + barcodeHandler.publishEvent(["name": "torchState", "data": torchState]) | ||
| 26 | }) | 28 | }) |
| 27 | self.barcodeHandler = barcodeHandler | 29 | self.barcodeHandler = barcodeHandler |
| 28 | super.init() | 30 | super.init() |
| @@ -145,16 +147,4 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { | @@ -145,16 +147,4 @@ public class SwiftMobileScannerPlugin: NSObject, FlutterPlugin { | ||
| 145 | }) | 147 | }) |
| 146 | result(nil) | 148 | result(nil) |
| 147 | } | 149 | } |
| 148 | - | ||
| 149 | - /// Observer for torch state | ||
| 150 | - public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | ||
| 151 | - switch keyPath { | ||
| 152 | - case "torchMode": | ||
| 153 | - // off = 0; on = 1; auto = 2; | ||
| 154 | - let state = change?[.newKey] as? Int | ||
| 155 | - barcodeHandler.publishEvent(["name": "torchState", "data": state]) | ||
| 156 | - default: | ||
| 157 | - break | ||
| 158 | - } | ||
| 159 | - } | ||
| 160 | } | 150 | } |
-
Please register or login to post a comment