> Documentation index: https://developer.android.com/reference/llms.txt

# ImageCapture.Builder

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

*** ** * ** ***


```
public final class ImageCapture.Builder implements ExtendableBuilder
```

<br />

*** ** * ** ***

Builder for an `ImageCapture`.

## Summary

| ### Public constructors |
|---|
| `Builder()` Creates a new Builder object. |

| ### Public methods |
|---|---|
| `@NonNull ImageCapture` | `build()` Builds an immutable `ImageCapture` from the current state. |
| `@NonNull ImageCapture.Builder` | `setCaptureMode(int captureMode)` Sets the image capture mode. |
| `@NonNull ImageCapture.Builder` | `setFlashMode(int flashMode)` Sets the flashMode. |
| `@NonNull ImageCapture.Builder` | `setInterop( @NonNull InteropConfigurator<ImageCapture.Builder> configurator )` Applies interoperability configuration to this builder. |
| `@NonNull ImageCapture.Builder` | `setIoExecutor(@NonNull Executor executor)` Sets the default executor that will be used for IO tasks. |
| `@NonNull ImageCapture.Builder` | `setJpegQuality(@IntRange(from = 1, to = 100) int jpegQuality)` Sets the output JPEG image compression quality. |
| `@NonNull ImageCapture.Builder` | `setOutputFormat(int outputFormat)` Sets the output format of the captured image. |
| `@NonNull ImageCapture.Builder` | `setPostviewEnabled(boolean postviewEnabled)` Enables postview image generation. |
| `@NonNull ImageCapture.Builder` | `setPostviewResolutionSelector( @NonNull ResolutionSelector resolutionSelector )` Set the `ResolutionSelector` to select the postview size from the available postview sizes. |
| `@NonNull ImageCapture.Builder` | `setResolutionSelector(@NonNull ResolutionSelector resolutionSelector)` Sets the resolution selector to select the preferred supported resolution. |
| `@NonNull ImageCapture.Builder` | `setScreenFlash(@NonNull ImageCapture.ScreenFlash screenFlash)` Sets the `ScreenFlash` instance necessary for screen flash operations. |
| `@NonNull ImageCapture.Builder` | `[setTargetAspectRatio](https://developer.android.com/reference/androidx/camera/core/ImageCapture.Builder#setTargetAspectRatio(int))(int aspectRatio)` **This method is deprecated.** use `ResolutionSelector` with `AspectRatioStrategy` to specify the preferred aspect ratio settings instead. <br /> |
| `@NonNull ImageCapture.Builder` | `setTargetName(@NonNull String targetName)` Sets the name of the target object being configured, used only for debug logging. |
| `@NonNull ImageCapture.Builder` | `[setTargetResolution](https://developer.android.com/reference/androidx/camera/core/ImageCapture.Builder#setTargetResolution(android.util.Size))(@NonNull Size resolution)` **This method is deprecated.** use `ResolutionSelector` with `ResolutionStrategy` to specify the preferred resolution settings instead. <br /> |
| `@NonNull ImageCapture.Builder` | `setTargetRotation(int rotation)` Sets the rotation of the intended target for images from this configuration. |

| ### Extension functions |
|---|---|
| `final @NonNull ImageCapture.Builder` | `Camera2InteropEx.camera2Interop( @NonNull ImageCapture.Builder receiver, @NonNull Function1<@NonNull ImageCaptureCamera2Interop, Unit> block )` Applies Camera2 options to an `ImageCapture.Builder`. |

## Public constructors

### Builder

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

```
public Builder()
```

Creates a new Builder object.

## Public methods

### build

```
public @NonNull ImageCapture build()
```

Builds an immutable `ImageCapture` from the current state.

| Returns |
|---|---|
| `@NonNull ImageCapture` | A `ImageCapture` populated with the current state. |

| Throws |
|---|---|
| `java.lang.IllegalArgumentException` | if attempting to set both target aspect ratio and target resolution, or attempting to set `FLASH_MODE_SCREEN` without setting a non-null `ScreenFlash` instance. |

### setCaptureMode

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

```
public @NonNull ImageCapture.Builder setCaptureMode(int captureMode)
```

Sets the image capture mode.

