public static class CameraMath


Utility functions for doing common transformations of camera and sensor coordinate spaces.

When doing camera coordinate transformations, there are three main "coordinate spaces" that a developer may need to understand and work with:

  • Sensor-relative coordinates: These are integer-pixel coordinates that correspond to the physical active pixel array of a camera device. Most operations in Camera2 (like 3A metering regions or crop regions) define coordinates relative to the physical sensor (with (0,0) being the top-left active pixel).

  • Stream-relative coordinates: When configuring camera output streams, they are configured with a specific resolution. The stream coordinate space is axially aligned and centered within the crop region of the sensor, but may be further cropped either horizontally or vertically to maintain square pixels and its own aspect ratio (see Android's camera cropping specification at Output streams, cropping, and zoom). Stream coordinates are relative to the streamWidth and streamHeight boundaries.

  • UI-relative coordinates: These represent the display area of a stream on a UI surface (like a viewfinder). UI coordinates are scaled, potentially mirrored, and rotated relative to the coordinate system of the screen or window the viewfinder is currently displayed on. Note: The uiWidth and uiHeight used in calculations represent the UI-relative size of the viewfinder, without accounting for any viewport clipping or padding. Callers must normalize UI coordinates to account for clipping or padding before calling these functions.

Summary

Constants

static final int

When converting to sensor coordinates, this is the minimum width/height for rectangles that will be sent to the sensor.

static final float

It's common to need to convert a point into a sensor-relative rectangle for things like touch-to-focus, which often starts from a touch point that then needs to be converted into a sensor-relative rectangle.

Public fields

static @NonNull CameraMath

Public methods

static final @NonNull DiscreteRotation
computeSensorRotationToDisplayOrientation(
    int displayRotation,
    int sensorOrientation,
    boolean sensorIsFacingScreen
)

Computes the rotation required to rotate coordinates from sensor space to the display coordinate system.

static final @NonNull DiscreteRotation
computeSensorRotationToJpegOrientation(
    int deviceOrientation,
    int sensorOrientation,
    boolean sensorIsFacingScreen
)

Computes the clockwise rotation required to be applied to the captured image to be viewed upright.

static final @NonNull PointF
mapSensorPointToStreamPointF(
    @NonNull Point sensorPoint,
    @NonNull Size streamSize,
    @NonNull Rect sensorCrop
)

Converts a point relative to the camera sensor to an equivalent point relative to a specific camera stream.

static final @NonNull PointF
mapSensorPointToUiPointF(
    @NonNull Point sensorPoint,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation,
    @NonNull Rect sensorCrop
)

Converts a point relative to the camera sensor to an equivalent point relative to a UI element.

static final @NonNull RectF
mapSensorRectToStreamRectF(
    @NonNull Rect sensorRect,
    @NonNull Size streamSize,
    @NonNull Rect sensorCrop
)

Converts a rectangle relative to the camera sensor to an equivalent rectangle relative to a specific camera stream.

static final @NonNull RectF
mapSensorRectToUiRectF(
    @NonNull Rect sensorRect,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation,
    @NonNull Rect sensorCrop
)

Converts a rectangle relative to the camera sensor to an equivalent rectangle relative to a UI element.

static final @NonNull SizeF
mapSensorSizeToStreamSizeF(
    @NonNull Size size,
    @NonNull Size streamSize,
    int sensorCropWidth,
    int sensorCropHeight
)

Converts a size relative to the camera sensor to an equivalent size relative to a specific camera stream.

static final @NonNull SizeF
mapSensorSizeToUiSizeF(
    @NonNull Size size,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    int sensorRotationToDisplayOrientation,
    int sensorCropWidth,
    int sensorCropHeight
)

Converts a size relative to the camera sensor to an equivalent size relative to a UI element.

static final @NonNull Point
mapStreamPointToSensorPoint(
    @NonNull PointF streamPoint,
    @NonNull Size streamSize,
    @NonNull Rect sensorCrop,
    boolean coerceToCropRegion
)

Converts a point relative to a specific camera stream to an equivalent point relative to the camera sensor.

static final @NonNull Rect
mapStreamPointToSensorRect(
    @NonNull PointF streamPoint,
    @NonNull Size streamSize,
    @NonNull Rect sensorCrop,
    int minimumSensorSize,
    boolean coerceToCropRegion,
    float touchToFocusRatio
)

Converts a point relative to a specific camera stream to an equivalent rectangle relative to the camera sensor.

static final @NonNull PointF
mapStreamPointToUiPointF(
    @NonNull PointF streamPoint,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation
)

Converts a point relative to a specific camera stream to an equivalent point relative to a UI element.

static final @NonNull Rect
mapStreamRectToSensorRect(
    @NonNull RectF streamRect,
    @NonNull Size streamSize,
    @NonNull Rect sensorCrop,
    int minimumSensorSize,
    boolean coerceToCropRegion
)

Converts a rectangle relative to a specific camera stream to an equivalent rectangle relative to the camera sensor.

static final @NonNull RectF
mapStreamRectToUiRectF(
    @NonNull RectF streamRect,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation
)

Converts a rectangle relative to a specific camera stream to an equivalent rectangle relative to a UI element.

static final @NonNull Size
mapStreamSizeToSensorSize(
    @NonNull Size size,
    @NonNull Size streamSize,
    int sensorCropWidth,
    int sensorCropHeight
)

Converts a size relative to a specific camera stream to an equivalent size relative to the camera sensor.

static final @NonNull SizeF
mapStreamSizeToUiSizeF(
    @NonNull Size size,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    int sensorRotationToDisplayOrientation
)

Converts a size relative to a specific camera stream to an equivalent size relative to a UI element.

static final @NonNull Point
mapUiPointToSensorPoint(
    @NonNull Point uiPoint,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation,
    @NonNull Rect sensorCrop,
    boolean coerceToCropRegion
)

Converts a point relative to a UI element to an equivalent point relative to the camera sensor.

static final @NonNull Rect
mapUiPointToSensorRect(
    @NonNull Point uiPoint,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation,
    @NonNull Rect sensorCrop,
    int minimumSensorSize,
    boolean coerceToCropRegion,
    float touchToFocusRatio
)

Converts a point relative to a UI element to an equivalent rectangle relative to the camera sensor.

static final @NonNull PointF
mapUiPointToStreamPointF(
    @NonNull Point uiPoint,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation
)

Converts a point relative to a UI element to an equivalent point relative to a specific camera stream.

static final @NonNull RectF
mapUiPointToStreamRectF(
    @NonNull Point uiPoint,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation,
    float touchToFocusRatio
)

Converts a point relative to a UI element to an equivalent rectangle relative to a specific camera stream.

static final @NonNull Rect
mapUiRectToSensorRect(
    @NonNull RectF uiRect,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation,
    @NonNull Rect sensorCrop,
    int minimumSensorSize,
    boolean coerceToCropRegion
)

Converts a rectangle relative to a UI element to an equivalent rectangle relative to the camera sensor.

static final @NonNull RectF
mapUiRectToStreamRectF(
    @NonNull RectF uiRect,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation
)

Converts a rectangle relative to a UI element to an equivalent rectangle relative to a specific camera stream.

static final @NonNull Size
mapUiSizeToSensorSize(
    @NonNull Size size,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    int sensorRotationToDisplayOrientation,
    int sensorCropWidth,
    int sensorCropHeight
)

Converts a size relative to a UI element to an equivalent size relative to the camera sensor.

static final @NonNull SizeF
mapUiSizeToStreamSizeF(
    @NonNull Size size,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    int sensorRotationToDisplayOrientation
)

Converts a size relative to a UI element to an equivalent size relative to a specific camera stream.

Constants

DEFAULT_MIN_SENSOR_PIXELS

Added in 1.7.0-alpha03
public static final int DEFAULT_MIN_SENSOR_PIXELS = 32

When converting to sensor coordinates, this is the minimum width/height for rectangles that will be sent to the sensor. This helps prevent scenarios where rectangles need to be clamped to the sensor, but where setting a small, or zero, size can lead to unexpected or bad behavior.

DEFAULT_POINT_TO_RECT_RATIO

Added in 1.7.0-alpha03
public static final float DEFAULT_POINT_TO_RECT_RATIO = 0.18375f

It's common to need to convert a point into a sensor-relative rectangle for things like touch-to-focus, which often starts from a touch point that then needs to be converted into a sensor-relative rectangle.

This ratio is used to compute a rectangle based on the short dimension of the current stream.

Public fields

INSTANCE

Added in 1.7.0-alpha03
public static @NonNull CameraMath INSTANCE

Public methods

computeSensorRotationToDisplayOrientation

Added in 1.7.0-alpha03
public static final @NonNull DiscreteRotation computeSensorRotationToDisplayOrientation(
    int displayRotation,
    int sensorOrientation,
    boolean sensorIsFacingScreen
)

Computes the rotation required to rotate coordinates from sensor space to the display coordinate system.

Parameters
int displayRotation

The rotation of the display in degrees. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be obtained from android.view.Display.getRotation and converted using DiscreteRotation.fromSurfaceRotation (e.g., DiscreteRotation.fromSurfaceRotation(display.rotation).degrees). (Note: do not pass android.view.Surface.ROTATION_0, android.view.Surface.ROTATION_90, etc. constants directly as they represent index values rather than degrees).

int sensorOrientation

The physical orientation of the sensor in degrees (typically android.hardware.camera2.CameraCharacteristics.SENSOR_ORIENTATION). Must be one of 0, 90, 180, or 270. Negative values are not supported.

boolean sensorIsFacingScreen

Whether the camera sensor is facing the screen (front-facing).

Throws
IllegalArgumentException

if displayRotation or sensorOrientation is not one of 0, 90, 180, or 270.

computeSensorRotationToJpegOrientation

Added in 1.7.0-alpha03
public static final @NonNull DiscreteRotation computeSensorRotationToJpegOrientation(
    int deviceOrientation,
    int sensorOrientation,
    boolean sensorIsFacingScreen
)

Computes the clockwise rotation required to be applied to the captured image to be viewed upright.

See android.hardware.camera2.CaptureRequest.JPEG_ORIENTATION for more details.

Parameters
int deviceOrientation

The physical orientation of the device in degrees. Must be one of 0, 90, 180, or 270. Negative values are not supported. This is typically obtained from an android.view.OrientationEventListener and rounded to the nearest 90 degrees using DiscreteRotation.round. See the Camera Orientation Guide for more details.

int sensorOrientation

The physical orientation of the sensor (typically android.hardware.camera2.CameraCharacteristics.SENSOR_ORIENTATION). Must be one of 0, 90, 180, or 270. Negative values are not supported.

boolean sensorIsFacingScreen

Whether the camera sensor is facing the screen (front-facing).

Throws
IllegalArgumentException

if deviceOrientation or sensorOrientation is not one of 0, 90, 180, or 270.

mapSensorPointToStreamPointF

Added in 1.7.0-alpha03
public static final @NonNull PointF mapSensorPointToStreamPointF(
    @NonNull Point sensorPoint,
    @NonNull Size streamSize,
    @NonNull Rect sensorCrop
)

Converts a point relative to the camera sensor to an equivalent point relative to a specific camera stream.

Parameters
@NonNull Point sensorPoint

The point on the sensor. Sensor coordinates are relative to the sensor's top-left active pixel (0,0).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

@NonNull Rect sensorCrop

The crop region of the sensor (typically android.hardware.camera2.CaptureRequest.SCALER_CROP_REGION, or android.hardware.camera2.CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE if no crop is applied).

mapSensorPointToUiPointF

Added in 1.7.0-alpha03
public static final @NonNull PointF mapSensorPointToUiPointF(
    @NonNull Point sensorPoint,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation,
    @NonNull Rect sensorCrop
)

Converts a point relative to the camera sensor to an equivalent point relative to a UI element.

Parameters
@NonNull Point sensorPoint

The point on the sensor. Sensor coordinates are relative to the sensor's top-left active pixel (0,0).

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

boolean streamMirroring

Whether the camera stream is mirrored.

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

@NonNull Rect sensorCrop

The crop region of the sensor (typically android.hardware.camera2.CaptureRequest.SCALER_CROP_REGION, or android.hardware.camera2.CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE if no crop is applied).

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.

mapSensorRectToStreamRectF

Added in 1.7.0-alpha03
public static final @NonNull RectF mapSensorRectToStreamRectF(
    @NonNull Rect sensorRect,
    @NonNull Size streamSize,
    @NonNull Rect sensorCrop
)

Converts a rectangle relative to the camera sensor to an equivalent rectangle relative to a specific camera stream.

Parameters
@NonNull Rect sensorRect

The rectangle on the sensor. Sensor coordinates are relative to the sensor's top-left active pixel (0,0).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

@NonNull Rect sensorCrop

The crop region of the sensor (typically android.hardware.camera2.CaptureRequest.SCALER_CROP_REGION, or android.hardware.camera2.CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE if no crop is applied).

mapSensorRectToUiRectF

Added in 1.7.0-alpha03
public static final @NonNull RectF mapSensorRectToUiRectF(
    @NonNull Rect sensorRect,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation,
    @NonNull Rect sensorCrop
)

Converts a rectangle relative to the camera sensor to an equivalent rectangle relative to a UI element.

Parameters
@NonNull Rect sensorRect

The rectangle on the sensor. Sensor coordinates are relative to the sensor's top-left active pixel (0,0).

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

boolean streamMirroring

Whether the camera stream is mirrored.

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

@NonNull Rect sensorCrop

The crop region of the sensor (typically android.hardware.camera2.CaptureRequest.SCALER_CROP_REGION, or android.hardware.camera2.CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE if no crop is applied).

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.

mapSensorSizeToStreamSizeF

Added in 1.7.0-alpha03
public static final @NonNull SizeF mapSensorSizeToStreamSizeF(
    @NonNull Size size,
    @NonNull Size streamSize,
    int sensorCropWidth,
    int sensorCropHeight
)

Converts a size relative to the camera sensor to an equivalent size relative to a specific camera stream.

Parameters
@NonNull Size size

The size on the sensor. Sensor coordinates are relative to the sensor's top-left active pixel (0,0).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

int sensorCropWidth

The width of the sensor crop region.

int sensorCropHeight

The height of the sensor crop region.

mapSensorSizeToUiSizeF

Added in 1.7.0-alpha03
public static final @NonNull SizeF mapSensorSizeToUiSizeF(
    @NonNull Size size,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    int sensorRotationToDisplayOrientation,
    int sensorCropWidth,
    int sensorCropHeight
)

Converts a size relative to the camera sensor to an equivalent size relative to a UI element.

Parameters
@NonNull Size size

The size on the sensor. Sensor coordinates are relative to the sensor's top-left active pixel (0,0).

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

int sensorCropWidth

The width of the sensor crop region.

int sensorCropHeight

The height of the sensor crop region.

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.

mapStreamPointToSensorPoint

Added in 1.7.0-alpha03
public static final @NonNull Point mapStreamPointToSensorPoint(
    @NonNull PointF streamPoint,
    @NonNull Size streamSize,
    @NonNull Rect sensorCrop,
    boolean coerceToCropRegion
)

Converts a point relative to a specific camera stream to an equivalent point relative to the camera sensor.

Parameters
@NonNull PointF streamPoint

The point in the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

@NonNull Size streamSize

The size of the camera stream.

@NonNull Rect sensorCrop

The crop region of the sensor (typically android.hardware.camera2.CaptureRequest.SCALER_CROP_REGION, or android.hardware.camera2.CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE if no crop is applied). Sensor coordinates are relative to the sensor's top-left active pixel (0,0).

boolean coerceToCropRegion

Whether to coerce the output point into the crop region.

mapStreamPointToSensorRect

Added in 1.7.0-alpha03
public static final @NonNull Rect mapStreamPointToSensorRect(
    @NonNull PointF streamPoint,
    @NonNull Size streamSize,
    @NonNull Rect sensorCrop,
    int minimumSensorSize,
    boolean coerceToCropRegion,
    float touchToFocusRatio
)

Converts a point relative to a specific camera stream to an equivalent rectangle relative to the camera sensor.

The output rectangle will have a size determined by the touchToFocusRatio and will be centered on the mapped point. If the calculated rectangle is smaller than minimumSensorSize in either dimension, it will be expanded symmetrically from its center to reach the minimum.

Parameters
@NonNull PointF streamPoint

The point in the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

@NonNull Size streamSize

The size of the camera stream.

@NonNull Rect sensorCrop

The crop region of the sensor (typically android.hardware.camera2.CaptureRequest.SCALER_CROP_REGION, or android.hardware.camera2.CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE if no crop is applied).

int minimumSensorSize

The minimum allowed width and height for the output rectangle in sensor pixels. If the calculated rectangle is smaller than this value in either dimension, it will be expanded symmetrically from its center to reach the minimum. Defaults to DEFAULT_MIN_SENSOR_PIXELS. Set to 0 or less to disable this behavior.

boolean coerceToCropRegion

Whether to coerce the output rectangle into the crop region.

float touchToFocusRatio

The ratio to use when computing a rectangle based on the short dimension of the stream.

mapStreamPointToUiPointF

Added in 1.7.0-alpha03
public static final @NonNull PointF mapStreamPointToUiPointF(
    @NonNull PointF streamPoint,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation
)

Converts a point relative to a specific camera stream to an equivalent point relative to a UI element.

Parameters
@NonNull PointF streamPoint

The point in the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream.

boolean streamMirroring

Whether the camera stream is mirrored.

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.

mapStreamRectToSensorRect

Added in 1.7.0-alpha03
public static final @NonNull Rect mapStreamRectToSensorRect(
    @NonNull RectF streamRect,
    @NonNull Size streamSize,
    @NonNull Rect sensorCrop,
    int minimumSensorSize,
    boolean coerceToCropRegion
)

Converts a rectangle relative to a specific camera stream to an equivalent rectangle relative to the camera sensor.

Parameters
@NonNull RectF streamRect

The rectangle in the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

@NonNull Size streamSize

The size of the camera stream.

@NonNull Rect sensorCrop

The crop region of the sensor (typically android.hardware.camera2.CaptureRequest.SCALER_CROP_REGION, or android.hardware.camera2.CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE if no crop is applied). Sensor coordinates are relative to the sensor's top-left active pixel (0,0).

int minimumSensorSize

The minimum allowed width and height for the output rectangle in sensor pixels. If the calculated rectangle is smaller than this value in either dimension, it will be expanded symmetrically from its center to reach the minimum. Defaults to DEFAULT_MIN_SENSOR_PIXELS. Set to 0 or less to disable this behavior.

boolean coerceToCropRegion

Whether to coerce the output rectangle into the crop region.

mapStreamRectToUiRectF

Added in 1.7.0-alpha03
public static final @NonNull RectF mapStreamRectToUiRectF(
    @NonNull RectF streamRect,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation
)

Converts a rectangle relative to a specific camera stream to an equivalent rectangle relative to a UI element.

Parameters
@NonNull RectF streamRect

The rectangle in the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream.

boolean streamMirroring

Whether the camera stream is mirrored.

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.

mapStreamSizeToSensorSize

Added in 1.7.0-alpha03
public static final @NonNull Size mapStreamSizeToSensorSize(
    @NonNull Size size,
    @NonNull Size streamSize,
    int sensorCropWidth,
    int sensorCropHeight
)

Converts a size relative to a specific camera stream to an equivalent size relative to the camera sensor.

Parameters
@NonNull Size size

The size in the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

@NonNull Size streamSize

The size of the camera stream.

int sensorCropWidth

The width of the sensor crop region.

int sensorCropHeight

The height of the sensor crop region.

mapStreamSizeToUiSizeF

Added in 1.7.0-alpha03
public static final @NonNull SizeF mapStreamSizeToUiSizeF(
    @NonNull Size size,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    int sensorRotationToDisplayOrientation
)

Converts a size relative to a specific camera stream to an equivalent size relative to a UI element.

Parameters
@NonNull Size size

The size in the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream.

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.

mapUiPointToSensorPoint

Added in 1.7.0-alpha03
public static final @NonNull Point mapUiPointToSensorPoint(
    @NonNull Point uiPoint,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation,
    @NonNull Rect sensorCrop,
    boolean coerceToCropRegion
)

Converts a point relative to a UI element to an equivalent point relative to the camera sensor.

Parameters
@NonNull Point uiPoint

The point in the UI. UI coordinates are relative to uiSize and do not account for the positioning of the UI. Callers must normalize coordinates to account for clipping or padding before calling this function.

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

boolean streamMirroring

Whether the camera stream is mirrored.

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

@NonNull Rect sensorCrop

The crop region of the sensor (typically android.hardware.camera2.CaptureRequest.SCALER_CROP_REGION, or android.hardware.camera2.CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE if no crop is applied). Sensor coordinates are relative to the sensor's top-left active pixel (0,0).

boolean coerceToCropRegion

Whether to coerce the output point into the crop region.

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.

mapUiPointToSensorRect

Added in 1.7.0-alpha03
public static final @NonNull Rect mapUiPointToSensorRect(
    @NonNull Point uiPoint,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation,
    @NonNull Rect sensorCrop,
    int minimumSensorSize,
    boolean coerceToCropRegion,
    float touchToFocusRatio
)

Converts a point relative to a UI element to an equivalent rectangle relative to the camera sensor.

The output rectangle will have a size determined by the touchToFocusRatio and will be centered on the mapped point. If the calculated rectangle is smaller than minimumSensorSize in either dimension, it will be expanded symmetrically from its center to reach the minimum.

Parameters
@NonNull Point uiPoint

The point in the UI. UI coordinates are relative to uiSize and do not account for the positioning of the UI. Callers must normalize coordinates to account for clipping or padding before calling this function.

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

boolean streamMirroring

Whether the camera stream is mirrored.

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

@NonNull Rect sensorCrop

The crop region of the sensor (typically android.hardware.camera2.CaptureRequest.SCALER_CROP_REGION, or android.hardware.camera2.CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE if no crop is applied). Sensor coordinates are relative to the sensor's top-left active pixel (0,0).

int minimumSensorSize

The minimum allowed width and height for the output rectangle in sensor pixels. If the calculated rectangle is smaller than this value in either dimension, it will be expanded symmetrically from its center to reach the minimum. Defaults to DEFAULT_MIN_SENSOR_PIXELS. Set to 0 or less to disable this behavior.

boolean coerceToCropRegion

Whether to coerce the output rectangle into the crop region.

float touchToFocusRatio

The ratio to use when computing a rectangle based on the short dimension of the stream.

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.

mapUiPointToStreamPointF

Added in 1.7.0-alpha03
public static final @NonNull PointF mapUiPointToStreamPointF(
    @NonNull Point uiPoint,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation
)

Converts a point relative to a UI element to an equivalent point relative to a specific camera stream.

Parameters
@NonNull Point uiPoint

The point in the UI. UI coordinates are relative to uiSize and do not account for the positioning of the UI. Callers must normalize coordinates to account for clipping or padding before calling this function.

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

boolean streamMirroring

Whether the camera stream is mirrored (e.g., for front-facing camera).

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.

mapUiPointToStreamRectF

Added in 1.7.0-alpha03
public static final @NonNull RectF mapUiPointToStreamRectF(
    @NonNull Point uiPoint,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation,
    float touchToFocusRatio
)

Converts a point relative to a UI element to an equivalent rectangle relative to a specific camera stream.

Parameters
@NonNull Point uiPoint

The point in the UI. UI coordinates are relative to uiSize and do not account for the positioning of the UI. Callers must normalize coordinates to account for clipping or padding before calling this function.

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

boolean streamMirroring

Whether the camera stream is mirrored.

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

float touchToFocusRatio

The ratio to use when computing a rectangle based on the short dimension of the stream.

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.

mapUiRectToSensorRect

Added in 1.7.0-alpha03
public static final @NonNull Rect mapUiRectToSensorRect(
    @NonNull RectF uiRect,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation,
    @NonNull Rect sensorCrop,
    int minimumSensorSize,
    boolean coerceToCropRegion
)

Converts a rectangle relative to a UI element to an equivalent rectangle relative to the camera sensor.

Parameters
@NonNull RectF uiRect

The rectangle in the UI. UI coordinates are relative to uiSize and do not account for the positioning of the UI. Callers must normalize coordinates to account for clipping or padding before calling this function.

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

boolean streamMirroring

Whether the camera stream is mirrored.

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

@NonNull Rect sensorCrop

The crop region of the sensor (typically android.hardware.camera2.CaptureRequest.SCALER_CROP_REGION, or android.hardware.camera2.CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE if no crop is applied). Sensor coordinates are relative to the sensor's top-left active pixel (0,0).

int minimumSensorSize

The minimum allowed width and height for the output rectangle in sensor pixels. If the calculated rectangle is smaller than this value in either dimension, it will be expanded symmetrically from its center to reach the minimum. Defaults to DEFAULT_MIN_SENSOR_PIXELS. Set to 0 or less to disable this behavior.

boolean coerceToCropRegion

Whether to coerce the output rectangle into the crop region.

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.

mapUiRectToStreamRectF

Added in 1.7.0-alpha03
public static final @NonNull RectF mapUiRectToStreamRectF(
    @NonNull RectF uiRect,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    boolean streamMirroring,
    int sensorRotationToDisplayOrientation
)

Converts a rectangle relative to a UI element to an equivalent rectangle relative to a specific camera stream.

Parameters
@NonNull RectF uiRect

The rectangle in the UI. UI coordinates are relative to uiSize and do not account for the positioning of the UI. Callers must normalize coordinates to account for clipping or padding before calling this function.

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

boolean streamMirroring

Whether the camera stream is mirrored.

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.

mapUiSizeToSensorSize

Added in 1.7.0-alpha03
public static final @NonNull Size mapUiSizeToSensorSize(
    @NonNull Size size,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    int sensorRotationToDisplayOrientation,
    int sensorCropWidth,
    int sensorCropHeight
)

Converts a size relative to a UI element to an equivalent size relative to the camera sensor.

Parameters
@NonNull Size size

The size in the UI. UI coordinates are relative to uiSize and do not account for the positioning of the UI. Callers must normalize coordinates to account for clipping or padding before calling this function.

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

int sensorCropWidth

The width of the sensor crop region.

int sensorCropHeight

The height of the sensor crop region.

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.

mapUiSizeToStreamSizeF

Added in 1.7.0-alpha03
public static final @NonNull SizeF mapUiSizeToStreamSizeF(
    @NonNull Size size,
    @NonNull Size uiSize,
    @NonNull Size streamSize,
    int sensorRotationToDisplayOrientation
)

Converts a size relative to a UI element to an equivalent size relative to a specific camera stream.

Parameters
@NonNull Size size

The size in the UI. UI coordinates are relative to uiSize and do not account for the positioning of the UI. Callers must normalize coordinates to account for clipping or padding before calling this function.

@NonNull Size uiSize

The size of the UI (specifically the UI-relative size of the viewfinder, without accounting for clipping or padding).

@NonNull Size streamSize

The size of the camera stream. Stream coordinates are relative to the streamWidth and streamHeight boundaries.

int sensorRotationToDisplayOrientation

The orientation from sensor to display. Must be one of 0, 90, 180, or 270. Negative values are not supported. This can be computed using computeSensorRotationToDisplayOrientation.

Throws
IllegalArgumentException

if sensorRotationToDisplayOrientation is not one of 0, 90, 180, or 270.