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

[camera] Add zoom support to platform interface #3312

Merged
merged 4 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0.2

- Added interface methods to support zoom features.

## 1.0.1

- Added interface methods for setting flash mode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,33 @@ class MethodChannelCamera extends CameraPlatform {
},
);

@override
Future<double> getMaxZoomLevel(int cameraId) => _channel.invokeMethod<double>(
'getMaxZoomLevel',
<String, dynamic>{'cameraId': cameraId},
);

@override
Future<double> getMinZoomLevel(int cameraId) => _channel.invokeMethod<double>(
'getMinZoomLevel',
<String, dynamic>{'cameraId': cameraId},
);

@override
Future<void> setZoomLevel(int cameraId, double zoom) async {
try {
await _channel.invokeMethod<double>(
'setZoomLevel',
<String, dynamic>{
'cameraId': cameraId,
'zoom': zoom,
},
);
} on PlatformException catch (e) {
throw CameraException(e.code, e.message);
}
}

@override
Widget buildPreview(int cameraId) {
return Texture(textureId: cameraId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,25 @@ abstract class CameraPlatform extends PlatformInterface {
throw UnimplementedError('setFlashMode() is not implemented.');
}

/// Gets the maximum supported zoom level for the selected camera.
Future<double> getMaxZoomLevel(int cameraId) {
throw UnimplementedError('getMaxZoomLevel() is not implemented.');
}

/// Gets the minimum supported zoom level for the selected camera.
Future<double> getMinZoomLevel(int cameraId) {
throw UnimplementedError('getMinZoomLevel() is not implemented.');
}

/// Set the zoom level for the selected camera.
///
/// The supplied [zoom] value should be between 1.0 and the maximum supported
/// zoom level returned by the `getMaxZoomLevel`. Throws an `CameraException`
/// when an illegal zoom level is supplied.
Future<void> setZoomLevel(int cameraId, double zoom) {
throw UnimplementedError('setZoomLevel() is not implemented.');
}

/// Returns a widget showing a live camera preview.
Widget buildPreview(int cameraId) {
throw UnimplementedError('buildView() has not been implemented.');
Expand Down
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.0.1
version: 1.0.2

dependencies:
flutter:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,45 @@ void main() {
throwsUnimplementedError,
);
});

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

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

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,84 @@ void main() {
expect(() => camera.handleMethodCall(MethodCall('unknown_method'), 1),
throwsA(isA<MissingPluginException>()));
});

test('Should get the max zoom level', () async {
// Arrange
MethodChannelMock channel = MethodChannelMock(
channelName: 'plugins.flutter.io/camera',
methods: {'getMaxZoomLevel': 10.0},
);

// Act
final maxZoomLevel = await camera.getMaxZoomLevel(cameraId);

// Assert
expect(maxZoomLevel, 10.0);
expect(channel.log, <Matcher>[
isMethodCall('getMaxZoomLevel', arguments: {
'cameraId': cameraId,
}),
]);
});

test('Should get the min zoom level', () async {
// Arrange
MethodChannelMock channel = MethodChannelMock(
channelName: 'plugins.flutter.io/camera',
methods: {'getMinZoomLevel': 1.0},
);

// Act
final maxZoomLevel = await camera.getMinZoomLevel(cameraId);

// Assert
expect(maxZoomLevel, 1.0);
expect(channel.log, <Matcher>[
isMethodCall('getMinZoomLevel', arguments: {
'cameraId': cameraId,
}),
]);
});

test('Should set the zoom level', () async {
// Arrange
MethodChannelMock channel = MethodChannelMock(
channelName: 'plugins.flutter.io/camera',
methods: {'setZoomLevel': null},
);

// Act
await camera.setZoomLevel(cameraId, 2.0);

// Assert
expect(channel.log, <Matcher>[
isMethodCall('setZoomLevel',
arguments: {'cameraId': cameraId, 'zoom': 2.0}),
]);
});

test('Should throw CameraException when illegal zoom level is supplied',
() async {
// Arrange
MethodChannelMock(
channelName: 'plugins.flutter.io/camera',
methods: {
'setZoomLevel': PlatformException(
code: 'ZOOM_ERROR',
message: 'Illegal zoom error',
details: null,
)
},
);

// Act & assert
expect(
() => camera.setZoomLevel(cameraId, -1.0),
throwsA(isA<CameraException>()
.having((e) => e.code, 'code', 'ZOOM_ERROR')
.having((e) => e.description, 'description',
'Illegal zoom error')));
});
});
});
}