This is useful for real-time image processing or analysis.
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.
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
funonClick(){valoutputFileOptions=ImageCapture.OutputFileOptions.Builder(File(...)).build()imageCapture.takePicture(outputFileOptions,cameraExecutor,object:ImageCapture.OnImageSavedCallback{overridefunonError(error:ImageCaptureException){// insert your code here.}overridefunonImageSaved(outputFileResults:ImageCapture.OutputFileResults){// insert your code here.}})}
Java
publicvoidonClick(){ImageCapture.OutputFileOptionsoutputFileOptions=newImageCapture.OutputFileOptions.Builder(newFile(...)).build();imageCapture.takePicture(outputFileOptions,cameraExecutor,newImageCapture.OnImageSavedCallback(){@OverridepublicvoidonImageSaved(ImageCapture.OutputFileResultsoutputFileResults){// insert your code here.}@OverridepublicvoidonError(ImageCaptureExceptionerror){// insert your code here.}});}
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-03-04 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-03-04 UTC."],[],[],null,["# Capture an image\n\nThis page describes how to capture high-quality images with CameraX. You do so\nwith the [`ImageCapture`](/reference/androidx/camera/core/ImageCapture) class and its associated methods.\n| **Note:** This workflow accommodates the 3A features: auto-white-balance, auto-exposure, and auto-focus. It also supports basic manual camera controls.\n\nKey concepts\n------------\n\nThe following are the primary concepts discussed in this document:\n\n- **[Storage method](#capture):** You can capture images either to an in-memory buffer or directly to a file.\n- **[Executors](#executors):** `ImageCapture` uses executors for handling callbacks and I/O operations. You can customize these executors for better performance and control.\n- **[Capture Modes](/media/camera/camerax/take-photo/options):** You can configure the capture mode to optimize for either latency or image quality.\n\n### Storage method\n\nThere are two ways to capture images with\n`ImageCapture`. They each use an overload of `ImageCapture.takePicture()`:\n\n- **File:** Use [`takePicture(OutputFileOptions, Executor,\n OnImageSavedCallback)`](/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\n disk.\n\n - This is the most common way to capture photos.\n- **In-Memory:** Use [`takePicture(Executor,\n OnImageCapturedCallback)`](/reference/androidx/camera/core/ImageCapture#takePicture(java.util.concurrent.Executor,%20androidx.camera.core.ImageCapture.OnImageCapturedCallback)) to receive an in-memory buffer of the captured\n image.\n\n - This is useful for real-time image processing or analysis.\n\n### Executors\n\nWhen you call `takePicture`, you pass an [`Executor`](/reference/java/util/concurrent/Executor) and either a\n`OnImageCapturedCallback` or `OnImageSavedCallback` function. The `Executor`\nruns the callback and handles any resulting IO.\n| **Note:** You can set the default executor for IO tasks with [`ImageCapture.Builder.setIoExecutor(Executor)`](/reference/androidx/camera/core/ImageCapture.Builder#setIoExecutor(java.util.concurrent.Executor)). If you don't set a default, CameraX uses to an internal IO executor.\n\nTake photo\n----------\n\nTo take a photo, you set up the camera and then call `takePicture`.\n\n### Set up the camera\n\nTo set up the camera, create a [`CameraProvider`](/media/camera/camerax/preview#get-provider). Then, create an\n`ImageCapture` object. Use [`ImageCapture.Builder()`](/reference/androidx/camera/core/ImageCapture.Builder): \n\n### Kotlin\n\n val imageCapture = ImageCapture.Builder()\n .setTargetRotation(view.display.rotation)\n .build()\n\n cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, imageCapture, preview)\n\n### Java\n\n ImageCapture imageCapture =\n new ImageCapture.Builder()\n .setTargetRotation(view.getDisplay().getRotation())\n .build();\n\n cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, imageCapture, preview);\n\n| **Note:** [`bindToLifecycle()`](/reference/androidx/camera/lifecycle/ProcessCameraProvider#bindToLifecycle(androidx.lifecycle.LifecycleOwner,%20androidx.camera.core.CameraSelector,%20androidx.camera.core.UseCase...)) returns a [`Camera`](/reference/androidx/camera/core/Camera) object. See the [Configuration options guide](/training/camerax/configuration#camera-output) for more on controlling camera output, such as zoom and exposure.\n\n### Take a picture\n\nAfter you configure the camera, call `takePicture()` to capture an image.\nThis example demonstrates how to use [`takePicture()`](/reference/androidx/camera/core/ImageCapture#takePicture(androidx.camera.core.ImageCapture.OutputFileOptions,%20java.util.concurrent.Executor,%20androidx.camera.core.ImageCapture.OnImageSavedCallback)) to save an image\nto disk: \n\n### Kotlin\n\n fun onClick() {\n val outputFileOptions = ImageCapture.OutputFileOptions.Builder(File(...)).build()\n imageCapture.takePicture(outputFileOptions, cameraExecutor,\n object : ImageCapture.OnImageSavedCallback {\n override fun onError(error: ImageCaptureException)\n {\n // insert your code here.\n }\n override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {\n // insert your code here.\n }\n })\n }\n\n### Java\n\n public void onClick() {\n ImageCapture.OutputFileOptions outputFileOptions =\n new ImageCapture.OutputFileOptions.Builder(new File(...)).build();\n imageCapture.takePicture(outputFileOptions, cameraExecutor,\n new ImageCapture.OnImageSavedCallback() {\n @Override\n public void onImageSaved(ImageCapture.OutputFileResults outputFileResults) {\n // insert your code here.\n }\n @Override\n public void onError(ImageCaptureException error) {\n // insert your code here.\n }\n }\n );\n }\n\nHere are the key points about this snippet:\n\n- The [`ImageCapture.OutputFileOptions`](/reference/androidx/camera/core/ImageCapture.OutputFileOptions) lets you configure save location and metadata.\n - Here, the `OutputFileOptions.Builder()` uses a `File` object to determine the save location.\n- The `takePicture()` function captures the image asynchronously using the provided options and executor.\n- The [`OnImageSavedCallback`](/reference/androidx/camera/core/ImageCapture#takePicture(androidx.camera.core.ImageCapture.OutputFileOptions,%20java.util.concurrent.Executor,%20androidx.camera.core.ImageCapture.OnImageSavedCallback)) provides callbacks for success and failure.\n - The `onImageSaved()` callback handles successful image capture and provides access to the [saved image results](/reference/androidx/camera/core/ImageCapture.OutputFileResults).\n - The `onError()` callback handles image capture errors.\n\nAdditional options\n------------------\n\nSee the [Configure for optimization, flash, and file format guide](/media/camera/camerax/take-photo/options) for extra\nways you can configure `ImageCapture`.\n\nFurther resources\n-----------------\n\nTo learn more about CameraX, consult the following resources:\n\n### Codelab\n\n\n- [Getting Started with CameraX](https://codelabs.developers.google.com/codelabs/camerax-getting-started)\n\n### Code sample\n\n- \n- [CameraX sample apps](https://github.com/android/camera-samples/)"]]