Skip to content

Commit 43dcfe5

Browse files
authored
[camera_android_camerax] Prefer higher resolution for max preset (#11956)
Updates `camera_android_camerax` so `ResolutionPreset.max` can select Camera2 high-resolution JPEG still capture sizes on Android. For `ResolutionPreset.max`, `ImageCapture` now uses `ResolutionSelector.PREFER_HIGHER_RESOLUTION_OVER_CAPTURE_RATE`; preview and image analysis keep the existing selector without high-resolution mode. Fixes flutter/flutter#188310 ## Problem On some Android devices, Camera2 exposes the largest 4:3 still resolution only through `getHighResolutionOutputSizes(ImageFormat.JPEG)`. The CameraX backend used `ResolutionStrategy.HIGHEST_AVAILABLE_STRATEGY` without `ResolutionSelector.PREFER_HIGHER_RESOLUTION_OVER_CAPTURE_RATE`, so CameraX could pick the largest regular JPEG size instead. ## Observed - Camera2 regular JPEG max: 3264x2448 - Camera2 high-resolution JPEG: 4624x3468 - Flutter `takePicture()`: 2448x3264 ## After fix - CameraX `ImageCapture`: 4624x3468 - Flutter `takePicture()`: 3468x4624 ## Risk / tradeoff High-resolution still capture can be slower and use more memory. Limited to `ResolutionPreset.max` and `ImageCapture` only. ## Note Generated Pigeon files (`camerax_library.g.dart`, `CameraXLibrary.g.kt`) include formatting churn from local pigeon regeneration; functional changes are limited to `ResolutionSelector` allowed resolution mode and max-preset ImageCapture selector behavior. ## Pre-Review Checklist ## Test plan
1 parent 7d17fc8 commit 43dcfe5

11 files changed

Lines changed: 889 additions & 2227 deletions

File tree

‎packages/camera/camera_android_camerax/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.7.4+3
2+
3+
* Updates `ResolutionPreset.max` to prefer higher resolution over capture rate
4+
for CameraX `ResolutionSelector` use cases on Android.
5+
16
## 0.7.4+2
27

38
* Bumps cameraxVersion from 1.6.0 to 1.6.1.

‎packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/CameraXLibrary.g.kt‎

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2013 The Flutter Authors
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
// Autogenerated from Pigeon (v26.1.7), do not edit directly.
4+
// Autogenerated from Pigeon (v26.3.4), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66
@file:Suppress("UNCHECKED_CAST", "ArrayInDataClass")
77

@@ -49,7 +49,7 @@ class CameraXError(
4949
val code: String,
5050
override val message: String? = null,
5151
val details: Any? = null
52-
) : Throwable()
52+
) : RuntimeException()
5353
/**
5454
* Maintains instances used to communicate with the corresponding objects in Dart.
5555
*
@@ -1301,7 +1301,7 @@ enum class CameraStateType(val raw: Int) {
13011301
}
13021302
}
13031303

1304-
/** The types (T) properly wrapped to be used as a LiveData<T>. */
1304+
/** The types (T) properly wrapped to be used as a `LiveData<T>`. */
13051305
enum class LiveDataSupportedType(val raw: Int) {
13061306
CAMERA_STATE(0),
13071307
ZOOM_STATE(1);
@@ -4607,6 +4607,7 @@ abstract class PigeonApiResolutionSelector(
46074607
abstract fun pigeon_defaultConstructor(
46084608
resolutionFilter: androidx.camera.core.resolutionselector.ResolutionFilter?,
46094609
resolutionStrategy: androidx.camera.core.resolutionselector.ResolutionStrategy?,
4610+
allowedResolutionMode: Long?,
46104611
aspectRatioStrategy: androidx.camera.core.resolutionselector.AspectRatioStrategy?
46114612
): androidx.camera.core.resolutionselector.ResolutionSelector
46124613

@@ -4620,6 +4621,15 @@ abstract class PigeonApiResolutionSelector(
46204621
pigeon_instance: androidx.camera.core.resolutionselector.ResolutionSelector
46214622
): androidx.camera.core.resolutionselector.ResolutionStrategy?
46224623

4624+
/**
4625+
* The allowed resolution mode for the `UseCase`.
4626+
*
4627+
* See [ResolutionSelectorAllowedResolutionMode].
4628+
*/
4629+
abstract fun allowedResolutionMode(
4630+
pigeon_instance: androidx.camera.core.resolutionselector.ResolutionSelector
4631+
): Long?
4632+
46234633
/**
46244634
* Returns the specified `AspectRatioStrategy`, or
46254635
* `AspectRatioStrategy.ratio_4_3FallbackAutoStrategy` if none is specified when creating the
@@ -4647,13 +4657,17 @@ abstract class PigeonApiResolutionSelector(
46474657
args[1] as androidx.camera.core.resolutionselector.ResolutionFilter?
46484658
val resolutionStrategyArg =
46494659
args[2] as androidx.camera.core.resolutionselector.ResolutionStrategy?
4660+
val allowedResolutionModeArg = args[3] as Long?
46504661
val aspectRatioStrategyArg =
4651-
args[3] as androidx.camera.core.resolutionselector.AspectRatioStrategy?
4662+
args[4] as androidx.camera.core.resolutionselector.AspectRatioStrategy?
46524663
val wrapped: List<Any?> =
46534664
try {
46544665
api.pigeonRegistrar.instanceManager.addDartCreatedInstance(
46554666
api.pigeon_defaultConstructor(
4656-
resolutionFilterArg, resolutionStrategyArg, aspectRatioStrategyArg),
4667+
resolutionFilterArg,
4668+
resolutionStrategyArg,
4669+
allowedResolutionModeArg,
4670+
aspectRatioStrategyArg),
46574671
pigeon_identifierArg)
46584672
listOf(null)
46594673
} catch (exception: Throwable) {
@@ -4708,23 +4722,30 @@ abstract class PigeonApiResolutionSelector(
47084722
pigeonRegistrar.instanceManager.addHostCreatedInstance(pigeon_instanceArg)
47094723
val resolutionFilterArg = resolutionFilter(pigeon_instanceArg)
47104724
val resolutionStrategyArg = resolutionStrategy(pigeon_instanceArg)
4725+
val allowedResolutionModeArg = allowedResolutionMode(pigeon_instanceArg)
47114726
val binaryMessenger = pigeonRegistrar.binaryMessenger
47124727
val codec = pigeonRegistrar.codec
47134728
val channelName =
47144729
"dev.flutter.pigeon.camera_android_camerax.ResolutionSelector.pigeon_newInstance"
47154730
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
4716-
channel.send(listOf(pigeon_identifierArg, resolutionFilterArg, resolutionStrategyArg)) {
4717-
if (it is List<*>) {
4718-
if (it.size > 1) {
4719-
callback(
4720-
Result.failure(CameraXError(it[0] as String, it[1] as String, it[2] as String?)))
4721-
} else {
4722-
callback(Result.success(Unit))
4731+
channel.send(
4732+
listOf(
4733+
pigeon_identifierArg,
4734+
resolutionFilterArg,
4735+
resolutionStrategyArg,
4736+
allowedResolutionModeArg)) {
4737+
if (it is List<*>) {
4738+
if (it.size > 1) {
4739+
callback(
4740+
Result.failure(
4741+
CameraXError(it[0] as String, it[1] as String, it[2] as String?)))
4742+
} else {
4743+
callback(Result.success(Unit))
4744+
}
4745+
} else {
4746+
callback(Result.failure(CameraXLibraryPigeonUtils.createConnectionError(channelName)))
4747+
}
47234748
}
4724-
} else {
4725-
callback(Result.failure(CameraXLibraryPigeonUtils.createConnectionError(channelName)))
4726-
}
4727-
}
47284749
}
47294750
}
47304751
}

‎packages/camera/camera_android_camerax/android/src/main/java/io/flutter/plugins/camerax/ResolutionSelectorProxyApi.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class ResolutionSelectorProxyApi extends PigeonApiResolutionSelector {
2626
public ResolutionSelector pigeon_defaultConstructor(
2727
@Nullable ResolutionFilter resolutionFilter,
2828
@Nullable ResolutionStrategy resolutionStrategy,
29+
@Nullable Long allowedResolutionMode,
2930
@Nullable AspectRatioStrategy aspectRatioStrategy) {
3031
final ResolutionSelector.Builder builder = new ResolutionSelector.Builder();
3132
if (aspectRatioStrategy != null) {
@@ -37,6 +38,9 @@ public ResolutionSelector pigeon_defaultConstructor(
3738
if (resolutionFilter != null) {
3839
builder.setResolutionFilter(resolutionFilter);
3940
}
41+
if (allowedResolutionMode != null) {
42+
builder.setAllowedResolutionMode(allowedResolutionMode.intValue());
43+
}
4044
return builder.build();
4145
}
4246

@@ -52,6 +56,12 @@ public ResolutionStrategy resolutionStrategy(@NonNull ResolutionSelector pigeonI
5256
return pigeonInstance.getResolutionStrategy();
5357
}
5458

59+
@Nullable
60+
@Override
61+
public Long allowedResolutionMode(@NonNull ResolutionSelector pigeonInstance) {
62+
return (long) pigeonInstance.getAllowedResolutionMode();
63+
}
64+
5565
@NonNull
5666
@Override
5767
public AspectRatioStrategy getAspectRatioStrategy(@NonNull ResolutionSelector pigeonInstance) {

‎packages/camera/camera_android_camerax/android/src/test/java/io/flutter/plugins/camerax/ResolutionSelectorTest.java‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,63 @@ public void pigeon_defaultConstructor_createsExpectedResolutionSelectorInstance(
2828
final AspectRatioStrategy aspectRatioStrategy = mock(AspectRatioStrategy.class);
2929

3030
final ResolutionSelector instance =
31-
api.pigeon_defaultConstructor(resolutionFilter, resolutionStrategy, aspectRatioStrategy);
31+
api.pigeon_defaultConstructor(
32+
resolutionFilter, resolutionStrategy, null, aspectRatioStrategy);
3233

3334
assertEquals(instance.getResolutionFilter(), resolutionFilter);
3435
assertEquals(instance.getResolutionStrategy(), resolutionStrategy);
3536
assertEquals(instance.getAspectRatioStrategy(), aspectRatioStrategy);
3637
}
3738

39+
@Test
40+
public void pigeon_defaultConstructor_setsPreferHigherResolutionOverCaptureRateWhenRequested() {
41+
final PigeonApiResolutionSelector api =
42+
new TestProxyApiRegistrar().getPigeonApiResolutionSelector();
43+
44+
final ResolutionSelector instance =
45+
api.pigeon_defaultConstructor(
46+
null,
47+
ResolutionStrategy.HIGHEST_AVAILABLE_STRATEGY,
48+
(long) ResolutionSelector.PREFER_HIGHER_RESOLUTION_OVER_CAPTURE_RATE,
49+
null);
50+
51+
assertEquals(
52+
ResolutionSelector.PREFER_HIGHER_RESOLUTION_OVER_CAPTURE_RATE,
53+
instance.getAllowedResolutionMode());
54+
}
55+
56+
@Test
57+
public void pigeon_defaultConstructor_setsPreferCaptureRateOverHigherResolutionWhenRequested() {
58+
final PigeonApiResolutionSelector api =
59+
new TestProxyApiRegistrar().getPigeonApiResolutionSelector();
60+
61+
final ResolutionSelector instance =
62+
api.pigeon_defaultConstructor(
63+
null,
64+
ResolutionStrategy.HIGHEST_AVAILABLE_STRATEGY,
65+
(long) ResolutionSelector.PREFER_CAPTURE_RATE_OVER_HIGHER_RESOLUTION,
66+
null);
67+
68+
assertEquals(
69+
ResolutionSelector.PREFER_CAPTURE_RATE_OVER_HIGHER_RESOLUTION,
70+
instance.getAllowedResolutionMode());
71+
}
72+
73+
@Test
74+
public void allowedResolutionMode_returnsExpectedAllowedResolutionMode() {
75+
final PigeonApiResolutionSelector api =
76+
new TestProxyApiRegistrar().getPigeonApiResolutionSelector();
77+
78+
final ResolutionSelector instance =
79+
new ResolutionSelector.Builder()
80+
.setAllowedResolutionMode(ResolutionSelector.PREFER_HIGHER_RESOLUTION_OVER_CAPTURE_RATE)
81+
.build();
82+
83+
assertEquals(
84+
(long) ResolutionSelector.PREFER_HIGHER_RESOLUTION_OVER_CAPTURE_RATE,
85+
api.allowedResolutionMode(instance).longValue());
86+
}
87+
3888
@Test
3989
public void resolutionFilter_returnsExpectedResolutionFilter() {
4090
final PigeonApiResolutionSelector api =

‎packages/camera/camera_android_camerax/lib/src/android_camera_camerax.dart‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,11 @@ class AndroidCameraCameraX extends CameraPlatform {
15541554
case ResolutionPreset.max:
15551555
// Automatically set strategy to choose highest available.
15561556
resolutionStrategy = ResolutionStrategy.highestAvailableStrategy;
1557-
return ResolutionSelector(resolutionStrategy: resolutionStrategy);
1557+
return ResolutionSelector(
1558+
resolutionStrategy: resolutionStrategy,
1559+
allowedResolutionMode:
1560+
ResolutionSelectorAllowedResolutionMode.preferHigherResolutionOverCaptureRate,
1561+
);
15581562
case null:
15591563
// If no preset is specified, default to CameraX's default behavior
15601564
// for each UseCase.

‎packages/camera/camera_android_camerax/lib/src/camerax_library.dart‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ class Surface {
9696
static const int rotation270 = 3;
9797
}
9898

99+
/// Allowed resolution mode constants for [ResolutionSelector].
100+
class ResolutionSelectorAllowedResolutionMode {
101+
/// CameraX prefers capture rate over higher resolution.
102+
///
103+
/// See https://developer.android.com/reference/kotlin/androidx/camera/core/resolutionselector/ResolutionSelector#PREFER_CAPTURE_RATE_OVER_HIGHER_RESOLUTION().
104+
static const int preferCaptureRateOverHigherResolution = 0;
105+
106+
/// CameraX prefers higher resolution over capture rate.
107+
///
108+
/// See https://developer.android.com/reference/kotlin/androidx/camera/core/resolutionselector/ResolutionSelector#PREFER_HIGHER_RESOLUTION_OVER_CAPTURE_RATE().
109+
static const int preferHigherResolutionOverCaptureRate = 1;
110+
}
111+
99112
/// An interface for retrieving camera information.
100113
///
101114
/// See https://developer.android.com/reference/androidx/camera/core/CameraInfo.

0 commit comments

Comments
 (0)