Valid capture modes are `CAPTURE_MODE_MINIMIZE_LATENCY`, which prioritizes latency over image quality, or `CAPTURE_MODE_MAXIMIZE_QUALITY`, which prioritizes image quality over latency.

If not set, the capture mode will default to `CAPTURE_MODE_MINIMIZE_LATENCY`.

| Parameters |
|---|---|
| `int captureMode` | The requested image capture mode. |

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | The current Builder. |

### setFlashMode

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

```
public @NonNull ImageCapture.Builder setFlashMode(int flashMode)
```

Sets the flashMode.

If not set, the flash mode will default to `FLASH_MODE_OFF`.

If set to `FLASH_MODE_SCREEN`, a non-null `ScreenFlash` instance must also be set with `setScreenFlash`. Otherwise, an `IllegalArgumentException` will be thrown when `build` is invoked.

See `setFlashMode` for more information.

| Parameters |
|---|---|
| `int flashMode` | The requested flash mode. Value is `FLASH_MODE_AUTO`, `FLASH_MODE_ON`, `FLASH_MODE_SCREEN`, or `FLASH_MODE_OFF`. |

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | The current Builder. |

### setInterop

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

```
public @NonNull ImageCapture.Builder setInterop(
    @NonNull InteropConfigurator<ImageCapture.Builder> configurator
)
```

Applies interoperability configuration to this builder.

To configure Camera2 options, use `Camera2Interop.forImageCapture(configurator)` (from the `camera-camera2` artifact) to create a configurator, then pass it to this method.

**Note:** Using Camera2 interop options can override internal CameraX configurations. The capture request keys for one-shot still captures (e.g. `takePicture`) are determined by copying all repeating request keys (which may include keys added via `setInterop` or `applyInteropAsync`) and then overriding them with the still capture request keys configured here. If an option configured via interop conflicts with options required by CameraX internally, the option from Camera2Interop will override, which may result in unexpected behavior.

**Warning:** Callbacks configured via interop receive raw `android.hardware.camera2.CameraCaptureSession` instances. Directly invoking state-altering methods on these raw objects (such as `close` or `abortCaptures`) bypasses CameraX pipeline management and may cause state desynchronization, stream interruption, or application crashes.

| Parameters |
|---|---|
| `@NonNull InteropConfigurator<ImageCapture.Builder> configurator` | the configurator that sets the interoperability options |

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | this builder |

### setIoExecutor

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

```
public @NonNull ImageCapture.Builder setIoExecutor(@NonNull Executor executor)
```

Sets the default executor that will be used for IO tasks.

This executor will be used for any IO tasks specifically for ImageCapture, such as `takePicture`. If no executor is set, then a default Executor specifically for IO will be used instead.

| Parameters |
|---|---|
| `@NonNull Executor executor` | The executor which will be used for IO tasks. |

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | the current Builder. |

### setJpegQuality

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

```
public @NonNull ImageCapture.Builder setJpegQuality(@IntRange(from = 1, to = 100) int jpegQuality)
```

Sets the output JPEG image compression quality.

This is used for the `ImageProxy` which is returned by `takePicture` or the output JPEG image which is saved by `takePicture`. The saved JPEG image might be cropped according to the `ViewPort` setting or the crop aspect ratio set by `setCropAspectRatio`. The JPEG quality setting will also be used to compress the cropped output image.

If not set, a default value will be used according to the capture mode setting. JPEG compression quality 95 is used for `CAPTURE_MODE_MINIMIZE_LATENCY` and 100 is used for `CAPTURE_MODE_MAXIMIZE_QUALITY`.

| Parameters |
|---|---|
| `@IntRange(from = 1, to = 100) int jpegQuality` | The requested output JPEG image compression quality. The value must be in range \[1..100\] which larger is higher quality. |

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | The current Builder. |

| Throws |
|---|---|
| `java.lang.IllegalArgumentException` | if the input value is not in range \[1..100\]. |

### setOutputFormat

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

```
public @NonNull ImageCapture.Builder setOutputFormat(int outputFormat)
```

Sets the output format of the captured image.

The supported output formats for capturing image depend on the capabilities of the camera. The supported output formats of the camera can be queried using `getSupportedOutputFormats`.

If not set, the output format will default to `OUTPUT_FORMAT_JPEG`.

