Skip to content

Commit 7b07f0f

Browse files
danielroekmvanbeusekom
authored andcommitted
[camera_platform_interface] Added imageFormatGroup to initialize (flutter#3364)
* Added imageFormatGroup to initialize * Apply suggestions from code review Co-authored-by: Maurits van Beusekom <[email protected]> * Added period to sentence * Moved ImageFormatGroup to platform_interface; Added extension to convert ImageFormatGroup to name; Changed int to ImageFormatGroup for initializeCamera * Fixed test * Separated Android and iOS in name extension * Clarified returns on name extension * Export image_format_group.dart in types.dart * Changed enum values to lowercase * Added ImageFormatGroup test * Fixed formatting * Removed target platform switch. * Fixed formatting Co-authored-by: Maurits van Beusekom <[email protected]>
1 parent 7422918 commit 7b07f0f

File tree

8 files changed

+91
-8
lines changed

8 files changed

+91
-8
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.3.0
2+
3+
- Introduces an option to set the image format when initializing.
4+
15
## 1.2.0
26

37
- Added interface to support automatic exposure.

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

Lines changed: 4 additions & 1 deletion
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/image_format_group.dart';
910
import 'package:camera_platform_interface/src/utils/utils.dart';
1011
import 'package:cross_file/cross_file.dart';
1112
import 'package:flutter/services.dart';
@@ -76,7 +77,8 @@ class MethodChannelCamera extends CameraPlatform {
7677
}
7778

7879
@override
79-
Future<void> initializeCamera(int cameraId) {
80+
Future<void> initializeCamera(int cameraId,
81+
{ImageFormatGroup imageFormatGroup}) {
8082
_channels.putIfAbsent(cameraId, () {
8183
final channel = MethodChannel('flutter.io/cameraPlugin/camera$cameraId');
8284
channel.setMethodCallHandler(
@@ -94,6 +96,7 @@ class MethodChannelCamera extends CameraPlatform {
9496
'initialize',
9597
<String, dynamic>{
9698
'cameraId': cameraId,
99+
'imageFormatGroup': imageFormatGroup.name(),
97100
},
98101
);
99102

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

Lines changed: 7 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/image_format_group.dart';
1112
import 'package:cross_file/cross_file.dart';
1213
import 'package:flutter/widgets.dart';
1314
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
@@ -54,7 +55,12 @@ abstract class CameraPlatform extends PlatformInterface {
5455
}
5556

5657
/// Initializes the camera on the device.
57-
Future<void> initializeCamera(int cameraId) {
58+
///
59+
/// [imageFormatGroup] is used to specify the image formatting used.
60+
/// On Android this defaults to ImageFormat.YUV_420_888 and applies only to the imageStream.
61+
/// On iOS this defaults to kCVPixelFormatType_32BGRA.
62+
Future<void> initializeCamera(int cameraId,
63+
{ImageFormatGroup imageFormatGroup}) {
5864
throw UnimplementedError('initializeCamera() is not implemented.');
5965
}
6066

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/// Group of image formats that are comparable across Android and iOS platforms.
2+
enum ImageFormatGroup {
3+
/// The image format does not fit into any specific group.
4+
unknown,
5+
6+
/// Multi-plane YUV 420 format.
7+
///
8+
/// This format is a generic YCbCr format, capable of describing any 4:2:0
9+
/// chroma-subsampled planar or semiplanar buffer (but not fully interleaved),
10+
/// with 8 bits per color sample.
11+
///
12+
/// On Android, this is `android.graphics.ImageFormat.YUV_420_888`. See
13+
/// https://developer.android.com/reference/android/graphics/ImageFormat.html#YUV_420_888
14+
///
15+
/// On iOS, this is `kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange`. See
16+
/// https://developer.apple.com/documentation/corevideo/1563591-pixel_format_identifiers/kcvpixelformattype_420ypcbcr8biplanarvideorange?language=objc
17+
yuv420,
18+
19+
/// 32-bit BGRA.
20+
///
21+
/// On iOS, this is `kCVPixelFormatType_32BGRA`. See
22+
/// https://developer.apple.com/documentation/corevideo/1563591-pixel_format_identifiers/kcvpixelformattype_32bgra?language=objc
23+
bgra8888,
24+
25+
/// 32-big RGB image encoded into JPEG bytes.
26+
///
27+
/// On Android, this is `android.graphics.ImageFormat.JPEG`. See
28+
/// https://developer.android.com/reference/android/graphics/ImageFormat#JPEG
29+
jpeg,
30+
}
31+
32+
/// Extension on [ImageFormatGroup] to stringify the enum
33+
extension ImageFormatGroupName on ImageFormatGroup {
34+
/// returns a String value for [ImageFormatGroup]
35+
/// returns 'unknown' if platform is not supported
36+
/// or if [ImageFormatGroup] is not supported for the platform
37+
String name() {
38+
switch (this) {
39+
case ImageFormatGroup.bgra8888:
40+
return 'bgra8888';
41+
case ImageFormatGroup.yuv420:
42+
return 'yuv420';
43+
case ImageFormatGroup.jpeg:
44+
return 'jpeg';
45+
case ImageFormatGroup.unknown:
46+
default:
47+
return 'unknown';
48+
}
49+
}
50+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export 'camera_description.dart';
66
export 'resolution_preset.dart';
77
export 'camera_exception.dart';
88
export 'flash_mode.dart';
9+
export 'image_format_group.dart';
910
export 'wb_mode.dart';
1011
export 'iso_mode.dart';
1112
export 'exposure_mode.dart';

packages/camera/camera_platform_interface/pubspec.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ 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.2.0
7-
version: 1.1.0
8-
version: 1.0.5
6+
version: 1.3.0
7+
98

109
dependencies:
1110
flutter:
@@ -23,5 +22,5 @@ dev_dependencies:
2322
pedantic: ^1.8.0
2423

2524
environment:
26-
sdk: ">=2.1.0 <3.0.0"
25+
sdk: ">=2.7.0 <3.0.0"
2726
flutter: ">=1.22.0"

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ void main() {
2525
MethodChannelMock cameraMockChannel = MethodChannelMock(
2626
channelName: 'plugins.flutter.io/camera',
2727
methods: {
28-
'create': {'cameraId': 1}
28+
'create': {
29+
'cameraId': 1,
30+
'imageFormatGroup': 'unknown',
31+
}
2932
});
3033
final camera = MethodChannelCamera();
3134

@@ -108,7 +111,10 @@ void main() {
108111
MethodChannelMock cameraMockChannel = MethodChannelMock(
109112
channelName: 'plugins.flutter.io/camera',
110113
methods: {
111-
'create': {'cameraId': 1},
114+
'create': {
115+
'cameraId': 1,
116+
'imageFormatGroup': 'unknown',
117+
},
112118
'initialize': null
113119
});
114120
final camera = MethodChannelCamera();
@@ -138,6 +144,7 @@ void main() {
138144
'initialize',
139145
arguments: {
140146
'cameraId': 1,
147+
'imageFormatGroup': 'unknown',
141148
},
142149
),
143150
]);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'package:camera_platform_interface/src/types/types.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
group('$ImageFormatGroup tests', () {
6+
test('ImageFormatGroupName extension returns correct values', () {
7+
expect(ImageFormatGroup.bgra8888.name(), 'bgra8888');
8+
expect(ImageFormatGroup.yuv420.name(), 'yuv420');
9+
expect(ImageFormatGroup.jpeg.name(), 'jpeg');
10+
expect(ImageFormatGroup.unknown.name(), 'unknown');
11+
});
12+
});
13+
}

0 commit comments

Comments
 (0)