Showing
1 changed file
with
42 additions
and
13 deletions
| @@ -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]) |
| @@ -381,29 +391,48 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | @@ -381,29 +391,48 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin, FlutterStreamHandler, | ||
| 381 | // result(nil) | 391 | // result(nil) |
| 382 | // } | 392 | // } |
| 383 | 393 | ||
| 394 | + func pause(_ result: FlutterResult) { | ||
| 395 | + if (paused || stopped) { | ||
| 396 | + result(nil) | ||
| 397 | + | ||
| 398 | + return | ||
| 399 | + } | ||
| 400 | + releaseCamera() | ||
| 401 | + } | ||
| 402 | + | ||
| 384 | func stop(_ result: FlutterResult) { | 403 | func stop(_ result: FlutterResult) { |
| 385 | - if (device == nil || captureSession == nil) { | 404 | + if (!paused && stopped) { |
| 405 | + result(nil) | ||
| 406 | + | ||
| 407 | + return | ||
| 408 | + } | ||
| 409 | + releaseCamera() | ||
| 410 | + releaseTexture() | ||
| 411 | + | ||
| 386 | result(nil) | 412 | result(nil) |
| 413 | + } | ||
| 387 | 414 | ||
| 415 | + private func releaseCamera() { | ||
| 416 | + guard let captureSession = captureSession else { | ||
| 388 | return | 417 | return |
| 389 | } | 418 | } |
| 390 | - captureSession!.stopRunning() | ||
| 391 | - for input in captureSession!.inputs { | ||
| 392 | - captureSession!.removeInput(input) | 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 |
| 404 | - textureId = nil | 431 | + } |
| 405 | 432 | ||
| 406 | - result(nil) | 433 | + private func releaseTexture() { |
| 434 | + registry.unregisterTexture(textureId) | ||
| 435 | + textureId = nil | ||
| 407 | } | 436 | } |
| 408 | 437 | ||
| 409 | // Observer for torch state | 438 | // Observer for torch state |
-
Please register or login to post a comment