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
@@ -1,3 +1,7 @@
## 1.1.0

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

## 1.0.4

- Added the torch option to the FlashMode enum, which when implemented indicates the flash light should be turned on continuously.
Expand Down
Expand Up @@ -5,6 +5,7 @@
import 'dart:async';

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 @@ -75,7 +76,7 @@ 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 @@ -93,6 +94,7 @@ class MethodChannelCamera extends CameraPlatform {
'initialize',
<String, dynamic>{
'cameraId': cameraId,
'imageFormatGroup': imageFormatGroup.name(),
},
);

Expand Down
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';

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/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 @@ -52,7 +53,11 @@ 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
@@ -0,0 +1,51 @@
// TODO:(bmparr) Turn [ImageFormatGroup] to a class with int values.

/// 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]
String name() {
switch (this) {
case ImageFormatGroup.jpeg:
return 'JPEG';
case ImageFormatGroup.yuv420:
return 'YUV420';
case ImageFormatGroup.bgra8888:
return 'BGRA888';
case ImageFormatGroup.unknown:
return 'UNKNOWN';
default:
return 'UNKNOWN';
}
}
}
4 changes: 2 additions & 2 deletions packages/camera/camera_platform_interface/pubspec.yaml
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.4
version: 1.1.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"
Expand Up @@ -24,7 +24,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 @@ -107,7 +110,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 All @@ -130,6 +136,7 @@ void main() {
'initialize',
arguments: {
'cameraId': 1,
'imageFormatGroup': 'UNKNOWN',
},
),
]);
Expand Down