# ViewPort

Artifact: [androidx.camera:camera-core](https://developer.android.com/jetpack/androidx/releases/camera) [View Source](https://cs.android.com/search?q=file:androidx/camera/core/ViewPort.java+class:androidx.camera.core.ViewPort) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

*** ** * ** ***


```
public final class ViewPort
```

<br />

*** ** * ** ***

The field of view of one or many `https://developer.android.com/reference/androidx/camera/core/UseCase`s.

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

If the `https://developer.android.com/reference/androidx/camera/core/ViewPort` is used with a `https://developer.android.com/reference/androidx/camera/core/ImageCapture` and `https://developer.android.com/reference/androidx/camera/core/ImageCapture#takePicture(androidx.camera.core.ImageCapture.OutputFileOptions,java.util.concurrent.Executor,androidx.camera.core.ImageCapture.OnImageSavedCallback)` 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 `https://developer.android.com/reference/androidx/camera/core/ImageCapture#takePicture(java.util.concurrent.Executor,androidx.camera.core.ImageCapture.OnImageCapturedCallback)`.

For `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis` and in-memory `https://developer.android.com/reference/androidx/camera/core/ImageCapture`, the output crop rect is `https://developer.android.com/reference/androidx/camera/core/ImageProxy#getCropRect()`; for on-disk `https://developer.android.com/reference/androidx/camera/core/ImageCapture`, the image is cropped before saving; for `https://developer.android.com/reference/androidx/camera/core/Preview`, the crop rect is `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest.TransformationInfo#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 |
|---|
| `public final class https://developer.android.com/reference/androidx/camera/core/ViewPort.Builder` Builder for `https://developer.android.com/reference/androidx/camera/core/ViewPort`. |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/camera/core/ViewPort#FILL_CENTER() = 1` Generate a crop rect that once applied, it scales the output while maintaining its aspect ratio, so it fills the entire `https://developer.android.com/reference/androidx/camera/core/ViewPort` and center it. |
| `static final int` | `https://developer.android.com/reference/androidx/camera/core/ViewPort#FILL_END() = 2` Generate a crop rect that once applied, it scales the output while maintaining its aspect ratio, so it fills the entire `https://developer.android.com/reference/androidx/camera/core/ViewPort`, and align it to the end of the `https://developer.android.com/reference/androidx/camera/core/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. |
| `static final int` | `https://developer.android.com/reference/androidx/camera/core/ViewPort#FILL_START() = 0` Generate a crop rect that once applied, it scales the output while maintaining its aspect ratio, so it fills the entire `https://developer.android.com/reference/androidx/camera/core/ViewPort`, and align it to the start of the `https://developer.android.com/reference/androidx/camera/core/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. |
| `static final int` | `https://developer.android.com/reference/androidx/camera/core/ViewPort#FIT() = 3` Generate the max possible crop rect ignoring the aspect ratio. |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/util/Rational.html` | `https://developer.android.com/reference/androidx/camera/core/ViewPort#getAspectRatio()()` Gets the aspect ratio of the `https://developer.android.com/reference/androidx/camera/core/ViewPort`. |
| `int` | `https://developer.android.com/reference/androidx/camera/core/ViewPort#getLayoutDirection()()` Gets the layout direction of the `https://developer.android.com/reference/androidx/camera/core/ViewPort`. |
| `int` | `https://developer.android.com/reference/androidx/camera/core/ViewPort#getRotation()()` Gets the rotation of the `https://developer.android.com/reference/androidx/camera/core/ViewPort`. |
| `int` | `https://developer.android.com/reference/androidx/camera/core/ViewPort#getScaleType()()` Gets the scale type of the `https://developer.android.com/reference/androidx/camera/core/ViewPort`. |

## Constants

### FILL_CENTER

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

```
public static final int FILL_CENTER = 1
```

Generate a crop rect that once applied, it scales the output while maintaining its aspect ratio, so it fills the entire `https://developer.android.com/reference/androidx/camera/core/ViewPort` and center it.

This may cause the output to be cropped if the output aspect ratio does not match that of the `https://developer.android.com/reference/androidx/camera/core/ViewPort`.

### FILL_END

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

```
public static final int FILL_END = 2
```

Generate a crop rect that once applied, it scales the output while maintaining its aspect ratio, so it fills the entire `https://developer.android.com/reference/androidx/camera/core/ViewPort`, and align it to the end of the `https://developer.android.com/reference/androidx/camera/core/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 `https://developer.android.com/reference/androidx/camera/core/ViewPort`.

### FILL_START

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

```
public static final int FILL_START = 0
```

Generate a crop rect that once applied, it scales the output while maintaining its aspect ratio, so it fills the entire `https://developer.android.com/reference/androidx/camera/core/ViewPort`, and align it to the start of the `https://developer.android.com/reference/androidx/camera/core/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 `https://developer.android.com/reference/androidx/camera/core/ViewPort`.

### FIT

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

```
public static final int FIT = 3
```

Generate the max possible crop rect ignoring the aspect ratio. For `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis` and `https://developer.android.com/reference/androidx/camera/core/ImageCapture`, the output will be an image defined by the crop rect.

For `https://developer.android.com/reference/androidx/camera/core/Preview`, further calculation is needed to to fit the crop rect into the viewfinder. Code sample below is a simplified version assuming `https://developer.android.com/reference/android/view/Surface.html` orientation is the same as the camera sensor orientation, the viewfinder is a `https://developer.android.com/reference/android/view/SurfaceView.html` and the viewfinder's pixel width/height is the same as the size request by CameraX in `https://developer.android.com/reference/androidx/camera/core/SurfaceRequest#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 methods

### getAspectRatio

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

```
public @NonNull Rational getAspectRatio()
```

Gets the aspect ratio of the `https://developer.android.com/reference/androidx/camera/core/ViewPort`.

### getLayoutDirection

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/camera#1.1.0)

```
public int getLayoutDirection()
```

Gets the layout direction of the `https://developer.android.com/reference/androidx/camera/core/ViewPort`.

Value is one of the following:

- `android.util.LayoutDirection.LTR`
- `android.util.LayoutDirection.RTL`

<br />

### getRotation

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

```
public int getRotation()
```

Gets the rotation of the `https://developer.android.com/reference/androidx/camera/core/ViewPort`.

Value is one of the following:

- `Surface.ROTATION_0`
- `Surface.ROTATION_90`
- `Surface.ROTATION_180`
- `Surface.ROTATION_270`

<br />

### getScaleType

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

```
public int getScaleType()
```

Gets the scale type of the `https://developer.android.com/reference/androidx/camera/core/ViewPort`.