Skip to content

Commit

Permalink
[camera_platform_interface] Add platform interface methods for settin…
Browse files Browse the repository at this point in the history
…g 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]>
  • Loading branch information
2 people authored and Minyewoo committed Jan 12, 2021
1 parent 374ba90 commit db66c37
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 21 deletions.
4 changes: 4 additions & 0 deletions packages/camera/camera_platform_interface/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.4.0

- Added interface methods to support auto focus.

## 1.3.0

- Introduces an option to set the image format when initializing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:camera_platform_interface/src/types/focus_mode.dart';

import '../../camera_platform_interface.dart';

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

/// The default exposure mode
final ExposureMode exposureMode;

/// The default focus mode
final FocusMode focusMode;


final IsoMode isoMode;
final WbMode wbMode;
/// Whether setting exposure points is supported.
final bool exposurePointSupported;

/// Whether setting focus points is supported.
final bool focusPointSupported;

/// Build a CameraInitialized event triggered from the camera represented by
/// `cameraId`.
///
Expand All @@ -63,8 +73,9 @@ class CameraInitializedEvent extends CameraEvent {
CameraInitializedEvent(
int cameraId,
this.previewWidth,
this.previewHeight,
this.previewHeight, [
this.exposureMode,
this.focusMode,
this.exposurePointSupported, this.isoMode, this.wbMode,
) : super(cameraId);

Expand All @@ -74,9 +85,11 @@ class CameraInitializedEvent extends CameraEvent {
: previewWidth = json['previewWidth'],
previewHeight = json['previewHeight'],
exposureMode = deserializeExposureMode(json['exposureMode']),
exposurePointSupported = json['exposurePointSupported'] ?? false,
focusMode = deserializeFocusMode(json['focusMode']),
focusPointSupported = json['focusPointSupported'] ?? false,
isoMode = deserializeIsoMode(json['isoMode']),
wbMode = deserializeWbMode(json['wbMode']),
exposurePointSupported = json['exposurePointSupported'],
super(json['cameraId']);

/// Converts the [CameraInitializedEvent] instance into a [Map] instance that
Expand All @@ -89,6 +102,8 @@ class CameraInitializedEvent extends CameraEvent {
'isoMode' : serializeIsoMode(isoMode),
'wbMode' : serializeWbMode(wbMode),
'exposurePointSupported': exposurePointSupported,
'focusMode': serializeFocusMode(focusMode),
'focusPointSupported': focusPointSupported,
};

@override
Expand All @@ -100,6 +115,8 @@ class CameraInitializedEvent extends CameraEvent {
previewWidth == other.previewWidth &&
previewHeight == other.previewHeight &&
exposureMode == other.exposureMode &&
focusMode == other.focusMode &&
focusPointSupported == other.focusPointSupported;
isoMode == other.isoMode &&
wbMode == other.wbMode &&
exposurePointSupported == other.exposurePointSupported;
Expand All @@ -110,6 +127,8 @@ class CameraInitializedEvent extends CameraEvent {
previewWidth.hashCode ^
previewHeight.hashCode ^
exposureMode.hashCode ^
focusMode.hashCode ^
focusPointSupported.hashCode;
isoMode.hashCode ^
wbMode.hashCode ^
exposurePointSupported.hashCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:math';

import 'package:camera_platform_interface/camera_platform_interface.dart';
import 'package:camera_platform_interface/src/types/focus_mode.dart';
import 'package:camera_platform_interface/src/types/image_format_group.dart';
import 'package:camera_platform_interface/src/utils/utils.dart';
import 'package:cross_file/cross_file.dart';
Expand Down Expand Up @@ -259,6 +260,31 @@ class MethodChannelCamera extends CameraPlatform {
},
);

@override
Future<void> setFocusMode(int cameraId, FocusMode mode) =>
_channel.invokeMethod<void>(
'setFocusMode',
<String, dynamic>{
'cameraId': cameraId,
'mode': serializeFocusMode(mode),
},
);

@override
Future<void> setFocusPoint(int cameraId, Point<double> point) {
assert(point == null || point.x >= 0 && point.x <= 1);
assert(point == null || point.y >= 0 && point.y <= 1);
return _channel.invokeMethod<void>(
'setFocusPoint',
<String, dynamic>{
'cameraId': cameraId,
'reset': point == null,
'x': point?.x,
'y': point?.y,
},
);
}

@override
Future<void> setIsoMode(int cameraId, IsoMode mode) =>
_channel.invokeMethod<void>(
Expand Down Expand Up @@ -378,6 +404,8 @@ class MethodChannelCamera extends CameraPlatform {
call.arguments['previewHeight'],
deserializeExposureMode(call.arguments['exposureMode']),
call.arguments['exposurePointSupported'],
deserializeFocusMode(call.arguments['focusMode']),
call.arguments['focusPointSupported'],
deserializeIsoMode(call.arguments['isoMode']),
deserializeWbMode(call.arguments['wbMode']),
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:math';
import 'package:camera_platform_interface/camera_platform_interface.dart';
import 'package:camera_platform_interface/src/method_channel/method_channel_camera.dart';
import 'package:camera_platform_interface/src/types/exposure_mode.dart';
import 'package:camera_platform_interface/src/types/focus_mode.dart';
import 'package:camera_platform_interface/src/types/image_format_group.dart';
import 'package:cross_file/cross_file.dart';
import 'package:flutter/widgets.dart';
Expand Down Expand Up @@ -133,7 +134,7 @@ abstract class CameraPlatform extends PlatformInterface {
throw UnimplementedError('setExposureMode() is not implemented.');
}

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

/// Sets the focus mode for taking pictures.
Future<void> setFocusMode(int cameraId, FocusMode mode) {
throw UnimplementedError('setFocusMode() is not implemented.');
}

/// Sets the focus point for automatically determining the focus values.
Future<void> setFocusPoint(int cameraId, Point<double> point) {
throw UnimplementedError('setFocusPoint() is not implemented.');
}

Future<void> setIsoMode(int cameraId, IsoMode mode) {
throw UnimplementedError('setIsoMode() is not implemented.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum ExposureMode {

/// Returns the exposure mode as a String.
String serializeExposureMode(ExposureMode exposureMode) {
if (exposureMode == null) return null;
switch (exposureMode) {
case ExposureMode.locked:
return 'locked';
Expand All @@ -25,6 +26,7 @@ String serializeExposureMode(ExposureMode exposureMode) {

/// Returns the exposure mode for a given String.
ExposureMode deserializeExposureMode(String str) {
if (str == null) return null;
switch (str) {
case "locked":
return ExposureMode.locked;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/// The possible focus modes that can be set for a camera.
enum FocusMode {
/// Automatically determine focus settings.
auto,

/// Lock the currently determined focus settings.
locked,
}

/// Returns the focus mode as a String.
String serializeFocusMode(FocusMode focusMode) {
if (focusMode == null) return null;
switch (focusMode) {
case FocusMode.locked:
return 'locked';
case FocusMode.auto:
return 'auto';
default:
throw ArgumentError('Unknown FocusMode value');
}
}

/// Returns the focus mode for a given String.
FocusMode deserializeFocusMode(String str) {
if (str == null) return null;
switch (str) {
case "locked":
return FocusMode.locked;
case "auto":
return FocusMode.auto;
default:
throw ArgumentError('"$str" is not a valid FocusMode value');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export 'image_format_group.dart';
export 'wb_mode.dart';
export 'iso_mode.dart';
export 'exposure_mode.dart';
export 'focus_mode.dart';
2 changes: 1 addition & 1 deletion packages/camera/camera_platform_interface/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: A common platform interface for the camera plugin.
homepage: https://github.com/flutter/plugins/tree/master/packages/camera/camera_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 1.3.0
version: 1.4.0


dependencies:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,32 @@ void main() {
);
});

test(
'Default implementation of setFocusMode() should throw unimplemented error',
() {
// Arrange
final cameraPlatform = ExtendsCameraPlatform();

// Act & Assert
expect(
() => cameraPlatform.setFocusMode(1, null),
throwsUnimplementedError,
);
});

test(
'Default implementation of setFocusPoint() should throw unimplemented error',
() {
// Arrange
final cameraPlatform = ExtendsCameraPlatform();

// Act & Assert
expect(
() => cameraPlatform.setFocusPoint(1, null),
throwsUnimplementedError,
);
});

test(
'Default implementation of startVideoRecording() should throw unimplemented error',
() {
Expand Down
Loading

0 comments on commit db66c37

Please sign in to comment.