An `IllegalArgumentException` will be thrown when binding the UseCase if the specified output format is not supported. Please note that the supported output formats might be changed when Extensions is enabled.

| Parameters |
|---|---|
| `int outputFormat` | The output image format. Value is `OUTPUT_FORMAT_JPEG` or `OUTPUT_FORMAT_JPEG_ULTRA_HDR` or `OUTPUT_FORMAT_RAW`. |

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | The current Builder. |

| See also |
|---|---|
| `ImageCapture.OutputFormat` |   |
| `getSupportedOutputFormats` |   |

### setPostviewEnabled

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

```
public @NonNull ImageCapture.Builder setPostviewEnabled(boolean postviewEnabled)
```

Enables postview image generation. A postview image is a low-quality image that's produced earlier during image capture than the final high-quality image, and can be used as a thumbnail or placeholder until the final image is ready.

When the postview is available, `onPostviewBitmapAvailable` or `onPostviewBitmapAvailable` will be called.

By default the largest available postview size that is smaller or equal to the ImagaeCapture size will be used to configure the postview. The `ResolutionSelector` can also be used to select a specific size via `setPostviewResolutionSelector`.

You can query the postview capability by invoking `getImageCaptureCapabilities`. If `isPostviewSupported` returns false and you still enable the postview, the postview image won't be generated.

| Parameters |
|---|---|
| `boolean postviewEnabled` | whether postview is enabled or not |

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | the current Builder. |

### setPostviewResolutionSelector

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

```
public @NonNull ImageCapture.Builder setPostviewResolutionSelector(
    @NonNull ResolutionSelector resolutionSelector
)
```

Set the `ResolutionSelector` to select the postview size from the available postview sizes. These available postview sizes are smaller or equal to the ImageCapture size. You can implement the `androidx.camera.core.resolutionselector.ResolutionFilter` and set it to the `ResolutionSelector` to get the list of available sizes and determine which size to use.

If no sizes can be selected using the given `ResolutionSelector`, it will throw an `IllegalArgumentException` when `bindToLifecycle()` is invoked.

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | the current Builder. |

### setResolutionSelector

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

```
public @NonNull ImageCapture.Builder setResolutionSelector(@NonNull ResolutionSelector resolutionSelector)
```

Sets the resolution selector to select the preferred supported resolution.

The default resolution strategy for ImageCapture is `HIGHEST_AVAILABLE_STRATEGY`, which will select the largest available resolution to use. Applications can override this default strategy with a different resolution strategy.

The existing `setTargetResolution` and `setTargetAspectRatio` APIs are deprecated and are not compatible with `setResolutionSelector`. Calling either of these APIs together with `setResolutionSelector` will result in an `IllegalArgumentException` being thrown when you attempt to build the `ImageCapture` instance.

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | The current Builder. |

### setScreenFlash

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

```
public @NonNull ImageCapture.Builder setScreenFlash(@NonNull ImageCapture.ScreenFlash screenFlash)
```

Sets the `ScreenFlash` instance necessary for screen flash operations.

If not set, the instance will be set to null and users will need to set it later before calling `setFlashMode` with `FLASH_MODE_SCREEN`.

See `setScreenFlash` for more information.

| Parameters |
|---|---|
| `@NonNull ImageCapture.ScreenFlash screenFlash` | The `ScreenFlash` to notify caller for the UI side changes required for photo capture with `FLASH_MODE_SCREEN`. |

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | The current Builder. |

### setTargetAspectRatio

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

