Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion android/src/main/kotlin/com/ultralytics/yolo/YOLOPlatformView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@ class YOLOPlatformView(
yoloView.switchCamera()
result.success(null)
}
"setTorchMode" -> {
val enabled = call.argument<Boolean>("enabled")
if (enabled != null) {
yoloView.setTorchMode(enabled)
result.success(null)
} else {
result.error("invalid_args", "enabled is required", null)
}
}
"setZoomLevel" -> {
val zoomLevel = call.argument<Double>("zoomLevel")
if (zoomLevel != null) {
Expand Down Expand Up @@ -500,4 +509,4 @@ class YOLOPlatformView(
else -> modelPath
}
}
}
}
8 changes: 8 additions & 0 deletions android/src/main/kotlin/com/ultralytics/yolo/YOLOView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,14 @@ class YOLOView @JvmOverloads constructor(
}
}

fun setTorchMode(enabled: Boolean) {
camera?.let { cam ->
if (cam.cameraInfo.hasFlashUnit()) {
cam.cameraControl.enableTorch(enabled)
}
}
}

// endregion

// region Model / Task
Expand Down
10 changes: 10 additions & 0 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,16 @@ Switch between front and back camera.
Future<void> switchCamera()
```

##### `setTorchMode()`

Turn the active camera torch on or off when supported.

```dart
Future<void> setTorchMode(bool enabled)
```

**Parameters**: `enabled` - `true` to enable the torch, `false` to disable it

##### `switchModel()`

Dynamically switch to a different model without restarting the camera.
Expand Down
12 changes: 12 additions & 0 deletions ios/Classes/SwiftYOLOPlatformView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,18 @@ public class SwiftYOLOPlatformView: NSObject, FlutterPlatformView, FlutterStream
self.yoloView?.switchCameraTapped()
result(nil) // Success

case "setTorchMode":
if let args = call.arguments as? [String: Any],
let enabled = args["enabled"] as? Bool
{
self.yoloView?.setTorchMode(enabled)
result(nil) // Success
} else {
result(
FlutterError(
code: "invalid_args", message: "Invalid arguments for setTorchMode", details: nil))
}

case "setZoomLevel":
if let args = call.arguments as? [String: Any],
let zoomLevel = args["zoomLevel"] as? Double
Expand Down
19 changes: 19 additions & 0 deletions ios/Classes/YOLOView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,25 @@ public class YOLOView: UIView, VideoCaptureDelegate {
}
}

public func setTorchMode(_ enabled: Bool) {
guard let device = videoCapture.captureDevice, device.hasTorch else { return }

do {
try device.lockForConfiguration()
defer {
device.unlockForConfiguration()
}

if enabled {
try device.setTorchModeOn(level: AVCaptureDevice.maxAvailableTorchLevel)
} else {
device.torchMode = .off
}
} catch {
print("Failed to set torch mode: \(error.localizedDescription)")
}
}

@objc func playTapped() {
selection.selectionChanged()
self.videoCapture.start()
Expand Down
12 changes: 12 additions & 0 deletions lib/widgets/yolo_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ class YOLOViewController {
}
}

Future<void> setTorchMode(bool enabled) async {
if (_methodChannel != null) {
try {
await _methodChannel!.invokeMethod('setTorchMode', {
'enabled': enabled,
});
} catch (e) {
logInfo('Error setting torch mode: $e');
}
}
}

Future<void> zoomIn() async {
if (_methodChannel != null) {
try {
Expand Down
6 changes: 6 additions & 0 deletions test/utils/test_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class YOLOTestHelpers {
return true;
case 'switchCamera':
return true;
case 'setTorchMode':
return true;
case 'zoomIn':
return true;
case 'zoomOut':
Expand Down Expand Up @@ -407,6 +409,10 @@ class YOLOTestHelpers {
log.add(call);
return true;
},
'setTorchMode': (call) {
log.add(call);
return true;
},
'zoomIn': (call) {
log.add(call);
return true;
Expand Down
7 changes: 7 additions & 0 deletions test/yolo_controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ void main() {
await controller.switchCamera();
YOLOTestHelpers.assertMethodCalled(log, 'switchCamera');

await controller.setTorchMode(true);
YOLOTestHelpers.assertMethodCalled(
log,
'setTorchMode',
arguments: {'enabled': true},
);

await controller.zoomIn();
YOLOTestHelpers.assertMethodCalled(log, 'zoomIn');

Expand Down
Loading