Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

add feature to enable streaming image with torch on #2957

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class Camera {
private boolean recordingVideo;
private CamcorderProfile recordingProfile;
private int currentOrientation = ORIENTATION_UNKNOWN;
private boolean isTorchOn = false;

// Mirrors camera.dart
public enum ResolutionPreset {
Expand Down Expand Up @@ -278,8 +279,7 @@ public void onCaptureFailed(
}
}

private void createCaptureSession(int templateType, Surface... surfaces)
throws CameraAccessException {
private void createCaptureSession(int templateType, Surface... surfaces) throws CameraAccessException {
createCaptureSession(templateType, null, surfaces);
}

Expand Down Expand Up @@ -318,8 +318,10 @@ public void onConfigured(@NonNull CameraCaptureSession session) {
return;
}
cameraCaptureSession = session;
captureRequestBuilder.set(
CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
if(isTorchOn){
captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
}
cameraCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), null, null);
if (onSuccessCallback != null) {
onSuccessCallback.run();
Expand Down Expand Up @@ -421,11 +423,13 @@ public void resumeVideoRecording(@NonNull final Result result) {
}

public void startPreview() throws CameraAccessException {
isTorchOn = false;
createCaptureSession(CameraDevice.TEMPLATE_PREVIEW, pictureImageReader.getSurface());
}

public void startPreviewWithImageStream(EventChannel imageStreamChannel)
public void startPreviewWithImageStream(EventChannel imageStreamChannel, boolean useTorch)
throws CameraAccessException {
isTorchOn = useTorch;
createCaptureSession(CameraDevice.TEMPLATE_RECORD, imageStreamReader.getSurface());

imageStreamChannel.setStreamHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,17 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull final Result result)
case "startImageStream":
{
try {
camera.startPreviewWithImageStream(imageStreamChannel);
camera.startPreviewWithImageStream(imageStreamChannel, false);
result.success(null);
} catch (Exception e) {
handleException(e, result);
}
break;
}
case "startImageStreamWithTorch":
{
try {
camera.startPreviewWithImageStream(imageStreamChannel, true);
result.success(null);
} catch (Exception e) {
handleException(e, result);
Expand Down
4 changes: 4 additions & 0 deletions packages/camera/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
children: <Widget>[
_cameraTogglesRowWidget(),
_thumbnailWidget(),
FlatButton(
onPressed: () => controller.startImageStreamWithTorch((image) => print(imagePath)),
child: Text('Stream with Torch'),
)
],
),
),
Expand Down
49 changes: 49 additions & 0 deletions packages/camera/lib/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,55 @@ class CameraController extends ValueNotifier<CameraValue> {
);
}

/// Start streaming images from platform camera with torch on.
///
/// Settings for capturing images on iOS and Android is set to always use the
/// latest image available from the camera and will drop all other images.
///
/// When running continuously with [CameraPreview] widget, this function runs
/// best with [ResolutionPreset.low]. Running on [ResolutionPreset.high] can
/// have significant frame rate drops for [CameraPreview] on lower end
/// devices.
///
/// Throws a [CameraException] if image streaming or video recording has
/// already started.
// TODO(bmparr): Add settings for resolution and fps.
Future<void> startImageStreamWithTorch(onLatestImageAvailable onAvailable) async {
if (!value.isInitialized || _isDisposed) {
throw CameraException(
'Uninitialized CameraController',
'startImageStream was called on uninitialized CameraController.',
);
}
if (value.isRecordingVideo) {
throw CameraException(
'A video recording is already started.',
'startImageStream was called while a video is being recorded.',
);
}
if (value.isStreamingImages) {
throw CameraException(
'A camera has started streaming images.',
'startImageStream was called while a camera was streaming images.',
);
}

try {
await _channel.invokeMethod<void>('startImageStreamWithTorch');
value = value.copyWith(isStreamingImages: true);
} on PlatformException catch (e) {
throw CameraException(e.code, e.message);
}
const EventChannel cameraEventChannel =
EventChannel('plugins.flutter.io/camera/imageStream');
_imageStreamSubscription =
cameraEventChannel.receiveBroadcastStream().listen(
(dynamic imageData) {
onAvailable(CameraImage._fromPlatformData(imageData));
},
);
}

/// Stop streaming images from platform camera.
///
/// Throws a [CameraException] if image streaming was not started or video
Expand Down