```
public @NonNull ImageCapture.Builder [setTargetAspectRatio](https://developer.android.com/reference/androidx/camera/core/ImageCapture.Builder#setTargetAspectRatio(int))(int aspectRatio)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> use `ResolutionSelector` with `AspectRatioStrategy` to specify the preferred aspect ratio settings instead.

Sets the aspect ratio of the intended target for images from this configuration.

The aspect ratio is the ratio of width to height in the sensor orientation.

It is not allowed to set both target aspect ratio and target resolution on the same use case. Attempting so will throw an IllegalArgumentException when building the Config.

The target aspect ratio is used as a hint when determining the resulting output aspect ratio which may differ from the request, possibly due to device constraints. Application code should check the resulting output's resolution and the resulting aspect ratio may not be exactly as requested.

If not set, or `RATIO_DEFAULT` is supplied, resolutions with aspect ratio 4:3 will be considered in higher priority.

| Parameters |
|---|---|
| `int aspectRatio` | The desired ImageCapture `AspectRatio` |

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | The current Builder. |

### setTargetName

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

```
public @NonNull ImageCapture.Builder setTargetName(@NonNull String targetName)
```

Sets the name of the target object being configured, used only for debug logging.

The name should be a value that can uniquely identify an instance of the object being configured.

If not set, the target name will default to a unique name automatically generated with the class canonical name and random UUID.

| Parameters |
|---|---|
| `@NonNull String targetName` | A unique string identifier for the instance of the class being configured. |

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | the current Builder. |

### setTargetResolution

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

```
public @NonNull ImageCapture.Builder [setTargetResolution](https://developer.android.com/reference/androidx/camera/core/ImageCapture.Builder#setTargetResolution(android.util.Size))(@NonNull Size resolution)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> use `ResolutionSelector` with `ResolutionStrategy` to specify the preferred resolution settings instead.

Sets the intended output target resolution.

The target resolution attempts to establish a minimum bound for the image resolution. The actual image resolution will be the closest available resolution in size that is not smaller than the target resolution, as determined by the Camera implementation. However, if no resolution exists that is equal to or larger than the target resolution, the nearest available resolution smaller than the target resolution will be chosen. Resolutions with the same aspect ratio of the provided `Size` will be considered in higher priority before resolutions of different aspect ratios.

It is not allowed to set both target aspect ratio and target resolution on the same use case. Attempting so will throw an IllegalArgumentException when building the Config.

The resolution `Size` should be expressed in the coordinate frame after rotating the supported sizes by the target rotation. For example, a device with portrait natural orientation in natural target rotation requesting a portrait image may specify 480x640, and the same device, rotated 90 degrees and targeting landscape orientation may specify 640x480.

When the target resolution is set, `setCropAspectRatio` will be automatically called to set corresponding value. Such that the output image will be cropped into the desired aspect ratio.

The maximum available resolution that could be selected for an `ImageCapture` will depend on the camera device's capability.

If not set, the largest available resolution will be selected to use. Usually, users will intend to get the largest still image that the camera device can support.

When using the `camera-camera2` CameraX implementation, which resolution will be finally selected will depend on the camera device's hardware level and the bound use cases combination. For more details see the guaranteed supported configurations tables in `android.hardware.camera2.CameraDevice`'s href="https://developer.android.com/reference/android/hardware/camera2/CameraDevice #regular-capture"\>Regular capture section.

| Parameters |
|---|---|
| `@NonNull Size resolution` | The target resolution to choose from supported output sizes list. |

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | The current Builder. |

### setTargetRotation

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

```
public @NonNull ImageCapture.Builder setTargetRotation(int rotation)
```

Sets the rotation of the intended target for images from this configuration.

This will affect the EXIF rotation metadata in images saved by takePicture calls and the `getRotationDegrees` value of the `ImageProxy` returned by `OnImageCapturedCallback`. These will be set to be the rotation, which if applied to the output image data, will make the image match the target rotation specified here.

This is one of four valid values: `ROTATION_0`, `ROTATION_90`, `ROTATION_180`, `ROTATION_270`. Rotation values are relative to the "natural" rotation, `ROTATION_0`.

In general, it is best to additionally set the target rotation dynamically on the use case. See `setTargetRotation` for additional documentation.

If not set, the target rotation will default to the value of `getRotation` of the default display at the time the use case is created. The use case is fully created once it has been attached to a camera.

| Parameters |
|---|---|
| `int rotation` | The rotation of the intended target. Value is one of the following: - `Surface.ROTATION_0` - `Surface.ROTATION_90` - `Surface.ROTATION_180` - `Surface.ROTATION_270` <br /> |

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | The current Builder. |

| See also |
|---|---|
| `setTargetRotation` |   |
| `OrientationEventListener` |   |

## Extension functions

### Camera2InteropEx.camera2Interop

Artifact: [androidx.camera:camera-camera2](https://developer.android.com/jetpack/androidx/releases/camera.md.txt) [View Source](https://cs.android.com/search?q=file:androidx/camera/camera2/interop/Camera2InteropEx.kt+function:camera2Interop)

```
public final @NonNull ImageCapture.Builder Camera2InteropEx.camera2Interop(
    @NonNull ImageCapture.Builder receiver,
    @NonNull Function1<@NonNull ImageCaptureCamera2Interop, Unit> block
)
```

Applies Camera2 options to an `ImageCapture.Builder`.

Configures Camera2 options specific to `ImageCapture.Builder`.

Options configured via `ImageCaptureCamera2Interop` include:

- Physical camera ID (`ImageCaptureCamera2Interop.physicalCameraId`)

- Stream use case (`ImageCaptureCamera2Interop.streamUseCase`)

- Mirror mode (`ImageCaptureCamera2Interop.mirrorMode`)

- Timestamp base (`ImageCaptureCamera2Interop.timestampBase`)

- Dynamic range profile (`ImageCaptureCamera2Interop.dynamicRangeProfile`)

- Surface group ID (`ImageCaptureCamera2Interop.surfaceGroupId`)

- Still capture request key-value pairs (`ImageCaptureCamera2Interop.stillCaptureRequest` via `stillCaptureRequest[...]` or `ImageCaptureCamera2Interop.setStillCaptureRequestOption`)

- Still capture request template type (`ImageCaptureCamera2Interop.stillCaptureRequestTemplateType`)

- Still capture callbacks (`ImageCaptureCamera2Interop.setStillCaptureCallback` or `ImageCaptureCamera2Interop.stillCaptureCallback`)

**Note:** Using Camera2 interop options can override internal CameraX configurations. The capture request keys for one-shot still captures (such as `androidx.camera.core.ImageCapture.takePicture`) are determined by copying all repeating request keys (which may include keys added via `androidx.camera.core.SessionConfig.Builder.setInterop` or `androidx.camera.core.CameraControl.applyInteropAsync`) and then overriding them with the still capture request keys configured here. If an option configured via interop conflicts with options required by CameraX internally, the option from Camera2Interop will override, which may result in unexpected behavior.

**Warning:** Callbacks configured via interop receive raw `android.hardware.camera2.CameraCaptureSession` instances. Directly invoking state-altering, lifecycle, or request-submitting methods on these raw objects bypasses CameraX pipeline management and causes state desynchronization, stream freezing, or crashes. Do not invoke:

- Session lifecycle methods: `android.hardware.camera2.CameraCaptureSession.close`, `android.hardware.camera2.CameraCaptureSession.abortCaptures`, or `android.hardware.camera2.CameraCaptureSession.stopRepeating`

- Request submission methods: `android.hardware.camera2.CameraCaptureSession.setRepeatingRequest`, `android.hardware.camera2.CameraCaptureSession.setRepeatingBurst`, `android.hardware.camera2.CameraCaptureSession.capture`, or `android.hardware.camera2.CameraCaptureSession.captureBurst`

- Surface / output configuration methods: `android.hardware.camera2.CameraCaptureSession.updateOutputConfiguration`, `android.hardware.camera2.CameraCaptureSession.finalizeOutputConfigurations`, `android.hardware.camera2.CameraCaptureSession.prepare`, or `android.hardware.camera2.CameraCaptureSession.switchToOffline`

```kotlin
import androidx.camera.camera2.interop.camera2Interop
import androidx.camera.core.ImageCapture
import androidx.camera.core.imageCapture

val imageCapture =
    ImageCapture.Builder()
        .camera2Interop {
            stillCaptureRequestTemplateType = CameraDevice.TEMPLATE_STILL_CAPTURE
            stillCaptureRequest[CaptureRequest.CONTROL_AF_MODE] =
                CaptureRequest.CONTROL_AF_MODE_OFF
            stillCaptureCallback =
                object : CameraCaptureSession.CaptureCallback() {
                    override fun onCaptureCompleted(
                        session: CameraCaptureSession,
                        request: CaptureRequest,
                        result: TotalCaptureResult,
                    ) {}
                }
            physicalCameraId = "5"
        }
        .build()
```

| Parameters |
|---|---|
| `@NonNull Function1<@NonNull ImageCaptureCamera2Interop, Unit> block` | configuration block setting Camera2 options on `ImageCaptureCamera2Interop` |

| Returns |
|---|---|
| `@NonNull ImageCapture.Builder` | this builder |