ImageProcessor


interface ImageProcessor


Post-processing effect for images.

This interface is for post-processing images. The input is an image from the camera, with the instructions on how to process it; the output is a processed image. CameraX forwards images to the implementation, and delivers the processed images to back the app.

Currently, it can only be used with the ImageCapture by targeting IMAGE_CAPTURE.

If the implementation fails to process the input, it should throw ProcessingException. The error will be caught by CameraX and propagated to the app via error callbacks such as onError or onError.

Code sample:

 class ImageProcessorImpl implements ImageProcessor {
     Response process(Request request) throws ProcessingException {
         try {
             ImageProxy image = request.getInputImages().get(0);
             ByteBuffer byteBuffer = image.getPlanes()[0];
             // Process the content of byteBuffer and create a Response object.
         } catch(Exception e) {
             throws new ProcessingException(e);
         }
     }
 }
See also
CameraEffect

Summary

Nested types

A request for processing one or multiple ImageProxy.

A response for returning a processed ImageProxy to CameraX.

Public functions

ImageProcessor.Response

Accepts original image from CameraX and returns processed image.

Public functions

process

Added in 1.3.0
fun process(request: ImageProcessor.Request): ImageProcessor.Response

Accepts original image from CameraX and returns processed image.

CameraX invokes this method for each new incoming image. It's invoked on the Executor provided in CameraEffect's constructor. It might be called in parallel, should the Executor allow multi-threading. The implementation must block the current calling thread until the output image is returned.

The implementation must follow the instruction in the Request to process the input image. For example, it must produce an output image with the format following the JavaDoc of getInputImage. Failing to do so might cause the processing to fail. For example, for ImageCapture, it will cause the takePicture call to fail.

The implementation must throw a ProcessingException if it fails to process the Request. CameraX will catch the error and deliver it to the app via error callbacks. For ImageCapture, the error callbacks are onError or onError.

Parameters
request: ImageProcessor.Request

a Request that contains the original image.

Returns
ImageProcessor.Response

a Response that contains the processed image.

Throws
androidx.camera.core.ProcessingException

if the implementation fails to process the Request.