Navaron Bracke

reimplement toggle torch on iOS

@@ -259,12 +259,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -259,12 +259,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
259 // as they interact with the hardware camera. 259 // as they interact with the hardware camera.
260 if (torch) { 260 if (torch) {
261 DispatchQueue.main.async { 261 DispatchQueue.main.async {
262 - do {  
263 - try self.toggleTorch(.on)  
264 - } catch {  
265 - // If the torch does not turn on,  
266 - // continue with the capture session anyway.  
267 - } 262 + self.turnTorchOn()
268 } 263 }
269 } 264 }
270 265
@@ -323,23 +318,60 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -323,23 +318,60 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
323 device = nil 318 device = nil
324 } 319 }
325 320
326 - /// Set the torch mode. 321 + /// Toggle the torch.
327 /// 322 ///
328 /// This method should be called on the main DispatchQueue. 323 /// This method should be called on the main DispatchQueue.
329 - func toggleTorch(_ torch: AVCaptureDevice.TorchMode) throws { 324 + func toggleTorch() {
330 guard let device = self.device else { 325 guard let device = self.device else {
331 return 326 return
332 } 327 }
333 328
334 - if (!device.hasTorch || !device.isTorchAvailable || !device.isTorchModeSupported(torch)) { 329 + if (!device.hasTorch || !device.isTorchAvailable) {
335 return 330 return
336 } 331 }
337 332
338 - if (device.torchMode != torch) { 333 + var newTorchMode: AVCaptureDevice.TorchMode = device.torchMode
  334 +
  335 + switch(device.torchMode) {
  336 + case AVCaptureDevice.TorchMode.auto:
  337 + newTorchMode = device.isTorchActive ? AVCaptureDevice.TorchMode.off : AVCaptureDevice.TorchMode.on
  338 + break;
  339 + case AVCaptureDevice.TorchMode.off:
  340 + newTorchMode = AVCaptureDevice.TorchMode.on
  341 + break;
  342 + case AVCaptureDevice.TorchMode.on:
  343 + newTorchMode = AVCaptureDevice.TorchMode.off
  344 + break;
  345 + default:
  346 + return;
  347 + }
  348 +
  349 + if (!device.isTorchModeSupported(newTorchMode) || device.torchMode == newTorchMode) {
  350 + return;
  351 + }
  352 +
  353 + do {
339 try device.lockForConfiguration() 354 try device.lockForConfiguration()
340 - device.torchMode = torch 355 + device.torchMode = newTorchMode
341 device.unlockForConfiguration() 356 device.unlockForConfiguration()
  357 + } catch(_) {}
  358 + }
  359 +
  360 + /// Turn the torch on.
  361 + private func turnTorchOn() {
  362 + guard let device = self.device else {
  363 + return
  364 + }
  365 +
  366 + if (!device.hasTorch || !device.isTorchAvailable || !device.isTorchModeSupported(.on) || device.torchMode == .on) {
  367 + return
342 } 368 }
  369 +
  370 + do {
  371 + try device.lockForConfiguration()
  372 + device.torchMode = .on
  373 + device.unlockForConfiguration()
  374 + } catch(_) {}
343 } 375 }
344 376
345 // Observer for torch state 377 // Observer for torch state
@@ -80,8 +80,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { @@ -80,8 +80,8 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
80 start(call, result) 80 start(call, result)
81 case "stop": 81 case "stop":
82 stop(result) 82 stop(result)
83 - case "torch":  
84 - toggleTorch(call, result) 83 + case "toggleTorch":
  84 + toggleTorch(result)
85 case "analyzeImage": 85 case "analyzeImage":
86 analyzeImage(call, result) 86 analyzeImage(call, result)
87 case "setScale": 87 case "setScale":
@@ -157,13 +157,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin { @@ -157,13 +157,9 @@ public class MobileScannerPlugin: NSObject, FlutterPlugin {
157 } 157 }
158 158
159 /// Toggles the torch. 159 /// Toggles the torch.
160 - private func toggleTorch(_ call: FlutterMethodCall, _ result: @escaping FlutterResult) {  
161 - do {  
162 - try mobileScanner.toggleTorch(call.arguments as? Int == 1 ? .on : .off)  
163 - result(nil)  
164 - } catch {  
165 - result(FlutterError(code: "MobileScanner", message: error.localizedDescription, details: nil))  
166 - } 160 + private func toggleTorch(_ result: @escaping FlutterResult) {
  161 + mobileScanner.toggleTorch()
  162 + result(nil)
167 } 163 }
168 164
169 /// Sets the zoomScale. 165 /// Sets the zoomScale.