The image capture use case is designed for capturing high-resolution, high-quality photos and provides auto-white-balance, auto-exposure, and auto-focus (3A) functionality, in additional to simple manual camera controls. The caller is responsible for deciding how to use the captured picture, including the following options:
takePicture(OnImageCapturedListener)
: This method provides an in-memory buffer of the captured image.takePicture(File, OnImageSavedListener)
: This method saves the captured image to the provided file location.takePicture(File, OnImageSavedListener, Metadata)
: This method enables you to specify the metadata to embed in the saved file's Exif.
Implementation
Basic controls for taking pictures are provided. Pictures are taken with flash options and using continuous auto-focus.
To optimize photo capture for latency, set ImageCapture.CaptureMode
to
MIN_LATENCY
. To optimize for quality, set it to MAX_QUALITY
.
The following code sample shows how to configure your app to take a photo:
Kotlin
val imageCaptureConfig = ImageCaptureConfig.Builder() .setTargetRotation(windowManager.defaultDisplay.rotation) .build() val imageCapture = ImageCapture(imageCaptureConfig) CameraX.bindToLifecycle(this as LifecycleOwner, imageCapture, imageAnalysis, preview)
Java
ImageCaptureConfig config = new ImageCaptureConfig.Builder() .setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()) .build(); ImagesCapture imageCapture = new ImageCapture(config); CameraX.bindToLifecycle((LifecycleOwner) this, imageCapture, imageAnalysis, preview);
Once you've configured the camera, the following code takes a photo based on user action:
Kotlin
fun onClick() { val file = File(...) imageCapture.takePicture(file, object : ImageCapture.OnImageSavedListener { override fun onError(error: ImageCapture.ImageCaptureError, message: String, exc: Throwable?) { // insert your code here. } override fun onImageSaved(file: File) { // insert your code here. } }) }
Java
public void onClick() { File file = new File(...); imageCapture.takePicture(file, new ImageCapture.OnImageSavedListener() { @Override public void onImageSaved(File file) { // insert your code here. } @Override public void onError( ImageCapture.ImageCaptureError imageCaptureError, String message, Throwable cause) { // insert your code here. } } }
The image capture method fully supports the JPEG
format.
Additional resources
To learn more about CameraX, consult the following additional resources.
Codelab
Code sample