Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[camerax] Add flash configuration for image capture #3800

Merged
merged 6 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/camera/camera_android_camerax/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.0+9

* Implements off, auto, and always flash mode configurations for image capture.

## 0.5.0+8

* Fixes unawaited_futures violations.
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera_android_camerax/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Any specified `ResolutionPreset` wll go unused in favor of CameraX defaults and

### Flash mode configuration \[[Issue #120715][120715]\]

`setFlashMode` is unimplemented.
Calling `setFlashMode` with mode `FlashMode.torch` currently does nothing.

### Exposure mode, point, & offset configuration \[[Issue #120468][120468]\]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ class AndroidCameraCameraX extends CameraPlatform {
@visibleForTesting
ImageCapture? imageCapture;

/// The flash mode currently configured for [imageCapture].
int? _currentFlashMode;

/// The [ImageAnalysis] instance that can be configured to analyze individual
/// frames.
ImageAnalysis? imageAnalysis;
Expand Down Expand Up @@ -459,13 +462,32 @@ class AndroidCameraCameraX extends CameraPlatform {
/// [cameraId] is not used.
@override
Future<XFile> takePicture(int cameraId) async {
// TODO(camsim99): Add support for flash mode configuration.
// https://github.com/flutter/flutter/issues/120715
if (_currentFlashMode != null) {
await imageCapture!.setFlashMode(_currentFlashMode!);
}
final String picturePath = await imageCapture!.takePicture();

return XFile(picturePath);
}

/// Sets the flash mode for the selected camera.
@override
Future<void> setFlashMode(int cameraId, FlashMode mode) async {
switch (mode) {
case FlashMode.off:
_currentFlashMode = ImageCapture.flashModeOff;
break;
case FlashMode.auto:
_currentFlashMode = ImageCapture.flashModeAuto;
break;
case FlashMode.always:
_currentFlashMode = ImageCapture.flashModeOn;
break;
case FlashMode.torch:
// TODO(camsim99): Implement torch mode when CameraControl is wrapped.
break;
}
}

/// Configures and starts a video recording. Returns silently without doing
/// anything if there is currently an active recording.
@override
Expand Down
2 changes: 1 addition & 1 deletion packages/camera/camera_android_camerax/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_android_camerax
description: Android implementation of the camera plugin using the CameraX library.
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_android_camerax
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.5.0+8
version: 0.5.0+9

environment:
sdk: ">=2.19.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,6 @@ void main() {
final AndroidCameraCameraX camera = AndroidCameraCameraX();
const String testPicturePath = 'test/absolute/path/to/picture';

camera.processCameraProvider = MockProcessCameraProvider();
camera.cameraSelector = MockCameraSelector();
camera.imageCapture = MockImageCapture();

when(camera.imageCapture!.takePicture())
Expand All @@ -791,6 +789,41 @@ void main() {
expect(imageFile.path, equals(testPicturePath));
});

test('setFlashMode configures ImageCapture with expected flash mode',
() async {
final AndroidCameraCameraX camera = AndroidCameraCameraX();
const int cameraId = 22;

camera.imageCapture = MockImageCapture();

for (final FlashMode flashMode in FlashMode.values) {
await camera.setFlashMode(cameraId, flashMode);

int? expectedFlashMode;
switch (flashMode) {
case FlashMode.off:
expectedFlashMode = ImageCapture.flashModeOff;
break;
case FlashMode.auto:
expectedFlashMode = ImageCapture.flashModeAuto;
break;
case FlashMode.always:
expectedFlashMode = ImageCapture.flashModeOn;
break;
case FlashMode.torch:
// TODO(camsim99): Test torch mode when implemented.
break;
}

if (expectedFlashMode == null) {
continue;
}

await camera.takePicture(cameraId);
verify(camera.imageCapture!.setFlashMode(expectedFlashMode));
}
});

test('getMinExposureOffset returns expected exposure offset', () async {
final AndroidCameraCameraX camera = AndroidCameraCameraX();
final MockCameraInfo mockCameraInfo = MockCameraInfo();
Expand Down