AspectRatioStrategy


public final class AspectRatioStrategy


The aspect ratio strategy defines the sequence of aspect ratios that are used to select the best size for a particular image.

Applications can create a ResolutionSelector with a proper AspectRatioStrategy to choose a resolution that matches the preferred aspect ratio.

By default, CameraX supports the common 4:3 and 16:9 aspect ratio settings. Some devices may offer additional output sizes. To access these, you'll need to create a ResolutionSelector with a ResolutionFilter to find and select those specific sizes.

Summary

Constants

static final int

CameraX automatically chooses the next best aspect ratio which contains the closest field of view (FOV) of the camera sensor, from the remaining options.

static final int

CameraX doesn't fall back to select sizes of any other aspect ratio when this fallback rule is used.

static final @NonNull AspectRatioStrategy

The pre-defined aspect ratio strategy that selects sizes with RATIO_16_9 in priority.

static final @NonNull AspectRatioStrategy

The pre-defined default aspect ratio strategy that selects sizes with RATIO_4_3 in priority.

Public constructors

AspectRatioStrategy(int preferredAspectRatio, int fallbackRule)

Creates a new AspectRatioStrategy instance, configured with the specified preferred aspect ratio and fallback rule.

Public methods

int

Returns the specified fallback rule for choosing the aspect ratio when the preferred aspect ratio is not available.

int

Returns the specified preferred aspect ratio.

Constants

FALLBACK_RULE_AUTO

Added in 1.3.0
public static final int FALLBACK_RULE_AUTO = 1

CameraX automatically chooses the next best aspect ratio which contains the closest field of view (FOV) of the camera sensor, from the remaining options.

FALLBACK_RULE_NONE

Added in 1.3.0
public static final int FALLBACK_RULE_NONE = 0

CameraX doesn't fall back to select sizes of any other aspect ratio when this fallback rule is used.

Note that an AspectRatioStrategy with more restricted settings may result in that no resolution can be selected to use. Applications will receive IllegalArgumentException when binding the UseCases with such kind of AspectRatioStrategy.

RATIO_16_9_FALLBACK_AUTO_STRATEGY

Added in 1.3.0
public static final @NonNull AspectRatioStrategy RATIO_16_9_FALLBACK_AUTO_STRATEGY

The pre-defined aspect ratio strategy that selects sizes with RATIO_16_9 in priority. Then, selects sizes with other aspect ratios according to which aspect ratio can contain the closest FOV of the camera sensor.

Please see the Output streams, Cropping, and Zoom introduction to know more about the camera FOV.

RATIO_4_3_FALLBACK_AUTO_STRATEGY

Added in 1.3.0
public static final @NonNull AspectRatioStrategy RATIO_4_3_FALLBACK_AUTO_STRATEGY

The pre-defined default aspect ratio strategy that selects sizes with RATIO_4_3 in priority. Then, selects sizes with other aspect ratios according to which aspect ratio can contain the closest FOV of the camera sensor.

Please see the Output streams, Cropping, and Zoom introduction to know more about the camera FOV.

Public constructors

AspectRatioStrategy

Added in 1.3.0
public AspectRatioStrategy(int preferredAspectRatio, int fallbackRule)

Creates a new AspectRatioStrategy instance, configured with the specified preferred aspect ratio and fallback rule.

OEMs might make the width or height of the supported output sizes be mod 16 aligned for performance reasons. This means that the device might support 1920x1088 instead of 1920x1080, even though a 16:9 aspect ratio size is 1920x1080. CameraX can select these mod 16 aligned sizes when applications specify the preferred aspect ratio as RATIO_16_9.

Some devices may have issues using sizes of the preferred aspect ratios. CameraX recommends that applications use the FALLBACK_RULE_AUTO setting to avoid no resolution being available, as an IllegalArgumentException may be thrown when calling bindToLifecycle to bind UseCases with the AspectRatioStrategy specified in the ResolutionSelector.

Parameters
int preferredAspectRatio

the preferred aspect ratio to select first.

int fallbackRule

the rule to follow when the preferred aspect ratio is not available.

Public methods

getFallbackRule

Added in 1.3.0
public int getFallbackRule()

Returns the specified fallback rule for choosing the aspect ratio when the preferred aspect ratio is not available.

getPreferredAspectRatio

Added in 1.3.0
public int getPreferredAspectRatio()

Returns the specified preferred aspect ratio.