fumin65

add pause function to macos part

@@ -36,6 +36,14 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, @@ -36,6 +36,14 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
36 var analyzing: Bool = false 36 var analyzing: Bool = false
37 var position = AVCaptureDevice.Position.back 37 var position = AVCaptureDevice.Position.back
38 38
  39 + private var stopped: Bool {
  40 + return device == nil || captureSession == nil
  41 + }
  42 +
  43 + private var paused: Bool {
  44 + return stopped && textureId != nil
  45 + }
  46 +
39 public static func register(with registrar: FlutterPluginRegistrar) { 47 public static func register(with registrar: FlutterPluginRegistrar) {
40 let instance = MobileScannerPlugin(registrar.textures) 48 let instance = MobileScannerPlugin(registrar.textures)
41 let method = FlutterMethodChannel(name: 49 let method = FlutterMethodChannel(name:
@@ -67,6 +75,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, @@ -67,6 +75,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
67 resetScale(call, result) 75 resetScale(call, result)
68 // case "analyze": 76 // case "analyze":
69 // switchAnalyzeMode(call, result) 77 // switchAnalyzeMode(call, result)
  78 + case "pause":
  79 + pause(result)
70 case "stop": 80 case "stop":
71 stop(result) 81 stop(result)
72 case "updateScanWindow": 82 case "updateScanWindow":
@@ -254,7 +264,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, @@ -254,7 +264,7 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
254 return 264 return
255 } 265 }
256 266
257 - textureId = registry.register(self) 267 + textureId = textureId ?? registry.register(self)
258 captureSession = AVCaptureSession() 268 captureSession = AVCaptureSession()
259 269
260 let argReader = MapArgumentReader(call.arguments as? [String: Any]) 270 let argReader = MapArgumentReader(call.arguments as? [String: Any])
@@ -380,30 +390,49 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, @@ -380,30 +390,49 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler,
380 // analyzeMode = call.arguments as! Int 390 // analyzeMode = call.arguments as! Int
381 // result(nil) 391 // result(nil)
382 // } 392 // }
  393 +
  394 + func pause(_ result: FlutterResult) {
  395 + if (paused || stopped) {
  396 + result(nil)
  397 +
  398 + return
  399 + }
  400 + releaseCamera()
  401 + }
383 402
384 func stop(_ result: FlutterResult) { 403 func stop(_ result: FlutterResult) {
385 - if (device == nil || captureSession == nil) { 404 + if (!paused && stopped) {
386 result(nil) 405 result(nil)
387 406
388 return 407 return
389 } 408 }
390 - captureSession!.stopRunning()  
391 - for input in captureSession!.inputs {  
392 - captureSession!.removeInput(input) 409 + releaseCamera()
  410 + releaseTexture()
  411 +
  412 + result(nil)
  413 + }
  414 +
  415 + private func releaseCamera() {
  416 + guard let captureSession = captureSession else {
  417 + return
  418 + }
  419 +
  420 + captureSession.stopRunning()
  421 + for input in captureSession.inputs {
  422 + captureSession.removeInput(input)
393 } 423 }
394 - for output in captureSession!.outputs {  
395 - captureSession!.removeOutput(output) 424 + for output in captureSession.outputs {
  425 + captureSession.removeOutput(output)
396 } 426 }
397 device.removeObserver(self, forKeyPath: #keyPath(AVCaptureDevice.torchMode)) 427 device.removeObserver(self, forKeyPath: #keyPath(AVCaptureDevice.torchMode))
398 - registry.unregisterTexture(textureId)  
399 -  
400 - // analyzeMode = 0  
401 latestBuffer = nil 428 latestBuffer = nil
402 - captureSession = nil 429 + self.captureSession = nil
403 device = nil 430 device = nil
  431 + }
  432 +
  433 + private func releaseTexture() {
  434 + registry.unregisterTexture(textureId)
404 textureId = nil 435 textureId = nil
405 -  
406 - result(nil)  
407 } 436 }
408 437
409 // Observer for torch state 438 // Observer for torch state