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

[camera_platform_interface] Added imageFormatGroup to initialize #3364

Merged
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.3.0

- Introduces an option to set the image format when initializing.

## 1.2.0

- Added interface to support automatic exposure.
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/image_format_group.dart';
import 'package:camera_platform_interface/src/utils/utils.dart';
import 'package:cross_file/cross_file.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -76,7 +77,8 @@ class MethodChannelCamera extends CameraPlatform {
}

@override
Future<void> initializeCamera(int cameraId) {
Future<void> initializeCamera(int cameraId,
{ImageFormatGroup imageFormatGroup}) {
_channels.putIfAbsent(cameraId, () {
final channel = MethodChannel('flutter.io/cameraPlugin/camera$cameraId');
channel.setMethodCallHandler(
Expand All @@ -94,6 +96,7 @@ class MethodChannelCamera extends CameraPlatform {
'initialize',
<String, dynamic>{
'cameraId': cameraId,
'imageFormatGroup': imageFormatGroup.name(),
},
);

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/image_format_group.dart';
import 'package:cross_file/cross_file.dart';
import 'package:flutter/widgets.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
Expand Down Expand Up @@ -54,7 +55,12 @@ abstract class CameraPlatform extends PlatformInterface {
}

/// Initializes the camera on the device.
Future<void> initializeCamera(int cameraId) {
///
/// [imageFormatGroup] is used to specify the image formatting used.
/// On Android this defaults to ImageFormat.YUV_420_888 and applies only to the imageStream.
/// On iOS this defaults to kCVPixelFormatType_32BGRA.
Future<void> initializeCamera(int cameraId,
{ImageFormatGroup imageFormatGroup}) {
throw UnimplementedError('initializeCamera() is not implemented.');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/// Group of image formats that are comparable across Android and iOS platforms.
enum ImageFormatGroup {
/// The image format does not fit into any specific group.
unknown,

/// Multi-plane YUV 420 format.
///
/// This format is a generic YCbCr format, capable of describing any 4:2:0
/// chroma-subsampled planar or semiplanar buffer (but not fully interleaved),
/// with 8 bits per color sample.
///
/// On Android, this is `android.graphics.ImageFormat.YUV_420_888`. See
/// https://developer.android.com/reference/android/graphics/ImageFormat.html#YUV_420_888
///
/// On iOS, this is `kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange`. See
/// https://developer.apple.com/documentation/corevideo/1563591-pixel_format_identifiers/kcvpixelformattype_420ypcbcr8biplanarvideorange?language=objc
yuv420,

/// 32-bit BGRA.
///
/// On iOS, this is `kCVPixelFormatType_32BGRA`. See
/// https://developer.apple.com/documentation/corevideo/1563591-pixel_format_identifiers/kcvpixelformattype_32bgra?language=objc
bgra8888,

/// 32-big RGB image encoded into JPEG bytes.
///
/// On Android, this is `android.graphics.ImageFormat.JPEG`. See
/// https://developer.android.com/reference/android/graphics/ImageFormat#JPEG
jpeg,
}

/// Extension on [ImageFormatGroup] to stringify the enum
extension ImageFormatGroupName on ImageFormatGroup {
/// returns a String value for [ImageFormatGroup]
/// returns 'unknown' if platform is not supported
/// or if [ImageFormatGroup] is not supported for the platform
String name() {
switch (this) {
case ImageFormatGroup.bgra8888:
return 'bgra8888';
case ImageFormatGroup.yuv420:
return 'yuv420';
case ImageFormatGroup.jpeg:
return 'jpeg';
case ImageFormatGroup.unknown:
default:
return 'unknown';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export 'camera_description.dart';
export 'resolution_preset.dart';
export 'camera_exception.dart';
export 'flash_mode.dart';
export 'image_format_group.dart';
export 'exposure_mode.dart';
4 changes: 2 additions & 2 deletions 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.2.0
version: 1.3.0

dependencies:
flutter:
Expand All @@ -21,5 +21,5 @@ dev_dependencies:
pedantic: ^1.8.0

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.7.0 <3.0.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constraint is updated to be able to use extension methods

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should already be a constraint since the flutter version minimum is 1.22.0

flutter: ">=1.22.0"
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ void main() {
MethodChannelMock cameraMockChannel = MethodChannelMock(
channelName: 'plugins.flutter.io/camera',
methods: {
'create': {'cameraId': 1}
'create': {
'cameraId': 1,
'imageFormatGroup': 'unknown',
}
});
final camera = MethodChannelCamera();

Expand Down Expand Up @@ -108,7 +111,10 @@ void main() {
MethodChannelMock cameraMockChannel = MethodChannelMock(
channelName: 'plugins.flutter.io/camera',
methods: {
'create': {'cameraId': 1},
'create': {
'cameraId': 1,
'imageFormatGroup': 'unknown',
},
'initialize': null
});
final camera = MethodChannelCamera();
Expand Down Expand Up @@ -136,6 +142,7 @@ void main() {
'initialize',
arguments: {
'cameraId': 1,
'imageFormatGroup': 'unknown',
},
),
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:camera_platform_interface/src/types/types.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('$ImageFormatGroup tests', () {
test('ImageFormatGroupName extension returns correct values', () {
expect(ImageFormatGroup.bgra8888.name(), 'bgra8888');
expect(ImageFormatGroup.yuv420.name(), 'yuv420');
expect(ImageFormatGroup.jpeg.name(), 'jpeg');
expect(ImageFormatGroup.unknown.name(), 'unknown');
});
});
}