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

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. (#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 <maurits@baseflow.com>
  • Loading branch information
BeMacized and mvanbeusekom authored Jan 6, 2021
1 parent b64bebf commit dac4b6f
Show file tree
Hide file tree
Showing 12 changed files with 291 additions and 42 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,9 +52,15 @@ class CameraInitializedEvent extends CameraEvent {
/// The default exposure mode
final ExposureMode exposureMode;

/// The default focus mode
final FocusMode focusMode;

/// 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 @@ -61,18 +69,22 @@ class CameraInitializedEvent extends CameraEvent {
CameraInitializedEvent(
int cameraId,
this.previewWidth,
this.previewHeight,
this.previewHeight, [
this.exposureMode,
this.exposurePointSupported,
) : super(cameraId);
this.exposurePointSupported = false,
this.focusMode,
this.focusPointSupported = false,
]) : super(cameraId);

/// Converts the supplied [Map] to an instance of the [CameraInitializedEvent]
/// class.
CameraInitializedEvent.fromJson(Map<String, dynamic> json)
: previewWidth = json['previewWidth'],
previewHeight = json['previewHeight'],
exposureMode = deserializeExposureMode(json['exposureMode']),
exposurePointSupported = json['exposurePointSupported'],
exposurePointSupported = json['exposurePointSupported'] ?? false,
focusMode = deserializeFocusMode(json['focusMode']),
focusPointSupported = json['focusPointSupported'] ?? false,
super(json['cameraId']);

/// Converts the [CameraInitializedEvent] instance into a [Map] instance that
Expand All @@ -83,6 +95,8 @@ class CameraInitializedEvent extends CameraEvent {
'previewHeight': previewHeight,
'exposureMode': serializeExposureMode(exposureMode),
'exposurePointSupported': exposurePointSupported,
'focusMode': serializeFocusMode(focusMode),
'focusPointSupported': focusPointSupported,
};

@override
Expand All @@ -94,15 +108,19 @@ class CameraInitializedEvent extends CameraEvent {
previewWidth == other.previewWidth &&
previewHeight == other.previewHeight &&
exposureMode == other.exposureMode &&
exposurePointSupported == other.exposurePointSupported;
exposurePointSupported == other.exposurePointSupported &&
focusMode == other.focusMode &&
focusPointSupported == other.focusPointSupported;

@override
int get hashCode =>
super.hashCode ^
previewWidth.hashCode ^
previewHeight.hashCode ^
exposureMode.hashCode ^
exposurePointSupported.hashCode;
exposurePointSupported.hashCode ^
focusMode.hashCode ^
focusPointSupported.hashCode;
}

/// An event fired when the resolution preset of the camera has changed.
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 @@ -249,6 +250,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<double> getMaxZoomLevel(int cameraId) => _channel.invokeMethod<double>(
'getMaxZoomLevel',
Expand Down Expand Up @@ -331,6 +357,8 @@ class MethodChannelCamera extends CameraPlatform {
call.arguments['previewHeight'],
deserializeExposureMode(call.arguments['exposureMode']),
call.arguments['exposurePointSupported'],
deserializeFocusMode(call.arguments['focusMode']),
call.arguments['focusPointSupported'],
));
break;
case 'resolution_changed':
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 @@ -129,7 +130,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 @@ -166,6 +167,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.');
}

/// Gets the maximum supported zoom level for the selected camera.
Future<double> getMaxZoomLevel(int cameraId) {
throw UnimplementedError('getMaxZoomLevel() 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 @@ -8,3 +8,4 @@ export 'camera_exception.dart';
export 'flash_mode.dart';
export 'image_format_group.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:
flutter:
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 dac4b6f

Please sign in to comment.