Skip to content

Commit ef5cc73

Browse files
mvanbeusekomamantoux
authored andcommitted
[camera] Add zoom support to platform interface (flutter#3312)
* Add zoom support to platform interface * Added method to retrieve min supported zoom level * Bumped version to 1.0.2 * Fixed small typo
1 parent df1b055 commit ef5cc73

File tree

6 files changed

+168
-1
lines changed

6 files changed

+168
-1
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.0.2
2+
3+
- Added interface methods to support zoom features.
4+
15
## 1.0.1
26

37
- Added interface methods for setting flash mode.

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,33 @@ class MethodChannelCamera extends CameraPlatform {
185185
},
186186
);
187187

188+
@override
189+
Future<double> getMaxZoomLevel(int cameraId) => _channel.invokeMethod<double>(
190+
'getMaxZoomLevel',
191+
<String, dynamic>{'cameraId': cameraId},
192+
);
193+
194+
@override
195+
Future<double> getMinZoomLevel(int cameraId) => _channel.invokeMethod<double>(
196+
'getMinZoomLevel',
197+
<String, dynamic>{'cameraId': cameraId},
198+
);
199+
200+
@override
201+
Future<void> setZoomLevel(int cameraId, double zoom) async {
202+
try {
203+
await _channel.invokeMethod<double>(
204+
'setZoomLevel',
205+
<String, dynamic>{
206+
'cameraId': cameraId,
207+
'zoom': zoom,
208+
},
209+
);
210+
} on PlatformException catch (e) {
211+
throw CameraException(e.code, e.message);
212+
}
213+
}
214+
188215
@override
189216
Widget buildPreview(int cameraId) {
190217
return Texture(textureId: cameraId);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ abstract class CameraPlatform extends PlatformInterface {
113113
throw UnimplementedError('setFlashMode() is not implemented.');
114114
}
115115

116+
/// Gets the maximum supported zoom level for the selected camera.
117+
Future<double> getMaxZoomLevel(int cameraId) {
118+
throw UnimplementedError('getMaxZoomLevel() is not implemented.');
119+
}
120+
121+
/// Gets the minimum supported zoom level for the selected camera.
122+
Future<double> getMinZoomLevel(int cameraId) {
123+
throw UnimplementedError('getMinZoomLevel() is not implemented.');
124+
}
125+
126+
/// Set the zoom level for the selected camera.
127+
///
128+
/// The supplied [zoom] value should be between 1.0 and the maximum supported
129+
/// zoom level returned by the `getMaxZoomLevel`. Throws an `CameraException`
130+
/// when an illegal zoom level is supplied.
131+
Future<void> setZoomLevel(int cameraId, double zoom) {
132+
throw UnimplementedError('setZoomLevel() is not implemented.');
133+
}
134+
116135
/// Returns a widget showing a live camera preview.
117136
Widget buildPreview(int cameraId) {
118137
throw UnimplementedError('buildView() has not been implemented.');

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.0.1
6+
version: 1.0.2
77

88
dependencies:
99
flutter:

packages/camera/camera_platform_interface/test/camera_platform_interface_test.dart

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,45 @@ void main() {
224224
throwsUnimplementedError,
225225
);
226226
});
227+
228+
test(
229+
'Default implementation of getMaxZoomLevel() should throw unimplemented error',
230+
() {
231+
// Arrange
232+
final cameraPlatform = ExtendsCameraPlatform();
233+
234+
// Act & Assert
235+
expect(
236+
() => cameraPlatform.getMaxZoomLevel(1),
237+
throwsUnimplementedError,
238+
);
239+
});
240+
241+
test(
242+
'Default implementation of getMinZoomLevel() should throw unimplemented error',
243+
() {
244+
// Arrange
245+
final cameraPlatform = ExtendsCameraPlatform();
246+
247+
// Act & Assert
248+
expect(
249+
() => cameraPlatform.getMinZoomLevel(1),
250+
throwsUnimplementedError,
251+
);
252+
});
253+
254+
test(
255+
'Default implementation of setZoomLevel() should throw unimplemented error',
256+
() {
257+
// Arrange
258+
final cameraPlatform = ExtendsCameraPlatform();
259+
260+
// Act & Assert
261+
expect(
262+
() => cameraPlatform.setZoomLevel(1, 1.0),
263+
throwsUnimplementedError,
264+
);
265+
});
227266
});
228267
}
229268

packages/camera/camera_platform_interface/test/method_channel/method_channel_camera_test.dart

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,84 @@ void main() {
509509
expect(() => camera.handleMethodCall(MethodCall('unknown_method'), 1),
510510
throwsA(isA<MissingPluginException>()));
511511
});
512+
513+
test('Should get the max zoom level', () async {
514+
// Arrange
515+
MethodChannelMock channel = MethodChannelMock(
516+
channelName: 'plugins.flutter.io/camera',
517+
methods: {'getMaxZoomLevel': 10.0},
518+
);
519+
520+
// Act
521+
final maxZoomLevel = await camera.getMaxZoomLevel(cameraId);
522+
523+
// Assert
524+
expect(maxZoomLevel, 10.0);
525+
expect(channel.log, <Matcher>[
526+
isMethodCall('getMaxZoomLevel', arguments: {
527+
'cameraId': cameraId,
528+
}),
529+
]);
530+
});
531+
532+
test('Should get the min zoom level', () async {
533+
// Arrange
534+
MethodChannelMock channel = MethodChannelMock(
535+
channelName: 'plugins.flutter.io/camera',
536+
methods: {'getMinZoomLevel': 1.0},
537+
);
538+
539+
// Act
540+
final maxZoomLevel = await camera.getMinZoomLevel(cameraId);
541+
542+
// Assert
543+
expect(maxZoomLevel, 1.0);
544+
expect(channel.log, <Matcher>[
545+
isMethodCall('getMinZoomLevel', arguments: {
546+
'cameraId': cameraId,
547+
}),
548+
]);
549+
});
550+
551+
test('Should set the zoom level', () async {
552+
// Arrange
553+
MethodChannelMock channel = MethodChannelMock(
554+
channelName: 'plugins.flutter.io/camera',
555+
methods: {'setZoomLevel': null},
556+
);
557+
558+
// Act
559+
await camera.setZoomLevel(cameraId, 2.0);
560+
561+
// Assert
562+
expect(channel.log, <Matcher>[
563+
isMethodCall('setZoomLevel',
564+
arguments: {'cameraId': cameraId, 'zoom': 2.0}),
565+
]);
566+
});
567+
568+
test('Should throw CameraException when illegal zoom level is supplied',
569+
() async {
570+
// Arrange
571+
MethodChannelMock(
572+
channelName: 'plugins.flutter.io/camera',
573+
methods: {
574+
'setZoomLevel': PlatformException(
575+
code: 'ZOOM_ERROR',
576+
message: 'Illegal zoom error',
577+
details: null,
578+
)
579+
},
580+
);
581+
582+
// Act & assert
583+
expect(
584+
() => camera.setZoomLevel(cameraId, -1.0),
585+
throwsA(isA<CameraException>()
586+
.having((e) => e.code, 'code', 'ZOOM_ERROR')
587+
.having((e) => e.description, 'description',
588+
'Illegal zoom error')));
589+
});
512590
});
513591
});
514592
}

0 commit comments

Comments
 (0)