AspectRatioStrategy


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

const 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.

const Int

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

const AspectRatioStrategy

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

const AspectRatioStrategy

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

Public constructors

AspectRatioStrategy(preferredAspectRatio: Int, fallbackRule: Int)

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

Public functions

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
const val FALLBACK_RULE_AUTO = 1: 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.

FALLBACK_RULE_NONE

Added in 1.3.0
const val FALLBACK_RULE_NONE = 0: Int

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
const val RATIO_16_9_FALLBACK_AUTO_STRATEGYAspectRatioStrategy

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
const val RATIO_4_3_FALLBACK_AUTO_STRATEGYAspectRatioStrategy

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
AspectRatioStrategy(preferredAspectRatio: Int, fallbackRule: Int)

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
preferredAspectRatio: Int

the preferred aspect ratio to select first which can be either RATIO_4_3 or RATIO_16_9.

fallbackRule: Int

the rule to follow when the preferred aspect ratio is unavailable. This can be either FALLBACK_RULE_AUTO or FALLBACK_RULE_NONE.

Public functions

getFallbackRule

Added in 1.3.0
fun getFallbackRule(): Int

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

getPreferredAspectRatio

Added in 1.3.0
fun getPreferredAspectRatio(): Int

Returns the specified preferred aspect ratio.