Skip to content

Commit

Permalink
reimplement toggle torch on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
navaronbracke committed Apr 30, 2024
1 parent 986f130 commit c544951
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
54 changes: 43 additions & 11 deletions ios/Classes/MobileScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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
Expand Down
14 changes: 5 additions & 9 deletions ios/Classes/MobileScannerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit c544951

Please sign in to comment.