Skip to content

Commit

Permalink
[camerax] Add flash configuration for image capture (flutter#3800)
Browse files Browse the repository at this point in the history
Implements off, auto, and always flash configurations for image capture.

Part of flutter#120715.
  • Loading branch information
camsim99 committed Jul 17, 2023
1 parent dc41ccc commit 228c2b1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
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+10

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

## 0.5.0+9

* Marks all Dart-wrapped Android native classes as `@immutable`.
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+9
version: 0.5.0+10

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

0 comments on commit 228c2b1

Please sign in to comment.