diff --git a/ios/Classes/MobileScanner.swift b/ios/Classes/MobileScanner.swift index 5f0b07858..e9c066f25 100644 --- a/ios/Classes/MobileScanner.swift +++ b/ios/Classes/MobileScanner.swift @@ -259,12 +259,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega // as they interact with the hardware camera. if (torch) { DispatchQueue.main.async { - do { - try self.toggleTorch(.on) - } catch { - // If the torch does not turn on, - // continue with the capture session anyway. - } + self.turnTorchOn() } } @@ -323,23 +318,60 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega device = nil } - /// Set the torch mode. + /// Toggle the torch. /// /// This method should be called on the main DispatchQueue. - func toggleTorch(_ torch: AVCaptureDevice.TorchMode) throws { + func toggleTorch() { guard let device = self.device else { return } - if (!device.hasTorch || !device.isTorchAvailable || !device.isTorchModeSupported(torch)) { + if (!device.hasTorch || !device.isTorchAvailable) { return } - if (device.torchMode != torch) { + var newTorchMode: AVCaptureDevice.TorchMode = device.torchMode + + switch(device.torchMode) { + case AVCaptureDevice.TorchMode.auto: + newTorchMode = device.isTorchActive ? AVCaptureDevice.TorchMode.off : AVCaptureDevice.TorchMode.on + break; + case AVCaptureDevice.TorchMode.off: + newTorchMode = AVCaptureDevice.TorchMode.on + break; + case AVCaptureDevice.TorchMode.on: + newTorchMode = AVCaptureDevice.TorchMode.off + break; + default: + return; + } + + if (!device.isTorchModeSupported(newTorchMode) || device.torchMode == newTorchMode) { + return; + } + + do { try device.lockForConfiguration() - device.torchMode = torch + device.torchMode = newTorchMode device.unlockForConfiguration() + } catch(_) {} + } + + /// Turn the torch on. + private func turnTorchOn() { + guard let device = self.device else { + return + } + + if (!device.hasTorch || !device.isTorchAvailable || !device.isTorchModeSupported(.on) || device.torchMode == .on) { + return } + + do { + try device.lockForConfiguration() + device.torchMode = .on + device.unlockForConfiguration() + } catch(_) {} } // Observer for torch state diff --git a/ios/Classes/MobileScannerPlugin.swift b/ios/Classes/MobileScannerPlugin.swift index 929bab9fd..eba359a36 100644 --- a/ios/Classes/MobileScannerPlugin.swift +++ b/ios/Classes/MobileScannerPlugin.swift @@ -80,8 +80,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { start(call, result) case "stop": stop(result) - case "torch": - toggleTorch(call, result) + case "toggleTorch": + toggleTorch(result) case "analyzeImage": analyzeImage(call, result) case "setScale": @@ -157,13 +157,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { } /// Toggles the torch. - private func toggleTorch(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) { - do { - try mobileScanner.toggleTorch(call.arguments as? Int == 1 ? .on : .off) - result(nil) - } catch { - result(FlutterError(code: "MobileScanner", message: error.localizedDescription, details: nil)) - } + private func toggleTorch(_ result: @escaping FlutterResult) { + mobileScanner.toggleTorch() + result(nil) } /// Sets the zoomScale.