public class FakeImage implements MutableImageWrapper


Fake implementation of MutableImageWrapper for testing.

This class supports optional injection of both a ByteBuffer and a HardwareBuffer.

HardwareBuffer & Backing Behavior

If a hardwareBuffer is provided, its width, height, and format metadata will be verified and accessible. However, accessing pixel data directly from within the HardwareBuffer is not supported on pure JVM. Accessing the buffer fields from imagePlanes will use data backed by the byteBuffer parameter and/or byteBuffer field on this image.

If provided, the HardwareBuffer will be closed when this FakeImage is closed.

Summary

Public constructors

FakeImage(
    int width,
    int height,
    int format,
    long timestamp,
    ByteBuffer byteBuffer,
    HardwareBuffer hardwareBuffer,
    @NonNull Rect cropRect,
    @NonNull List<@NonNull ImagePlane> imagePlanes
)

Public methods

void

Closes the image.

final int

The number of times close has been called on this image.

@NonNull Rect

The crop rectangle of the image.

int

The dataspace associated with this image.

int

The format of the image.

HardwareBuffer

The hardware buffer for this image.

int

The height of the image in pixels.

@NonNull List<@NonNull ImagePlane>

The image planes for this image.

SyncFence

The sync fence associated with this image, or null if there is no fence.

long

The timestamp associated with this image, in nanoseconds.

int

The width of the image in pixels.

boolean

Returns true if this image has been closed, false otherwise.

void

The crop rectangle of the image.

void
setDataSpace(int value)

The dataspace associated with this image.

void
setFormat(int value)

The format of the image.

void
setHeight(int value)

The height of the image in pixels.

void

The image planes for this image.

void

The sync fence associated with this image, or null if there is no fence.

void
setTimestamp(long value)

The timestamp associated with this image, in nanoseconds.

void
setWidth(int value)

The width of the image in pixels.

@NonNull String
T
<T extends Object> unwrapAs(@NonNull Class<@NonNull T> type)

Unwraps this fake image to retrieve the underlying type.

Public constructors

FakeImage

public FakeImage(
    int width,
    int height,
    int format,
    long timestamp,
    ByteBuffer byteBuffer,
    HardwareBuffer hardwareBuffer,
    @NonNull Rect cropRect,
    @NonNull List<@NonNull ImagePlane> imagePlanes
)

Public methods

close

Added in 1.7.0-alpha03
public void close()

Closes the image.

Increments closeCount. If this is the first call to close, it will also close the underlying HardwareBuffer if it was provided or created.

getCloseCount

Added in 1.7.0-alpha03
public final int getCloseCount()

The number of times close has been called on this image.

Useful for verifying lifecycle management in tests.

getCropRect

Added in 1.7.0-alpha03
public @NonNull Rect getCropRect()

The crop rectangle of the image.

See also
setCropRect

getDataSpace

public int getDataSpace()

The dataspace associated with this image.

Defaults to android.hardware.DataSpace.DATASPACE_UNKNOWN.

getFormat

Added in 1.7.0-alpha03
public int getFormat()

The format of the image.

The value returned is one of the constants defined in ImageFormat.

getHardwareBuffer

public HardwareBuffer getHardwareBuffer()

The hardware buffer for this image.

If a HardwareBuffer was provided in the constructor, that buffer is returned. Otherwise, a mock HardwareBuffer is generated dynamically based on the image format, width, and height.

Throws IllegalStateException if this image is already closed.

getHeight

Added in 1.7.0-alpha03
public int getHeight()

The height of the image in pixels.

See also
getHeight

getImagePlanes

Added in 1.7.0-alpha03
public @NonNull List<@NonNull ImagePlanegetImagePlanes()

The image planes for this image.

If no image planes were provided in the constructor, they are generated automatically based on the image format and the backing ByteBuffer.

Supported formats for automatic plane generation:

Throws IllegalStateException if this image is already closed. Throws UnsupportedOperationException if automatic generation is attempted on an unsupported format.

getSyncFence

public SyncFence getSyncFence()

The sync fence associated with this image, or null if there is no fence.

See also
setFence

getTimestamp

Added in 1.7.0-alpha03
public long getTimestamp()

The timestamp associated with this image, in nanoseconds.

See also
setTimestamp

getWidth

Added in 1.7.0-alpha03
public int getWidth()

The width of the image in pixels.

See also
getWidth

isClosed

Added in 1.7.0-alpha03
public boolean isClosed()

Returns true if this image has been closed, false otherwise.

setCropRect

Added in 1.7.0-alpha03
public void setCropRect(@NonNull Rect value)

The crop rectangle of the image.

See also
setCropRect

setDataSpace

Added in 1.7.0-alpha03
public void setDataSpace(int value)

The dataspace associated with this image.

Defaults to android.hardware.DataSpace.DATASPACE_UNKNOWN.

setFormat

Added in 1.7.0-alpha03
public void setFormat(int value)

The format of the image.

The value returned is one of the constants defined in ImageFormat.

setHeight

Added in 1.7.0-alpha03
public void setHeight(int value)

The height of the image in pixels.

See also
getHeight

setImagePlanes

public void setImagePlanes(@NonNull List<@NonNull ImagePlane> value)

The image planes for this image.

If no image planes were provided in the constructor, they are generated automatically based on the image format and the backing ByteBuffer.

Supported formats for automatic plane generation:

Throws IllegalStateException if this image is already closed. Throws UnsupportedOperationException if automatic generation is attempted on an unsupported format.

setSyncFence

Added in 1.7.0-alpha03
public void setSyncFence(SyncFence value)

The sync fence associated with this image, or null if there is no fence.

See also
setFence

setTimestamp

Added in 1.7.0-alpha03
public void setTimestamp(long value)

The timestamp associated with this image, in nanoseconds.

See also
setTimestamp

setWidth

Added in 1.7.0-alpha03
public void setWidth(int value)

The width of the image in pixels.

See also
getWidth

toString

public @NonNull String toString()

unwrapAs

Added in 1.7.0-alpha03
public T <T extends Object> unwrapAs(@NonNull Class<@NonNull T> type)

Unwraps this fake image to retrieve the underlying type.

Supports unwrapping to:

  • The FakeImage class itself (or subclasses).

  • ByteBuffer, which returns the backing buffer.

  • HardwareBuffer, which returns the associated hardware buffer (on API level 26+).

Parameters
@NonNull Class<@NonNull T> type

The class of the object to return.

Returns
T

The unwrapped object, or null if the type is not supported.