ImageProxy


public interface ImageProxy extends AutoCloseable


An image proxy which has a similar interface as android.media.Image.

Summary

Nested types

public interface ImageProxy.PlaneProxy

A plane proxy which has an analogous interface as android.media.Image.Plane.

Public methods

abstract void

Closes the underlying android.media.Image.

abstract @NonNull Rect

Returns the crop rectangle.

abstract int

Returns the image format.

default @Nullable HardwareBuffer

Returns the HardwareBuffer for this image.

abstract int

Returns the image height.

abstract @Nullable Image

Returns the android Image.

abstract @NonNull ImageInfo

Returns the ImageInfo.

abstract @NonNull ImageProxy.PlaneProxy[]

Returns the array of planes.

abstract int

Returns the image width.

abstract void

Sets the crop rectangle.

default @NonNull Bitmap

Converts ImageProxy to Bitmap.

Public methods

close

Added in 1.0.0
abstract void close()

Closes the underlying android.media.Image.

See also
close

getCropRect

Added in 1.0.0
abstract @NonNull Rect getCropRect()

Returns the crop rectangle.

See also
getCropRect

getFormat

Added in 1.0.0
abstract int getFormat()

Returns the image format.

The image format can be one of the ImageFormat or PixelFormat constants.

See also
getFormat

getHardwareBuffer

Added in 1.7.0-alpha02
@RequiresApi(value = Build.VERSION_CODES.P)
default @Nullable HardwareBuffer getHardwareBuffer()

Returns the HardwareBuffer for this image.

This method is primarily intended for GPU-based processing, specifically when ImageAnalysis is configured with OUTPUT_IMAGE_FORMAT_PRIVATE. In other cases, its availability depends on the Android API level (must be 28 or higher) and whether the ImageProxy is backed by an android.media.Image that provides a HardwareBuffer.

If it is supported, each call to this method will return a new HardwareBuffer instance. The application must close the obtained HardwareBuffer instance when it is no longer needed to ensure that resources are released. To comply with the underlying android.media.Image lifecycle, the HardwareBuffer should be closed before the ImageProxy is closed, and must not be used after the ImageProxy is closed.

Returns
@Nullable HardwareBuffer

the hardware buffer, or null if it is not available.

getHeight

Added in 1.0.0
abstract int getHeight()

Returns the image height.

See also
getHeight

getImage

Added in 1.0.0
@ExperimentalGetImage
abstract @Nullable Image getImage()

Returns the android Image.

If the ImageProxy is a wrapper for an android Image, it will return the Image. It is possible for an ImageProxy to wrap something that isn't an Image. If that's the case then it will return null.

The returned image should not be closed by the application. Instead it should be closed by the ImageProxy, which happens, for example, on return from the ImageAnalysis.Analyzer function. Destroying the ImageAnalysis will close the underlying android.media.ImageReader. So an Image obtained with this method will behave as such.

Returns
@Nullable Image

the android image.

See also
close

getImageInfo

Added in 1.0.0
abstract @NonNull ImageInfo getImageInfo()

Returns the ImageInfo.

getPlanes

Added in 1.0.0
abstract @NonNull ImageProxy.PlaneProxy[] getPlanes()

Returns the array of planes.

If the image format is PRIVATE, the returned array may be empty because the image data is not CPU accessible.

See also
getPlanes

getWidth

Added in 1.0.0
abstract int getWidth()

Returns the image width.

See also
getWidth

setCropRect

Added in 1.0.0
abstract void setCropRect(@Nullable Rect rect)

Sets the crop rectangle.

See also
setCropRect

toBitmap

Added in 1.3.0
default @NonNull Bitmap toBitmap()

Converts ImageProxy to Bitmap.

The supported ImageProxy format is YUV_420_888, JPEG or RGBA_8888. If format is invalid, an IllegalArgumentException will be thrown. If the conversion to bitmap failed, an UnsupportedOperationException will be thrown.

Note that this method does not support PRIVATE format.

Returns
@NonNull Bitmap

Bitmap instance.