Skip to content

Commit db66c37

Browse files
BeMacizedmvanbeusekom
authored andcommitted
[camera_platform_interface] Add platform interface methods for setting auto focus. (flutter#3369)
* Added platform interface methods for setting auto exposure. * Added platform interface methods for setting auto exposure. * Remove workspace files * Added auto exposure implementations for Android and iOS * Added platform interface methods for managing auto focus. * Formatted code * Export focus mode * Update platform interface for changes to autofocus methods * Revert "Update platform interface for changes to autofocus methods" This reverts commit bdeed1d. * iOS fix for setting the exposure point * Removed unnecessary check * Updated changelog and pubspec.yaml * Update platform interface dependency * Implement PR feedback * Restore test * Revert test change * Update camera pubspec * Update platform interface to prevent breaking changes with current master Co-authored-by: Maurits van Beusekom <[email protected]>
1 parent 374ba90 commit db66c37

File tree

12 files changed

+283
-21
lines changed

12 files changed

+283
-21
lines changed

packages/camera/camera_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.4.0
2+
3+
- Added interface methods to support auto focus.
4+
15
## 1.3.0
26

37
- Introduces an option to set the image format when initializing.

packages/camera/camera_platform_interface/lib/src/events/camera_event.dart

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:camera_platform_interface/src/types/focus_mode.dart';
6+
57
import '../../camera_platform_interface.dart';
68

79
/// Generic Event coming from the native side of Camera.
@@ -50,11 +52,19 @@ class CameraInitializedEvent extends CameraEvent {
5052

5153
/// The default exposure mode
5254
final ExposureMode exposureMode;
55+
56+
/// The default focus mode
57+
final FocusMode focusMode;
58+
59+
5360
final IsoMode isoMode;
5461
final WbMode wbMode;
5562
/// Whether setting exposure points is supported.
5663
final bool exposurePointSupported;
5764

65+
/// Whether setting focus points is supported.
66+
final bool focusPointSupported;
67+
5868
/// Build a CameraInitialized event triggered from the camera represented by
5969
/// `cameraId`.
6070
///
@@ -63,8 +73,9 @@ class CameraInitializedEvent extends CameraEvent {
6373
CameraInitializedEvent(
6474
int cameraId,
6575
this.previewWidth,
66-
this.previewHeight,
76+
this.previewHeight, [
6777
this.exposureMode,
78+
this.focusMode,
6879
this.exposurePointSupported, this.isoMode, this.wbMode,
6980
) : super(cameraId);
7081

@@ -74,9 +85,11 @@ class CameraInitializedEvent extends CameraEvent {
7485
: previewWidth = json['previewWidth'],
7586
previewHeight = json['previewHeight'],
7687
exposureMode = deserializeExposureMode(json['exposureMode']),
88+
exposurePointSupported = json['exposurePointSupported'] ?? false,
89+
focusMode = deserializeFocusMode(json['focusMode']),
90+
focusPointSupported = json['focusPointSupported'] ?? false,
7791
isoMode = deserializeIsoMode(json['isoMode']),
7892
wbMode = deserializeWbMode(json['wbMode']),
79-
exposurePointSupported = json['exposurePointSupported'],
8093
super(json['cameraId']);
8194

8295
/// Converts the [CameraInitializedEvent] instance into a [Map] instance that
@@ -89,6 +102,8 @@ class CameraInitializedEvent extends CameraEvent {
89102
'isoMode' : serializeIsoMode(isoMode),
90103
'wbMode' : serializeWbMode(wbMode),
91104
'exposurePointSupported': exposurePointSupported,
105+
'focusMode': serializeFocusMode(focusMode),
106+
'focusPointSupported': focusPointSupported,
92107
};
93108

94109
@override
@@ -100,6 +115,8 @@ class CameraInitializedEvent extends CameraEvent {
100115
previewWidth == other.previewWidth &&
101116
previewHeight == other.previewHeight &&
102117
exposureMode == other.exposureMode &&
118+
focusMode == other.focusMode &&
119+
focusPointSupported == other.focusPointSupported;
103120
isoMode == other.isoMode &&
104121
wbMode == other.wbMode &&
105122
exposurePointSupported == other.exposurePointSupported;
@@ -110,6 +127,8 @@ class CameraInitializedEvent extends CameraEvent {
110127
previewWidth.hashCode ^
111128
previewHeight.hashCode ^
112129
exposureMode.hashCode ^
130+
focusMode.hashCode ^
131+
focusPointSupported.hashCode;
113132
isoMode.hashCode ^
114133
wbMode.hashCode ^
115134
exposurePointSupported.hashCode;

packages/camera/camera_platform_interface/lib/src/method_channel/method_channel_camera.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:async';
66
import 'dart:math';
77

88
import 'package:camera_platform_interface/camera_platform_interface.dart';
9+
import 'package:camera_platform_interface/src/types/focus_mode.dart';
910
import 'package:camera_platform_interface/src/types/image_format_group.dart';
1011
import 'package:camera_platform_interface/src/utils/utils.dart';
1112
import 'package:cross_file/cross_file.dart';
@@ -259,6 +260,31 @@ class MethodChannelCamera extends CameraPlatform {
259260
},
260261
);
261262

263+
@override
264+
Future<void> setFocusMode(int cameraId, FocusMode mode) =>
265+
_channel.invokeMethod<void>(
266+
'setFocusMode',
267+
<String, dynamic>{
268+
'cameraId': cameraId,
269+
'mode': serializeFocusMode(mode),
270+
},
271+
);
272+
273+
@override
274+
Future<void> setFocusPoint(int cameraId, Point<double> point) {
275+
assert(point == null || point.x >= 0 && point.x <= 1);
276+
assert(point == null || point.y >= 0 && point.y <= 1);
277+
return _channel.invokeMethod<void>(
278+
'setFocusPoint',
279+
<String, dynamic>{
280+
'cameraId': cameraId,
281+
'reset': point == null,
282+
'x': point?.x,
283+
'y': point?.y,
284+
},
285+
);
286+
}
287+
262288
@override
263289
Future<void> setIsoMode(int cameraId, IsoMode mode) =>
264290
_channel.invokeMethod<void>(
@@ -378,6 +404,8 @@ class MethodChannelCamera extends CameraPlatform {
378404
call.arguments['previewHeight'],
379405
deserializeExposureMode(call.arguments['exposureMode']),
380406
call.arguments['exposurePointSupported'],
407+
deserializeFocusMode(call.arguments['focusMode']),
408+
call.arguments['focusPointSupported'],
381409
deserializeIsoMode(call.arguments['isoMode']),
382410
deserializeWbMode(call.arguments['wbMode']),
383411
));

packages/camera/camera_platform_interface/lib/src/platform_interface/camera_platform.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:math';
88
import 'package:camera_platform_interface/camera_platform_interface.dart';
99
import 'package:camera_platform_interface/src/method_channel/method_channel_camera.dart';
1010
import 'package:camera_platform_interface/src/types/exposure_mode.dart';
11+
import 'package:camera_platform_interface/src/types/focus_mode.dart';
1112
import 'package:camera_platform_interface/src/types/image_format_group.dart';
1213
import 'package:cross_file/cross_file.dart';
1314
import 'package:flutter/widgets.dart';
@@ -133,7 +134,7 @@ abstract class CameraPlatform extends PlatformInterface {
133134
throw UnimplementedError('setExposureMode() is not implemented.');
134135
}
135136

136-
/// Sets the exposure point for automatically determining the exposure value.
137+
/// Sets the exposure point for automatically determining the exposure values.
137138
Future<void> setExposurePoint(int cameraId, Point<double> point) {
138139
throw UnimplementedError('setExposurePoint() is not implemented.');
139140
}
@@ -170,6 +171,16 @@ abstract class CameraPlatform extends PlatformInterface {
170171
throw UnimplementedError('setExposureOffset() is not implemented.');
171172
}
172173

174+
/// Sets the focus mode for taking pictures.
175+
Future<void> setFocusMode(int cameraId, FocusMode mode) {
176+
throw UnimplementedError('setFocusMode() is not implemented.');
177+
}
178+
179+
/// Sets the focus point for automatically determining the focus values.
180+
Future<void> setFocusPoint(int cameraId, Point<double> point) {
181+
throw UnimplementedError('setFocusPoint() is not implemented.');
182+
}
183+
173184
Future<void> setIsoMode(int cameraId, IsoMode mode) {
174185
throw UnimplementedError('setIsoMode() is not implemented.');
175186
}

packages/camera/camera_platform_interface/lib/src/types/exposure_mode.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ enum ExposureMode {
1313

1414
/// Returns the exposure mode as a String.
1515
String serializeExposureMode(ExposureMode exposureMode) {
16+
if (exposureMode == null) return null;
1617
switch (exposureMode) {
1718
case ExposureMode.locked:
1819
return 'locked';
@@ -25,6 +26,7 @@ String serializeExposureMode(ExposureMode exposureMode) {
2526

2627
/// Returns the exposure mode for a given String.
2728
ExposureMode deserializeExposureMode(String str) {
29+
if (str == null) return null;
2830
switch (str) {
2931
case "locked":
3032
return ExposureMode.locked;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// The possible focus modes that can be set for a camera.
6+
enum FocusMode {
7+
/// Automatically determine focus settings.
8+
auto,
9+
10+
/// Lock the currently determined focus settings.
11+
locked,
12+
}
13+
14+
/// Returns the focus mode as a String.
15+
String serializeFocusMode(FocusMode focusMode) {
16+
if (focusMode == null) return null;
17+
switch (focusMode) {
18+
case FocusMode.locked:
19+
return 'locked';
20+
case FocusMode.auto:
21+
return 'auto';
22+
default:
23+
throw ArgumentError('Unknown FocusMode value');
24+
}
25+
}
26+
27+
/// Returns the focus mode for a given String.
28+
FocusMode deserializeFocusMode(String str) {
29+
if (str == null) return null;
30+
switch (str) {
31+
case "locked":
32+
return FocusMode.locked;
33+
case "auto":
34+
return FocusMode.auto;
35+
default:
36+
throw ArgumentError('"$str" is not a valid FocusMode value');
37+
}
38+
}

packages/camera/camera_platform_interface/lib/src/types/types.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export 'image_format_group.dart';
1010
export 'wb_mode.dart';
1111
export 'iso_mode.dart';
1212
export 'exposure_mode.dart';
13+
export 'focus_mode.dart';

packages/camera/camera_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: A common platform interface for the camera plugin.
33
homepage: https://github.com/flutter/plugins/tree/master/packages/camera/camera_platform_interface
44
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
55
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
6-
version: 1.3.0
6+
version: 1.4.0
77

88

99
dependencies:

packages/camera/camera_platform_interface/test/camera_platform_interface_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,32 @@ void main() {
264264
);
265265
});
266266

267+
test(
268+
'Default implementation of setFocusMode() should throw unimplemented error',
269+
() {
270+
// Arrange
271+
final cameraPlatform = ExtendsCameraPlatform();
272+
273+
// Act & Assert
274+
expect(
275+
() => cameraPlatform.setFocusMode(1, null),
276+
throwsUnimplementedError,
277+
);
278+
});
279+
280+
test(
281+
'Default implementation of setFocusPoint() should throw unimplemented error',
282+
() {
283+
// Arrange
284+
final cameraPlatform = ExtendsCameraPlatform();
285+
286+
// Act & Assert
287+
expect(
288+
() => cameraPlatform.setFocusPoint(1, null),
289+
throwsUnimplementedError,
290+
);
291+
});
292+
267293
test(
268294
'Default implementation of startVideoRecording() should throw unimplemented error',
269295
() {

0 commit comments

Comments
 (0)