This page describes how to capture high-quality images with CameraX. You do so
with the [`ImageCapture`](https://developer.android.com/reference/androidx/camera/core/ImageCapture.md.txt) class and its associated methods.

> [!NOTE]
> **Note:** This workflow accommodates the 3A features: auto-white-balance, auto-exposure, and auto-focus. It also supports basic manual camera controls.

## Key concepts

The following are the primary concepts discussed in this document:

- **[Storage method](https://developer.android.com/media/camera/camerax/take-photo.md.txt#capture):** You can capture images either to an in-memory buffer or directly to a file.
- **[Executors](https://developer.android.com/media/camera/camerax/take-photo.md.txt#executors):** `ImageCapture` uses executors for handling callbacks and I/O operations. You can customize these executors for better performance and control.
- **[Capture Modes](https://developer.android.com/media/camera/camerax/take-photo/options.md.txt):** You can configure the capture mode to optimize for either latency or image quality.

### Storage method

There are two ways to capture images with
`ImageCapture`. They each use an overload of `ImageCapture.takePicture()`:

- **File:** Use [`takePicture(OutputFileOptions, Executor,
  OnImageSavedCallback)`](https://developer.android.com/reference/androidx/camera/core/ImageCapture#takePicture(androidx.camera.core.ImageCapture.OutputFileOptions,%20java.util.concurrent.Executor,%20androidx.camera.core.ImageCapture.OnImageSavedCallback)) to save the captured image directly to a file on
  disk.

  - This is the most common way to capture photos.
- **In-Memory:** Use [`takePicture(Executor,
  OnImageCapturedCallback)`](https://developer.android.com/reference/androidx/camera/core/ImageCapture#takePicture(java.util.concurrent.Executor,%20androidx.camera.core.ImageCapture.OnImageCapturedCallback)) to receive an in-memory buffer of the captured
  image.

  - This is useful for real-time image processing or analysis.

### Executors

When you call `takePicture`, you pass an [`Executor`](https://developer.android.com/reference/java/util/concurrent/Executor.md.txt) and either a
`OnImageCapturedCallback` or `OnImageSavedCallback` function. The `Executor`
runs the callback and handles any resulting IO.

> [!NOTE]
> **Note:** You can set the default executor for IO tasks with [`ImageCapture.Builder.setIoExecutor(Executor)`](https://developer.android.com/reference/androidx/camera/core/ImageCapture.Builder.md.txt#setIoExecutor(java.util.concurrent.Executor)). If you don't set a default, CameraX uses to an internal IO executor.

## Take photo

To take a photo, you set up the camera and then call `takePicture`.

### Set up the camera

To set up the camera, create a [`CameraProvider`](https://developer.android.com/media/camera/camerax/preview.md.txt#get-provider). Then, create an
`ImageCapture` object. Use [`ImageCapture.Builder()`](https://developer.android.com/reference/androidx/camera/core/ImageCapture.Builder.md.txt):


```kotlin
val imageCapture = ImageCapture.Builder()
    .setTargetRotation(view.display.rotation)
    .build()

cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, imageCapture, preview)
```

<br />

> [!NOTE]
> **Note:** [`bindToLifecycle()`](https://developer.android.com/reference/androidx/camera/lifecycle/ProcessCameraProvider.md.txt#bindToLifecycle(androidx.lifecycle.LifecycleOwner,%20androidx.camera.core.CameraSelector,%20androidx.camera.core.UseCase...)) returns a [`Camera`](https://developer.android.com/reference/androidx/camera/core/Camera.md.txt) object. See the [Configuration options guide](https://developer.android.com/training/camerax/configuration#camera-output) for more on controlling camera output, such as zoom and exposure.

### Take a picture

After you configure the camera, call `takePicture()` to capture an image.
This example demonstrates how to use [`takePicture()`](https://developer.android.com/reference/androidx/camera/core/ImageCapture.md.txt#takePicture(androidx.camera.core.ImageCapture.OutputFileOptions,%20java.util.concurrent.Executor,%20androidx.camera.core.ImageCapture.OnImageSavedCallback)) to save an image
to disk:


```kotlin
fun onClick() {
    val outputFileOptions = ImageCapture.OutputFileOptions.Builder(File("photo.jpg")).build()
    imageCapture.takePicture(
        outputFileOptions, cameraExecutor,
        object : ImageCapture.OnImageSavedCallback {
            override fun onError(error: ImageCaptureException) {
                // Insert your code here
            }
            override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
                // Insert your code here
            }
        }
    )
}
```

<br />

Here are the key points about this snippet:

- The [`ImageCapture.OutputFileOptions`](https://developer.android.com/reference/androidx/camera/core/ImageCapture.OutputFileOptions.md.txt) lets you configure save location and metadata.
  - Here, the `OutputFileOptions.Builder()` uses a `File` object to determine the save location.
- The `takePicture()` function captures the image asynchronously using the provided options and executor.
- The [`OnImageSavedCallback`](https://developer.android.com/reference/androidx/camera/core/ImageCapture.md.txt#takePicture(androidx.camera.core.ImageCapture.OutputFileOptions,%20java.util.concurrent.Executor,%20androidx.camera.core.ImageCapture.OnImageSavedCallback)) provides callbacks for success and failure.
  - The `onImageSaved()` callback handles successful image capture and provides access to the [saved image results](https://developer.android.com/reference/androidx/camera/core/ImageCapture.OutputFileResults.md.txt).
  - The `onError()` callback handles image capture errors.

## Additional options

See the [Configure for optimization, flash, and file format guide](https://developer.android.com/media/camera/camerax/take-photo/options.md.txt) for extra
ways you can configure `ImageCapture`.

## Further resources

To learn more about CameraX, consult the following resources:

### Codelab


- [Getting Started with CameraX](https://codelabs.developers.google.com/codelabs/camerax-getting-started)

### Code sample

- 
- [CameraX sample apps](https://github.com/android/camera-samples/)