# FakeImage

Artifact: [androidx.camera:camera-common-testing](https://developer.android.com/jetpack/androidx/releases/camera) [View Source](https://cs.android.com/search?q=file:androidx/camera/common/testing/FakeImage.kt+class:androidx.camera.common.testing.FakeImage) Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

*** ** * ** ***


```
public class FakeImage implements MutableImageWrapper
```

<br />

*** ** * ** ***

Fake implementation of `https://developer.android.com/reference/androidx/camera/common/MutableImageWrapper` for testing.

This class supports optional injection of both a `https://developer.android.com/reference/java/nio/ByteBuffer.html` and a `https://developer.android.com/reference/android/hardware/HardwareBuffer.html`.

## 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 `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#(kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Long,java.nio.ByteBuffer,android.hardware.HardwareBuffer,android.graphics.Rect,kotlin.collections.List)` will use data backed by the `byteBuffer` parameter and/or `byteBuffer` field on this image.

If provided, the `HardwareBuffer` will be closed when this `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage` is closed.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#FakeImage(kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Long,java.nio.ByteBuffer,android.hardware.HardwareBuffer,android.graphics.Rect,kotlin.collections.List)( int width, int height, int format, long timestamp, https://developer.android.com/reference/java/nio/ByteBuffer.html byteBuffer, https://developer.android.com/reference/android/hardware/HardwareBuffer.html hardwareBuffer, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Rect.html cropRect, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/common/ImagePlane> imagePlanes )` |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#close()()` Closes the image. |
| `final int` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#getCloseCount()()` The number of times `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#close()` has been called on this image. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Rect.html` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#getCropRect()()` The crop rectangle of the image. |
| `int` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#getDataSpace()()` The dataspace associated with this image. |
| `int` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#getFormat()()` The format of the image. |
| `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#getHardwareBuffer()()` The hardware buffer for this image. |
| `int` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#getHeight()()` The height of the image in pixels. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/common/ImagePlane>` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#getImagePlanes()()` The image planes for this image. |
| `https://developer.android.com/reference/android/hardware/SyncFence.html` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#getSyncFence()()` The sync fence associated with this image, or `null` if there is no fence. |
| `long` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#getTimestamp()()` The timestamp associated with this image, in nanoseconds. |
| `int` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#getWidth()()` The width of the image in pixels. |
| `boolean` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#isClosed()()` Returns `true` if this image has been closed, `false` otherwise. |
| `void` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#setCropRect(android.graphics.Rect)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/graphics/Rect.html value)` The crop rectangle of the image. |
| `void` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#setDataSpace(kotlin.Int)(int value)` The dataspace associated with this image. |
| `void` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#setFormat(kotlin.Int)(int value)` The format of the image. |
| `void` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#setHeight(kotlin.Int)(int value)` The height of the image in pixels. |
| `void` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#setImagePlanes(kotlin.collections.List)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/common/ImagePlane> value)` The image planes for this image. |
| `void` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#setSyncFence(android.hardware.SyncFence)(https://developer.android.com/reference/android/hardware/SyncFence.html value)` The sync fence associated with this image, or `null` if there is no fence. |
| `void` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#setTimestamp(kotlin.Long)(long value)` The timestamp associated with this image, in nanoseconds. |
| `void` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#setWidth(kotlin.Int)(int value)` The width of the image in pixels. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#toString()()` |
| `T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#unwrapAs(java.lang.Class)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/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-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public void close()
```

Closes the image.

Increments `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#closeCount()`. If this is the first call to `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#close()`, it will also close the underlying `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` if it was provided or created.

### getCloseCount

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public final int getCloseCount()
```

The number of times `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage#close()` has been called on this image.

Useful for verifying lifecycle management in tests.

### getCropRect

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public @NonNull Rect getCropRect()
```

The crop rectangle of the image.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html#setCropRect(android.graphics.Rect)` |   |

### getDataSpace

```
public int getDataSpace()
```

The dataspace associated with this image.

Defaults to `https://developer.android.com/reference/android/hardware/DataSpace.html#DATASPACE_UNKNOWN`.

Value is one of the following:

- `4096`
- `4098`
- `4100`
- `4101`
- `0`
- `406913024`
- `142671872`
- `411107328`
- `143261696`
- `168165376`
- `163971072`
- `151715840`
- `146931712`
- `281149440`
- `281280512`
- `147193856`
- `281083904`
- `155844608`
- `138477568`

<br />

### getFormat

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public int getFormat()
```

The format of the image.

The value returned is one of the constants defined in `https://developer.android.com/reference/androidx/camera/common/ImageFormat`.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html#getFormat()` |   |
| `https://developer.android.com/reference/androidx/camera/common/ImageFormat` |   |

### getHardwareBuffer

```
public HardwareBuffer getHardwareBuffer()
```

The hardware buffer for this image.

If a `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` was provided in the constructor, that buffer is returned. Otherwise, a mock `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` is generated dynamically based on the image format, width, and height.

Throws `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` if this image is already closed.

### getHeight

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public int getHeight()
```

The height of the image in pixels.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html#getHeight()` |   |

### getImagePlanes

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public @NonNull List<@NonNull ImagePlane> getImagePlanes()
```

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 `https://developer.android.com/reference/java/nio/ByteBuffer.html`.

Supported formats for automatic plane generation:

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#YUV_420_888`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#NV21`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#JPEG`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#HEIC`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#DEPTH_JPEG`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#JPEG_R`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#DEPTH_POINT_CLOUD`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#PRIVATE`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#UNKNOWN`

Throws `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` if this image is already closed. Throws `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unsupported-operation-exception/index.html` 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 |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html#setFence(android.hardware.SyncFence)` |   |

### getTimestamp

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public long getTimestamp()
```

The timestamp associated with this image, in nanoseconds.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html#setTimestamp(long)` |   |

### getWidth

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public int getWidth()
```

The width of the image in pixels.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html#getWidth()` |   |

### isClosed

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public boolean isClosed()
```

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

### setCropRect

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public void setCropRect(@NonNull Rect value)
```

The crop rectangle of the image.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html#setCropRect(android.graphics.Rect)` |   |

### setDataSpace

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public void setDataSpace(int value)
```

The dataspace associated with this image.

Defaults to `https://developer.android.com/reference/android/hardware/DataSpace.html#DATASPACE_UNKNOWN`.

### setFormat

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public void setFormat(int value)
```

The format of the image.

The value returned is one of the constants defined in `https://developer.android.com/reference/androidx/camera/common/ImageFormat`.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html#getFormat()` |   |
| `https://developer.android.com/reference/androidx/camera/common/ImageFormat` |   |

### setHeight

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public void setHeight(int value)
```

The height of the image in pixels.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html#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 `https://developer.android.com/reference/java/nio/ByteBuffer.html`.

Supported formats for automatic plane generation:

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#YUV_420_888`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#NV21`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#JPEG`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#HEIC`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#DEPTH_JPEG`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#JPEG_R`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#DEPTH_POINT_CLOUD`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#PRIVATE`

- `https://developer.android.com/reference/android/graphics/ImageFormat.html#UNKNOWN`

Throws `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` if this image is already closed. Throws `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unsupported-operation-exception/index.html` if automatic generation is attempted on an unsupported format.

### setSyncFence

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public void setSyncFence(SyncFence value)
```

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

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html#setFence(android.hardware.SyncFence)` |   |

### setTimestamp

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public void setTimestamp(long value)
```

The timestamp associated with this image, in nanoseconds.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html#setTimestamp(long)` |   |

### setWidth

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public void setWidth(int value)
```

The width of the image in pixels.

| See also |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html#getWidth()` |   |

### toString

```
public @NonNull String toString()
```

### unwrapAs

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

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

Unwraps this fake image to retrieve the underlying type.

Supports unwrapping to:

- The `https://developer.android.com/reference/androidx/camera/common/testing/FakeImage` class itself (or subclasses).

- `https://developer.android.com/reference/java/nio/ByteBuffer.html`, which returns the backing buffer.

- `https://developer.android.com/reference/android/hardware/HardwareBuffer.html`, which returns the associated hardware buffer (on API level 26+).

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> type` | The class of the object to return. |

| Returns |
|---|---|
| `T` | The unwrapped object, or `null` if the type is not supported. |