# ViewPort.Builder

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.Builder) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

*** ** * ** ***


```
public final class ViewPort.Builder
```

<br />

*** ** * ** ***

Builder for `https://developer.android.com/reference/androidx/camera/core/ViewPort`.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/camera/core/ViewPort.Builder#Builder(android.util.Rational,int)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/util/Rational.html aspectRatio, int rotation)` Creates `https://developer.android.com/reference/androidx/camera/core/ViewPort.Builder` with aspect ratio and rotation. |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/camera/core/ViewPort` | `https://developer.android.com/reference/androidx/camera/core/ViewPort.Builder#build()()` Builds the `https://developer.android.com/reference/androidx/camera/core/ViewPort`. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/camera/core/ViewPort.Builder` | `https://developer.android.com/reference/androidx/camera/core/ViewPort.Builder#setLayoutDirection(int)(int layoutDirection)` Sets the layout direction of the `https://developer.android.com/reference/androidx/camera/core/ViewPort`. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/camera/core/ViewPort.Builder` | `https://developer.android.com/reference/androidx/camera/core/ViewPort.Builder#setScaleType(int)(int scaleType)` Sets the scale type of the `https://developer.android.com/reference/androidx/camera/core/ViewPort`. |

## Public constructors

### Builder

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

```
public Builder(@NonNull Rational aspectRatio, int rotation)
```

Creates `https://developer.android.com/reference/androidx/camera/core/ViewPort.Builder` with aspect ratio and rotation.

To create a `https://developer.android.com/reference/androidx/camera/core/ViewPort` that is based on the `https://developer.android.com/reference/androidx/camera/core/Preview` use case, the aspect ratio should be the dimension of the `https://developer.android.com/reference/android/view/View.html` and the rotation should be the value of `https://developer.android.com/reference/androidx/camera/core/Preview#getTargetRotation()`:

```
val aspectRatio = Rational(viewFinder.width, viewFinder.height)
val viewport = ViewPort.Builder(aspectRatio, preview.getTargetRotation()).build()
```

In a scenario where `https://developer.android.com/reference/androidx/camera/core/Preview` is not used, for example, face detection in `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis` and taking pictures with `https://developer.android.com/reference/androidx/camera/core/ImageCapture` when faces are found, the `https://developer.android.com/reference/androidx/camera/core/ViewPort` should be created with the aspect ratio and rotation of the `https://developer.android.com/reference/androidx/camera/core/ImageCapture` use case.

All `https://developer.android.com/reference/androidx/camera/core/UseCase`s have a configurable aspect ratio setting that determines the supported sizes for creating the capture session to receive images from the camera. See `https://developer.android.com/reference/androidx/camera/core/resolutionselector/ResolutionSelector.Builder#setAspectRatioStrategy(androidx.camera.core.resolutionselector.AspectRatioStrategy)` for `https://developer.android.com/reference/androidx/camera/core/Preview`, `https://developer.android.com/reference/androidx/camera/core/ImageCapture`, and `https://developer.android.com/reference/androidx/camera/core/ImageAnalysis`, and `https://developer.android.com/reference/androidx/camera/video/Recorder.Builder#setAspectRatio(int)` for `https://developer.android.com/reference/androidx/camera/video/VideoCapture`. This is distinct from the `https://developer.android.com/reference/androidx/camera/core/ViewPort` 's aspect ratio setting, which is used to calculate output crop rectangles among bound `https://developer.android.com/reference/androidx/camera/core/UseCase`s to ensure that they have the same content.

To obtain the maximum field of view (FOV) of the full camera sensor, it is recommended that the `https://developer.android.com/reference/androidx/camera/core/ViewPort` and the bound `https://developer.android.com/reference/androidx/camera/core/UseCase`s have matching aspect ratio settings. Otherwise, the output crop rectangles may be a double-cropped result from the full camera sensor FOV.

For example, if all `https://developer.android.com/reference/androidx/camera/core/UseCase`s have a 16:9 aspect ratio preference setting and are bound with a `https://developer.android.com/reference/androidx/camera/core/ViewPort` with a 4:3 aspect ratio, the images obtained from the camera will be 16:9 images cropped from the 4:3 camera sensor data. The content inside the output crop rectangles will be 4:3 cropped images from the 16:9 images cropped from the 4:3 camera sensor data. In other words, the images will be cropped twice: first to fit the 16:9 aspect ratio, and then to fit the 4:3 aspect ratio. This will result in a loss of FOV. To avoid this, it is important to ensure that the `https://developer.android.com/reference/androidx/camera/core/ViewPort` and the bound `https://developer.android.com/reference/androidx/camera/core/UseCase`s have matching aspect ratio settings.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/util/Rational.html aspectRatio` | aspect ratio of the output crop rect if the scale type is FILL_START, FILL_CENTER or FILL_END. This is usually the width/height of the preview viewfinder that displays the camera feed. The value is ignored if the scale type is FIT. |
| `int rotation` | The rotation value is one of four valid values: `https://developer.android.com/reference/android/view/Surface.html#ROTATION_0`, `https://developer.android.com/reference/android/view/Surface.html#ROTATION_90`, `https://developer.android.com/reference/android/view/Surface.html#ROTATION_180`, `https://developer.android.com/reference/android/view/Surface.html#ROTATION_270`. Value is one of the following: - `Surface.ROTATION_0` - `Surface.ROTATION_90` - `Surface.ROTATION_180` - `Surface.ROTATION_270` <br /> |

## Public methods

### build

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

```
public @NonNull ViewPort build()
```

Builds the `https://developer.android.com/reference/androidx/camera/core/ViewPort`.

### setLayoutDirection

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

```
public @NonNull ViewPort.Builder setLayoutDirection(int layoutDirection)
```

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

The layout direction decides the start and the end of the crop rect if the scale type is `https://developer.android.com/reference/androidx/camera/core/ViewPort#FILL_END()` or `https://developer.android.com/reference/androidx/camera/core/ViewPort#FILL_START()`.

The default value is `https://developer.android.com/reference/android/util/LayoutDirection.html#LTR` if not set.

### setScaleType

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

```
public @NonNull ViewPort.Builder setScaleType(int scaleType)
```

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

The value is used by `https://developer.android.com/reference/androidx/camera/core/UseCase` to calculate the crop rect.

The default value is `https://developer.android.com/reference/androidx/camera/core/ViewPort#FILL_CENTER()` if not set.