Skip to content
Merged
4 changes: 4 additions & 0 deletions packages/camera/camera_android_camerax/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.6.1

* Modifies resolution selection logic to use an `AspectRatioStrategy` for all aspect ratios supported by CameraX.

## 0.6.0+1

* Updates `README.md` to encourage developers to opt into this implementation of the camera plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import 'package:async/async.dart';
import 'package:camera_platform_interface/camera_platform_interface.dart';
import 'package:flutter/services.dart'
show DeviceOrientation, PlatformException;
import 'package:flutter/widgets.dart';
import 'package:flutter/widgets.dart'
show Size, Texture, Widget, visibleForTesting;
import 'package:stream_transform/stream_transform.dart';

import 'analyzer.dart';
import 'aspect_ratio_strategy.dart';
import 'camera.dart';
import 'camera2_camera_control.dart';
import 'camera_control.dart';
Expand Down Expand Up @@ -1150,32 +1152,45 @@ class AndroidCameraCameraX extends CameraPlatform {
ResolutionStrategy.fallbackRuleClosestLowerThenHigher;

Size? boundSize;
int? aspectRatio;
ResolutionStrategy? resolutionStrategy;
switch (preset) {
case ResolutionPreset.low:
boundSize = const Size(320, 240);
aspectRatio = AspectRatio.ratio4To3;
case ResolutionPreset.medium:
// TODO(camsim99): Handle the 3:2 aspect ratio case:
// https://github.com/flutter/flutter/issues/144363.
boundSize = const Size(720, 480);
case ResolutionPreset.high:
boundSize = const Size(1280, 720);
aspectRatio = AspectRatio.ratio16To9;
case ResolutionPreset.veryHigh:
boundSize = const Size(1920, 1080);
aspectRatio = AspectRatio.ratio16To9;
case ResolutionPreset.ultraHigh:
boundSize = const Size(3840, 2160);
aspectRatio = AspectRatio.ratio16To9;
case ResolutionPreset.max:
// Automatically set strategy to choose highest available.
resolutionStrategy =
proxy.createResolutionStrategy(highestAvailable: true);
return proxy.createResolutionSelector(resolutionStrategy);
return proxy.createResolutionSelector(
resolutionStrategy, /* AspectRatioStrategy */ null);
case null:
// If no preset is specified, default to CameraX's default behavior
// for each UseCase.
return null;
}

final AspectRatioStrategy? aspectRatioStrategy = aspectRatio == null
? null
: proxy.createAspectRatioStrategy(
aspectRatio, AspectRatioStrategy.fallbackRuleAuto);
resolutionStrategy = proxy.createResolutionStrategy(
boundSize: boundSize, fallbackRule: fallbackRule);
return proxy.createResolutionSelector(resolutionStrategy);
return proxy.createResolutionSelector(
resolutionStrategy, aspectRatioStrategy);
}

/// Returns the [QualitySelector] that maps to the specified resolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:ui' show Size;

import 'analyzer.dart';
import 'aspect_ratio_strategy.dart';
import 'camera2_camera_control.dart';
import 'camera_control.dart';
import 'camera_info.dart';
Expand Down Expand Up @@ -59,6 +60,7 @@ class CameraXProxy {
this.createCaptureRequestOptions = _createAttachedCaptureRequestOptions,
this.createMeteringPoint = _createAttachedMeteringPoint,
this.createFocusMeteringAction = _createAttachedFocusMeteringAction,
this.createAspectRatioStrategy = _createAttachedAspectRatioStrategy,
});

/// Returns a [ProcessCameraProvider] instance.
Expand Down Expand Up @@ -115,9 +117,9 @@ class CameraXProxy {
int? fallbackRule}) createResolutionStrategy;

/// Returns a [ResolutionSelector] configured with the specified
/// [ResolutionStrategy].
ResolutionSelector Function(ResolutionStrategy resolutionStrategy)
createResolutionSelector;
/// [ResolutionStrategy] and [AspectRatioStrategy].
ResolutionSelector Function(ResolutionStrategy resolutionStrategy,
AspectRatioStrategy? aspectRatioStrategy) createResolutionSelector;

/// Returns a [FallbackStrategy] configured with the specified [VideoQuality]
/// and [VideoResolutionFallbackRule].
Expand Down Expand Up @@ -151,7 +153,7 @@ class CameraXProxy {
Camera2CameraControl Function(CameraControl cameraControl)
getCamera2CameraControl;

/// Create [CapureRequestOptions] with specified options.
/// Create [CaptureRequestOptions] with specified options.
CaptureRequestOptions Function(
List<(CaptureRequestKeySupportedType, Object?)> options)
createCaptureRequestOptions;
Expand All @@ -167,6 +169,11 @@ class CameraXProxy {
FocusMeteringAction Function(List<(MeteringPoint, int?)> meteringPointInfos,
bool? disableAutoCancel) createFocusMeteringAction;

/// Creates an [AspectRatioStrategy] with specified aspect ratio and fallback
/// rule.
AspectRatioStrategy Function(int aspectRatio, int fallbackRule)
createAspectRatioStrategy;

static Future<ProcessCameraProvider> _getProcessCameraProvider() {
return ProcessCameraProvider.getInstance();
}
Expand Down Expand Up @@ -234,8 +241,11 @@ class CameraXProxy {
}

static ResolutionSelector _createAttachedResolutionSelector(
ResolutionStrategy resolutionStrategy) {
return ResolutionSelector(resolutionStrategy: resolutionStrategy);
ResolutionStrategy resolutionStrategy,
AspectRatioStrategy? aspectRatioStrategy) {
return ResolutionSelector(
resolutionStrategy: resolutionStrategy,
aspectRatioStrategy: aspectRatioStrategy);
}

static FallbackStrategy _createAttachedFallbackStrategy(
Expand Down Expand Up @@ -291,4 +301,10 @@ class CameraXProxy {
meteringPointInfos: meteringPointInfos,
disableAutoCancel: disableAutoCancel);
}

static AspectRatioStrategy _createAttachedAspectRatioStrategy(
int preferredAspectRatio, int fallbackRule) {
return AspectRatioStrategy(
preferredAspectRatio: preferredAspectRatio, fallbackRule: fallbackRule);
}
}
2 changes: 1 addition & 1 deletion packages/camera/camera_android_camerax/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_android_camerax
description: Android implementation of the camera plugin using the CameraX library.
repository: https://github.com/flutter/packages/tree/main/packages/camera/camera_android_camerax
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.6.0+1
version: 0.6.1

environment:
sdk: ^3.1.0
Expand Down
Loading