# 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)

*** ** * ** ***


```
open class FakeImage : MutableImageWrapper
```

<br />

*** ** * ** ***

Fake implementation of `https://developer.android.com/reference/kotlin/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/kotlin/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/kotlin/androidx/camera/common/testing/FakeImage` is closed.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/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)( width: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, height: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, format: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, timestamp: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, byteBuffer: https://developer.android.com/reference/java/nio/ByteBuffer.html?, hardwareBuffer: https://developer.android.com/reference/android/hardware/HardwareBuffer.html?, cropRect: https://developer.android.com/reference/android/graphics/Rect.html, imagePlanes: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/camera/common/ImagePlane> )` |

| ### Public functions |
|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#close()()` Closes the image. |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#toString()()` |
| `open T?` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#unwrapAs(java.lang.Class)(type: https://developer.android.com/reference/java/lang/Class.html<T>)` Unwraps this fake image to retrieve the underlying type. |

| ### Public properties |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#closeCount()` The number of times `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#close()` has been called on this image. |
| `open https://developer.android.com/reference/android/graphics/Rect.html` | `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#cropRect()` The crop rectangle of the image. |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#dataSpace()` The dataspace associated with this image. |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#format()` The format of the image. |
| `open https://developer.android.com/reference/android/hardware/HardwareBuffer.html?` | `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#hardwareBuffer()` The hardware buffer for this image. |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#height()` The height of the image in pixels. |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/camera/common/ImagePlane>` | `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#imagePlanes()` The image planes for this image. |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#isClosed()` Returns `true` if this image has been closed, `false` otherwise. |
| `open https://developer.android.com/reference/android/hardware/SyncFence.html?` | `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#syncFence()` The sync fence associated with this image, or `null` if there is no fence. |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#timestamp()` The timestamp associated with this image, in nanoseconds. |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#width()` The width of the image in pixels. |

## Public constructors

### FakeImage

```
FakeImage(
    width: Int,
    height: Int,
    format: Int,
    timestamp: Long,
    byteBuffer: ByteBuffer? = null,
    hardwareBuffer: HardwareBuffer? = null,
    cropRect: Rect = Rect(0, 0, width, height),
    imagePlanes: List<ImagePlane> = emptyList()
)
```

## Public functions

### close

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

```
open fun close(): Unit
```

Closes the image.

Increments `https://developer.android.com/reference/kotlin/androidx/camera/common/testing/FakeImage#closeCount()`. If this is the first call to `https://developer.android.com/reference/kotlin/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.

### toString

```
open fun toString(): String
```

### unwrapAs

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

```
open fun <T : Any> unwrapAs(type: Class<T>): T?
```

Unwraps this fake image to retrieve the underlying type.

Supports unwrapping to:

- The `https://developer.android.com/reference/kotlin/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 |
|---|---|
| `type: https://developer.android.com/reference/java/lang/Class.html<T>` | The class of the object to return. |

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

## Public properties

### closeCount

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

```
val closeCount: Int
```

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

Useful for verifying lifecycle management in tests.

### cropRect

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

```
open var cropRect: Rect
```

The crop rectangle of the image.

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

### dataSpace

```
open var dataSpace: Int
```

The dataspace associated with this image.

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

### format

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

```
open var format: Int
```

The format of the image.

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

Value is one of the following:

- `0`
- `4`
- `842094169`
- `538982489`
- `16`
- `17`
- `20`
- `256`
- `1768253795`
- `35`
- `39`
- `40`
- `41`
- `42`
- `32`
- `36`
- `37`
- `38`
- `1144402265`
- `257`
- `34`
- `1212500294`
- `54`
- `4101`
- `4098`
- `4099`
- `540422489`
- `60`
- `4102`
- `56`
- `55`
- `57`
- `58`
- `59`
- `-3`
- `-2`
- `-1`
- `1`
- `2`
- `3`
- `22`
- `43`
- `33`
- `48`
- `49`
- `50`
- `51`
- `52`
- `53`

<br />

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

### hardwareBuffer

```
open val hardwareBuffer: HardwareBuffer?
```

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.

### height

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

```
open var height: Int
```

The height of the image in pixels.

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

### imagePlanes

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

```
open var imagePlanes: List<ImagePlane>
```

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.

### isClosed

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

```
open val isClosed: Boolean
```

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

### syncFence

```
open var syncFence: SyncFence?
```

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)` |   |

### timestamp

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

```
open var timestamp: Long
```

The timestamp associated with this image, in nanoseconds.

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

### width

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

```
open var width: Int
```

The width of the image in pixels.

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