ImageCapture
public final class ImageCapture extends UseCase
A use case for taking a picture.
This class is designed for basic picture taking. It provides takePicture() functions to take a picture to memory or save to a file, and provides image metadata. Pictures are taken in automatic mode after focus has converged. The flash mode can additionally be set by the application.
TakePicture returns immediately and a listener is called to provide the results after the capture completes. Multiple calls to takePicture will take pictures sequentially starting after the previous picture is captured.
Note that focus and exposure metering regions can be controlled via Preview.
When capturing to memory, the captured image is made available through an ImageProxy via an ImageCapture.OnImageCapturedCallback.
Summary
Nested types |
|---|
public final class ImageCapture.Builder implements ExtendableBuilderBuilder for an |
public final class ImageCapture.MetadataHolder class for metadata that will be saved with captured images. |
public abstract class ImageCapture.OnImageCapturedCallbackCallback for image capture events. |
public interface ImageCapture.OnImageSavedCallbackListener containing callbacks for image file I/O events. |
public final class ImageCapture.OutputFileOptionsOptions for saving newly captured image. |
public final class ImageCapture.OutputFileOptions.BuilderBuilder class for |
public class ImageCapture.OutputFileResultsInfo about the saved image file. |
public interface ImageCapture.ScreenFlashInterface to do the application changes required for screen flash operations. |
public interface ImageCapture.ScreenFlashListenerCallback listener for discovering when the application has completed its changes for a screen flash image capture. |
Constants |
|
|---|---|
static final int |
Optimizes capture pipeline to prioritize image quality over latency. |
static final int |
Optimizes capture pipeline to prioritize latency over image quality. |
static final int |
Optimizes capture pipeline to have better latency while keeping good image quality. |
static final int |
An error indicating the request cannot be done due to camera is closed. |
static final int |
An error reported by camera framework indicating the capture request is failed. |
static final int |
ERROR_FILE_IO = 1An error occurred while attempting to read or write a file, such as when saving an image to a File. |
static final int |
An error indicating this ImageCapture is not bound to a valid camera. |
static final int |
ERROR_UNKNOWN = 0An unknown error occurred. |
static final int |
FLASH_MODE_AUTO = 0Auto flash. |
static final int |
FLASH_MODE_OFF = 2No flash. |
static final int |
FLASH_MODE_ON = 1Always flash. |
static final int |
Screen flash. |
static final int |
Captures 8-bit standard dynamic range (SDR) images using the |
static final int |
Captures Ultra HDR compressed images using the |
static final int |
Captures raw images in the |
static final int |
Captures raw images in the |
Public methods |
|
|---|---|
int |
Returns the set capture mode. |
int |
Get the flash mode. |
static @NonNull ImageCaptureCapabilities |
getImageCaptureCapabilities(@NonNull CameraInfo cameraInfo)Returns |
@IntRange(from = 1, to = 100) int |
Returns the JPEG quality setting. |
int |
Returns the output format setting. |
@Nullable ResolutionSelector |
Returns the |
@NonNull ImageCaptureLatencyEstimate |
Returns an estimate of the capture and processing sequence duration based on the current camera configuration and scene conditions. |
@Nullable ResolutionInfo |
Gets selected resolution information of the |
@Nullable ResolutionSelector |
Returns the resolution selector setting. |
@Nullable ImageCapture.ScreenFlash |
Returns the |
int |
Returns the desired rotation of the output image. |
boolean |
Returns if postview is enabled or not. |
void |
setCropAspectRatio(@NonNull Rational aspectRatio)Sets target cropping aspect ratio for output image. |
void |
setFlashMode(int flashMode)Set the flash mode. |
void |
setScreenFlash(@Nullable ImageCapture.ScreenFlash screenFlash)Sets |
void |
setTargetRotation(int rotation)Sets the desired rotation of the output image. |
void |
takePicture(Captures a new still image for in memory access. |
void |
takePicture(Captures a new still image and saves to a file along with application specified metadata. |
void |
takePicture(Captures two still images simultaneously and saves to a file along with application specified metadata. |
@NonNull String |
toString() |
Extension functions |
|
|---|---|
final @NonNull ImageProxy |
ImageCaptureExtKt.takePicture(Captures a new still image for in memory access. |
final @NonNull ImageCapture.OutputFileResults |
ImageCaptureExtKt.takePicture(Captures a new still image and saves to a file along with application specified metadata. |
Inherited methods |
||
|---|---|---|
|
Constants
CAPTURE_MODE_MAXIMIZE_QUALITY
public static final int CAPTURE_MODE_MAXIMIZE_QUALITY = 0
Optimizes capture pipeline to prioritize image quality over latency. When the capture mode is set to MAX_QUALITY, images may take longer to capture.
CAPTURE_MODE_MINIMIZE_LATENCY
public static final int CAPTURE_MODE_MINIMIZE_LATENCY = 1
Optimizes capture pipeline to prioritize latency over image quality. When the capture mode is set to MIN_LATENCY, images may capture faster but the image quality may be reduced.
CAPTURE_MODE_ZERO_SHUTTER_LAG
@ExperimentalZeroShutterLag
public static final int CAPTURE_MODE_ZERO_SHUTTER_LAG = 2
Optimizes capture pipeline to have better latency while keeping good image quality. When the capture mode is set to ZERO_SHUTTER_LAG, the latency between the shutter button is clicked and the picture is taken is expected to be minimized, compared with other capture modes.
ZERO_SHUTTER_LAG mode is aiming to provide the minimum latency for instant capture. It caches intermediate results and deliver the one with the closest timestamp when takePicture is invoked.
isZslSupported can be used to query the device capability to support this mode or not. However, this mode also depends on use cases configuration and flash mode settings. If VideoCapture is bound or flash mode is not OFF or OEM Extension is ON, this mode will be disabled automatically.
ERROR_CAMERA_CLOSED
public static final int ERROR_CAMERA_CLOSED = 3
An error indicating the request cannot be done due to camera is closed.
ERROR_CAPTURE_FAILED
public static final int ERROR_CAPTURE_FAILED = 2
An error reported by camera framework indicating the capture request is failed.
ERROR_FILE_IO
public static final int ERROR_FILE_IO = 1
An error occurred while attempting to read or write a file, such as when saving an image to a File.
ERROR_INVALID_CAMERA
public static final int ERROR_INVALID_CAMERA = 4
An error indicating this ImageCapture is not bound to a valid camera.
ERROR_UNKNOWN
public static final int ERROR_UNKNOWN = 0
An unknown error occurred.
See message parameter in onError callback or log for more details.
FLASH_MODE_AUTO
public static final int FLASH_MODE_AUTO = 0
Auto flash. The flash will be used according to the camera system's determination when taking a picture.
FLASH_MODE_OFF
public static final int FLASH_MODE_OFF = 2
No flash. The flash will never be used when taking a picture.
FLASH_MODE_ON
public static final int FLASH_MODE_ON = 1
Always flash. The flash will always be used when taking a picture.
FLASH_MODE_SCREEN
public static final int FLASH_MODE_SCREEN = 3
Screen flash. Display screen brightness will be used as alternative to flash when taking a picture with front camera.
This flash mode can be set via setFlashMode after setting a non-null ScreenFlash instance with setScreenFlash. This mode will always invoke all the necessary operations for a screen flash image capture, i.e. it is similar to FLASH_MODE_ON, not FLASH_MODE_AUTO.
The following code snippet shows an example implementation of how this flash mode can be set to an ImageCapture instance.
imageCapture.setScreenFlash(new ImageCapture.ScreenFlash() {
public void apply(long expirationTimeMillis,
ScreenFlashListener screenFlashListener) {
whiteColorOverlayView.setVisibility(View.VISIBLE);
maximizeScreenBrightness();
screenFlashListener.onCompleted();
}
public void clear() {
restoreScreenBrightness();
whiteColorOverlayView.setVisibility(View.INVISIBLE);
}
});
imageCapture.setFlashMode(ImageCapture.FLASH_MODE_SCREEN);| See also | |
|---|---|
setFlashMode |
OUTPUT_FORMAT_JPEG
public static final int OUTPUT_FORMAT_JPEG = 0
Captures 8-bit standard dynamic range (SDR) images using the JPEG image format.
OUTPUT_FORMAT_JPEG_ULTRA_HDR
public static final int OUTPUT_FORMAT_JPEG_ULTRA_HDR = 1
Captures Ultra HDR compressed images using the JPEG_R image format.
This format is backward compatible with SDR JPEG images and supports HDR rendering of content. This means that on older apps or devices, images appear seamlessly as regular JPEG; on apps and devices that have been updated to fully support the format, images appear as HDR.
For more information see Support Ultra HDR.
OUTPUT_FORMAT_RAW
public static final int OUTPUT_FORMAT_RAW = 2
Captures raw images in the RAW_SENSOR image format.
OUTPUT_FORMAT_RAW_JPEG
public static final int OUTPUT_FORMAT_RAW_JPEG = 3
Captures raw images in the RAW_SENSOR and JPEG image formats.
Public methods
getCaptureMode
public int getCaptureMode()
Returns the set capture mode.
This is set when constructing an ImageCapture using setCaptureMode. This is static for an instance of ImageCapture.
getFlashMode
public int getFlashMode()
Get the flash mode.
| Returns | |
|---|---|
int |
the flashMode. Value is |
getImageCaptureCapabilities
public static @NonNull ImageCaptureCapabilities getImageCaptureCapabilities(@NonNull CameraInfo cameraInfo)
Returns ImageCaptureCapabilities to query ImageCapture capability of the given CameraInfo.
Some capabilities are only exposed on Extensions-enabled cameras. To get the correct capabilities when Extensions are enabled, you need to pass the CameraInfo from the Extensions-enabled Camera instance. To do this, use the CameraSelector instance retrieved from getExtensionEnabledCameraSelector to invoke bindToLifecycle where you can skip use cases arguments if you'd like to query it before opening the camera. Then, use the returned Camera to get the CameraInfo instance.
>The following code snippet demonstrates how to enable postview:
CameraSelector extensionCameraSelector = extensionsManager.getExtensionEnabledCameraSelector(cameraSelector, ExtensionMode.NIGHT); Camera camera = cameraProvider.bindToLifecycle(activity, extensionCameraSelector); ImageCaptureCapabilities capabilities = ImageCapture.getImageCaptureCapabilities(camera.getCameraInfo()); ImageCapture imageCapture = new ImageCapture.Builder() .setPostviewEnabled(capabilities.isPostviewSupported()) .build();
getJpegQuality
public @IntRange(from = 1, to = 100) int getJpegQuality()
Returns the JPEG quality setting.
This is set when constructing an ImageCapture using setJpegQuality. If not set, a default value will be set according to the capture mode setting. JPEG compression quality 95 is set for CAPTURE_MODE_MINIMIZE_LATENCY and 100 is set for CAPTURE_MODE_MAXIMIZE_QUALITY. This is static for an instance of ImageCapture.
getOutputFormat
public int getOutputFormat()
Returns the output format setting.
If the output format was not provided to setOutputFormat, this will return the default of OUTPUT_FORMAT_JPEG.
| Returns | |
|---|---|
int |
the output format set for this |
| See also | |
|---|---|
setOutputFormat |
getPostviewResolutionSelector
public @Nullable ResolutionSelector getPostviewResolutionSelector()
Returns the ResolutionSelector used to select the postview size.
| See also | |
|---|---|
setPostviewResolutionSelector |
getRealtimeCaptureLatencyEstimate
public @NonNull ImageCaptureLatencyEstimate getRealtimeCaptureLatencyEstimate()
Returns an estimate of the capture and processing sequence duration based on the current camera configuration and scene conditions. The value will vary as the scene and/or camera configuration change.
The processing estimate can vary based on device processing load.
If this method returns UNDEFINED_IMAGE_CAPTURE_LATENCY, it means that the camera HAL doesn't provide latency information. In this case, this method will consistently return UNDEFINED_IMAGE_CAPTURE_LATENCY for the current camera configuration, as long as the UseCase and camera configuration remain unchanged (e.g., extensions mode and camera settings are kept the same).
getResolutionInfo
public @Nullable ResolutionInfo getResolutionInfo()
Gets selected resolution information of the ImageCapture.
The returned ResolutionInfo will be expressed in the coordinates of the camera sensor. Note that the resolution might not be the same as the resolution of the received image by calling takePicture because the received image might have been rotated to the upright orientation using the target rotation setting by the device.
The resolution information might change if the use case is unbound and then rebound, setTargetRotation is called to change the target rotation setting, or setCropAspectRatio is called to change the crop aspect ratio setting. The application needs to call getResolutionInfo() again to get the latest ResolutionInfo for the changes.
| Returns | |
|---|---|
@Nullable ResolutionInfo |
the resolution information if the use case has been bound by the |
getResolutionSelector
public @Nullable ResolutionSelector getResolutionSelector()
Returns the resolution selector setting.
This setting is set when constructing an ImageCapture using setResolutionSelector.
getScreenFlash
public @Nullable ImageCapture.ScreenFlash getScreenFlash()
Returns the ScreenFlash instance currently set, null if none.
getTargetRotation
public int getTargetRotation()
Returns the desired rotation of the output image.
The rotation can be set prior to constructing an ImageCapture using setTargetRotation or dynamically by calling setTargetRotation. The rotation of an image taken is determined by the rotation value set at the time image capture is initiated, such as when calling takePicture.
If no target rotation is set by the application, it is set 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.
| Returns | |
|---|---|
int |
The rotation of the intended target. |
isPostviewEnabled
public boolean isPostviewEnabled()
Returns if postview is enabled or not.
| See also | |
|---|---|
setPostviewEnabled |
setCropAspectRatio
public void setCropAspectRatio(@NonNull Rational aspectRatio)
Sets target cropping aspect ratio for output image.
This aspect ratio is orientation-dependent. It should be expressed in the coordinate frame after rotating the image by the target rotation.
This sets the cropping rectangle returned by getCropRect returned from takePicture.
For example, assume the aspectRatio of 3x4. If an image has a resolution of 480x640 after applying the target rotation, then the output ImageProxy of takePicture would have a cropping rectangle of 480x640 after applying the rotation degrees. However, if an image has a resolution of 640x480 after applying the target rotation, then the cropping rectangle of the output ImageProxy would be 360x480 after applying the rotation degrees.
This crops the saved image when calling takePicture. Note that the cropping will introduce an additional latency.
Cropping occurs around the center of the image and as though it were in the target rotation. For example, assume the aspectRatio of 3x4. If an image has a resolution of 480x640 after applying the target rotation, then the saved output image would be 480x640 after applying the EXIF orientation value. However, if an image has a resolution of 640x480 after applying the target rotation, then the saved output image would be 360x480 after applying the EXIF orientation value.
This setting value will be automatically updated to match the new target rotation value when setTargetRotation is called.
setFlashMode
public void setFlashMode(int flashMode)
Set the flash mode.
The flash control for the subsequent photo capture requests. Applications can check if there is a flash unit via hasFlashUnit and update UI component if necessary. If there is no flash unit and flashMode is not FLASH_MODE_SCREEN, then calling this API will take no effect for the subsequent photo capture requests and they will act like FLASH_MODE_OFF.
When the torch is enabled via enableTorch, the torch will remain enabled during photo capture regardless of flashMode setting. When the torch is disabled, flash will function as specified by setFlashMode(int).
On some LEGACY devices like Samsung A3, taking pictures with FLASH_MODE_AUTO mode could cause a crash. To workaround this CameraX will disable the auto flash behavior internally on devices that have this issue.
If FLASH_MODE_SCREEN is set, a ScreenFlash implementation must be set via setScreenFlash before calling this API. Trying to use FLASH_MODE_SCREEN without a ScreenFlash instance set or with a non-front camera will result in an IllegalArgumentException. It is the application's responsibility to change flashMode while switching the camera in case it leads to a non-supported case (e.g. switching to rear camera while FLASH_MODE_SCREEN is still on).
| Parameters | |
|---|---|
int flashMode |
the flash mode. Value is |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
If flash mode is invalid or FLASH_MODE_SCREEN is used without a |
setScreenFlash
public void setScreenFlash(@Nullable ImageCapture.ScreenFlash screenFlash)
Sets ScreenFlash for subsequent photo capture requests.
The calling of this API will take effect for FLASH_MODE_SCREEN only and the screenFlash instance will be ignored for other flash modes.
If the implementation provided by the user is no longer valid (e.g. due to any android.app.Activity or android.view.View reference used in the implementation becoming invalid), user needs to re-set a new valid ScreenFlash or clear the previous one with setScreenFlash(null), whichever appropriate.
| Parameters | |
|---|---|
@Nullable ImageCapture.ScreenFlash screenFlash |
A |
setTargetRotation
public void setTargetRotation(int rotation)
Sets the desired rotation of the output image.
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 target rotation specified here.
While rotation can also be set via setTargetRotation, using setTargetRotation(int) allows the target rotation to be set dynamically.
In general, it is best to use an android.view.OrientationEventListener to set the target rotation. This way, the rotation output will indicate which way is down for a given image. This is important since display orientation may be locked by device default, user setting, or app configuration, and some devices may not transition to a reverse-portrait display orientation. In these cases, set target rotation dynamically according to the android.view.OrientationEventListener, without re-creating the use case. snapToSurfaceRotation is a helper function to convert the orientation of the android.view.OrientationEventListener to a rotation value. See snapToSurfaceRotation for more information and sample code.
When this function is called, value set by setTargetResolution will be updated automatically to make sure the suitable resolution can be selected when the use case is bound. Value set by setCropAspectRatio will also be updated automatically to make sure the output image is cropped into expected aspect ratio.
If no target rotation is set by the application, it is set to the value of getRotation of the default display at the time the use case is bound. To return to the default value, set the value to
context.getSystemService(WindowManager.class).getDefaultDisplay().getRotation();
takePicture uses the target rotation at the time it begins executing (which may be delayed waiting on a previous takePicture call to complete).
| Parameters | |
|---|---|
int rotation |
Target rotation of the output image, expressed as one of |
takePicture
public void takePicture(
@NonNull Executor executor,
@NonNull ImageCapture.OnImageCapturedCallback callback
)
Captures a new still image for in memory access.
The listener is responsible for calling close on the returned image.
For simultaneous image capture with OUTPUT_FORMAT_RAW_JPEG, the onCaptureSuccess will be triggered twice, one for RAW_SENSOR and the other for JPEG. The order of the callbacks for which image format is triggered first is not guaranteed.
| Parameters | |
|---|---|
@NonNull Executor executor |
The executor in which the callback methods will be run. |
@NonNull ImageCapture.OnImageCapturedCallback callback |
Callback to be invoked for the newly captured image. |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
If |
takePicture
public void takePicture(
@NonNull ImageCapture.OutputFileOptions outputFileOptions,
@NonNull Executor executor,
@NonNull ImageCapture.OnImageSavedCallback imageSavedCallback
)
Captures a new still image and saves to a file along with application specified metadata.
If the ImageCapture is in a UseCaseGroup where ViewPort is set, or setCropAspectRatio is used, the image may be cropped before saving to disk which causes an additional latency.
| Parameters | |
|---|---|
@NonNull ImageCapture.OutputFileOptions outputFileOptions |
Options to store the newly captured image. |
@NonNull Executor executor |
The executor in which the callback methods will be run. |
@NonNull ImageCapture.OnImageSavedCallback imageSavedCallback |
Callback to be called for the newly captured image. |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
If |
| See also | |
|---|---|
ViewPort |
takePicture
public void takePicture(
@NonNull ImageCapture.OutputFileOptions rawOutputFileOptions,
@NonNull ImageCapture.OutputFileOptions jpegOutputFileOptions,
@NonNull Executor executor,
@NonNull ImageCapture.OnImageSavedCallback imageSavedCallback
)
Captures two still images simultaneously and saves to a file along with application specified metadata.
Currently only OUTPUT_FORMAT_RAW_JPEG is supporting simultaneous image capture. It needs two OutputFileOptions, the first one is used for RAW_SENSOR image and the second one is for JPEG. The order of the callbacks for which image format is triggered first is not guaranteed. Check with getImageFormat in onImageSaved for the image format.
| Parameters | |
|---|---|
@NonNull ImageCapture.OutputFileOptions rawOutputFileOptions |
Options to store the newly captured raw image. |
@NonNull ImageCapture.OutputFileOptions jpegOutputFileOptions |
Options to store the newly captured jpeg image. |
@NonNull Executor executor |
The executor in which the callback methods will be run. |
@NonNull ImageCapture.OnImageSavedCallback imageSavedCallback |
Callback to be called for the newly captured image. |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
If |
Extension functions
ImageCaptureExtKt.takePicture
public final @NonNull ImageProxy ImageCaptureExtKt.takePicture(
@NonNull ImageCapture receiver,
Function0<Unit> onCaptureStarted,
Function1<@NonNull Integer, Unit> onCaptureProcessProgressed,
Function1<@NonNull Bitmap, Unit> onPostviewBitmapAvailable
)
Captures a new still image for in memory access.
The caller is responsible for calling ImageProxy.close on the returned image.
| Parameters | |
|---|---|
Function0<Unit> onCaptureStarted |
Callback for when the camera has started exposing the frame. |
Function1<@NonNull Integer, Unit> onCaptureProcessProgressed |
Callback to report the progress of the capture's processing. |
Function1<@NonNull Bitmap, Unit> onPostviewBitmapAvailable |
Callback to notify that the postview bitmap is available. |
ImageCaptureExtKt.takePicture
public final @NonNull ImageCapture.OutputFileResults ImageCaptureExtKt.takePicture(
@NonNull ImageCapture receiver,
@NonNull ImageCapture.OutputFileOptions outputFileOptions,
Function0<Unit> onCaptureStarted,
Function1<@NonNull Integer, Unit> onCaptureProcessProgressed,
Function1<@NonNull Bitmap, Unit> onPostviewBitmapAvailable
)
Captures a new still image and saves to a file along with application specified metadata.
| Parameters | |
|---|---|
@NonNull ImageCapture.OutputFileOptions outputFileOptions |
Options to store the output image file. |
Function0<Unit> onCaptureStarted |
Callback for when the camera has started exposing the frame. |
Function1<@NonNull Integer, Unit> onCaptureProcessProgressed |
Callback to report the progress of the capture's processing. |
Function1<@NonNull Bitmap, Unit> onPostviewBitmapAvailable |
Callback to notify that the postview bitmap is available. |