ViewPort

@RequiresApi(value = 21)
class ViewPort


The field of view of one or many UseCases.

The ViewPort defines a FOV which is used by CameraX to calculate output crop rects. For use cases associated with the same ViewPort in a UseCaseGroup, the output crop rect will be mapped to the same camera sensor area. Usually ViewPort is configured to optimize for Preview so that ImageAnalysis and ImageCapture produce the same crop rect in a WYSIWYG way.

If the ViewPort is used with a ImageCapture and takePicture is called, the image may be cropped before saving to disk which introduces an additional latency. To avoid the latency and get the uncropped image, please use the in-memory method takePicture.

For ImageAnalysis and in-memory ImageCapture, the output crop rect is getCropRect; for on-disk ImageCapture, the image is cropped before saving; for Preview, the crop rect is getCropRect. Caller should transform the output in a way that only the area defined by the crop rect is visible to end users. Once the crop rect is applied, all the use cases will produce the same image with possibly different resolutions.

Summary

Nested types

Builder for ViewPort.

Constants

const Int

Generate a crop rect that once applied, it scales the output while maintaining its aspect ratio, so it fills the entire ViewPort and center it.

const Int

Generate a crop rect that once applied, it scales the output while maintaining its aspect ratio, so it fills the entire ViewPort, and align it to the end of the ViewPort, which is the bottom right corner in a left-to-right (LTR) layout, or the bottom left corner in a right-to-left (RTL) layout.

const Int

Generate a crop rect that once applied, it scales the output while maintaining its aspect ratio, so it fills the entire ViewPort, and align it to the start of the ViewPort, which is the top left corner in a left-to-right (LTR) layout, or the top right corner in a right-to-left (RTL) layout.

const Int
FIT = 3

Generate the max possible crop rect ignoring the aspect ratio.

Public functions

Rational

Gets the aspect ratio of the ViewPort.

Int

Gets the layout direction of the ViewPort.

Int

Gets the rotation of the ViewPort.

Int

Gets the scale type of the ViewPort.

Constants

FILL_CENTER

Added in 1.0.0
const val FILL_CENTER = 1: Int

Generate a crop rect that once applied, it scales the output while maintaining its aspect ratio, so it fills the entire ViewPort and center it.

This may cause the output to be cropped if the output aspect ratio does not match that of the ViewPort.

FILL_END

Added in 1.0.0
const val FILL_END = 2: Int

Generate a crop rect that once applied, it scales the output while maintaining its aspect ratio, so it fills the entire ViewPort, and align it to the end of the ViewPort, which is the bottom right corner in a left-to-right (LTR) layout, or the bottom left corner in a right-to-left (RTL) layout.

This may cause the output to be cropped if the output aspect ratio does not match that of the ViewPort.

FILL_START

Added in 1.0.0
const val FILL_START = 0: Int

Generate a crop rect that once applied, it scales the output while maintaining its aspect ratio, so it fills the entire ViewPort, and align it to the start of the ViewPort, which is the top left corner in a left-to-right (LTR) layout, or the top right corner in a right-to-left (RTL) layout.

This may cause the output to be cropped if the output aspect ratio does not match that of the ViewPort.

FIT

Added in 1.0.0
const val FIT = 3: Int

Generate the max possible crop rect ignoring the aspect ratio. For ImageAnalysis and ImageCapture, the output will be an image defined by the crop rect.

For Preview, further calculation is needed to to fit the crop rect into the viewfinder. Code sample below is a simplified version assuming Surface orientation is the same as the camera sensor orientation, the viewfinder is a SurfaceView and the viewfinder's pixel width/height is the same as the size request by CameraX in getResolution. For more complicated scenarios, please check out the source code of PreviewView in androidx.camera.view artifact.

First, calculate the transformation to fit the crop rect in the center of the viewfinder:

  val transformation = Matrix()
  transformation.setRectToRect(
      cropRect, new RectF(0, 0, viewFinder.width, viewFinder.height, ScaleToFit.CENTER))

Then apply the transformation to the viewfinder:

  val transformedRect = RectF(0, 0, viewFinder.width, viewFinder.height)
  transformation.mapRect(surfaceRect)
  viewFinder.pivotX = 0
  viewFinder.pivotY = 0
  viewFinder.translationX = transformedRect.left
  viewFinder.translationY = transformedRect.top
  viewFinder.scaleX = surfaceRect.width/transformedRect.width
  viewFinder.scaleY = surfaceRect.height/transformedRect.height

Public functions

getAspectRatio

Added in 1.0.0
fun getAspectRatio(): Rational

Gets the aspect ratio of the ViewPort.

getLayoutDirection

Added in 1.1.0
fun getLayoutDirection(): Int

Gets the layout direction of the ViewPort.

getRotation

Added in 1.0.0
fun getRotation(): Int

Gets the rotation of the ViewPort.

getScaleType

Added in 1.0.0
fun getScaleType(): Int

Gets the scale type of the ViewPort.