Skip to content

Commit

Permalink
[camera_platform_interface] Added imageFormatGroup to initialize (flu…
Browse files Browse the repository at this point in the history
…tter#3364)

* Added imageFormatGroup to initialize

* Apply suggestions from code review

Co-authored-by: Maurits van Beusekom <maurits@vnbskm.nl>

* 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 <maurits@vnbskm.nl>
  • Loading branch information
2 people authored and Minyewoo committed Jan 12, 2021
1 parent 7422918 commit 7b07f0f
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 8 deletions.
4 changes: 4 additions & 0 deletions packages/camera/camera_platform_interface/CHANGELOG.md
@@ -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
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
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
@@ -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';
}
}
}
Expand Up @@ -6,6 +6,7 @@ export 'camera_description.dart';
export 'resolution_preset.dart';
export 'camera_exception.dart';
export 'flash_mode.dart';
export 'image_format_group.dart';
export 'wb_mode.dart';
export 'iso_mode.dart';
export 'exposure_mode.dart';
7 changes: 3 additions & 4 deletions packages/camera/camera_platform_interface/pubspec.yaml
Expand Up @@ -3,9 +3,8 @@ 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.1.0
version: 1.0.5
version: 1.3.0


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

environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.7.0 <3.0.0"
flutter: ">=1.22.0"
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 @@ -138,6 +144,7 @@ void main() {
'initialize',
arguments: {
'cameraId': 1,
'imageFormatGroup': 'unknown',
},
),
]);
Expand Down
@@ -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');
});
});
}

0 comments on commit 7b07f0f

Please sign in to comment.