Capture an image

This page describes how to capture high-quality images with CameraX. You do so with the ImageCapture class and its associated methods.

Key concepts

The following are the primary concepts discussed in this document:

  • Storage method: You can capture images either to an in-memory buffer or directly to a file.
  • Executors: ImageCapture uses executors for handling callbacks and I/O operations. You can customize these executors for better performance and control.
  • Capture Modes: 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():

Executors

When you call takePicture, you pass an Executor and either a OnImageCapturedCallback or OnImageSavedCallback function. The Executor runs the callback and handles any resulting IO.

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. Then, create an ImageCapture object. Use ImageCapture.Builder():

Kotlin

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

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

Java

ImageCapture imageCapture =
    new ImageCapture.Builder()
        .setTargetRotation(view.getDisplay().getRotation())
        .build();

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

Take a picture

After you configure the camera, call takePicture() to capture an image. This example demonstrates how to use takePicture() to save an image to disk:

Kotlin

fun onClick() {
    val outputFileOptions = ImageCapture.OutputFileOptions.Builder(File(...)).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.
            }
        })
}

Java

public void onClick() {
    ImageCapture.OutputFileOptions outputFileOptions =
            new ImageCapture.OutputFileOptions.Builder(new File(...)).build();
    imageCapture.takePicture(outputFileOptions, cameraExecutor,
        new ImageCapture.OnImageSavedCallback() {
            @Override
            public void onImageSaved(ImageCapture.OutputFileResults outputFileResults) {
                // insert your code here.
            }
            @Override
            public void onError(ImageCaptureException error) {
                // insert your code here.
            }
       }
    );
}

Here are the key points about this snippet:

  • The ImageCapture.OutputFileOptions 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 provides callbacks for success and failure.
    • The onImageSaved() callback handles successful image capture and provides access to the saved image results.
    • The onError() callback handles image capture errors.

Additional options

See the Configure for optimization, flash, and file format guide for extra ways you can configure ImageCapture.

Further resources

To learn more about CameraX, consult the following resources:

Codelab

  • Getting Started with CameraX
  • Code sample

  • CameraX sample apps