# ImageProxy

Artifact: [androidx.camera:camera-core](https://developer.android.com/jetpack/androidx/releases/camera) [View Source](https://cs.android.com/search?q=file:androidx/camera/core/ImageProxy.java+class:androidx.camera.core.ImageProxy) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/camera#1.0.0)

*** ** * ** ***


```
interface ImageProxy : ImageWrapper, AutoCloseable
```

<br />

*** ** * ** ***

An image proxy which has a similar interface as `https://developer.android.com/reference/android/media/Image.html`.

## Summary

| ### Nested types |
|---|
| `interface https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy.PlaneProxy : https://developer.android.com/reference/kotlin/androidx/camera/common/ImagePlane` A plane proxy which has an analogous interface as `https://developer.android.com/reference/android/media/Image.Plane.html`. |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#close()()` Closes the underlying `https://developer.android.com/reference/android/media/Image.html`. |
| `https://developer.android.com/reference/android/graphics/Rect.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#getCropRect()()` Returns the crop rectangle. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#getDataSpace()()` The dataspace associated with this image. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#getFormat()()` Returns the image format. |
| `https://developer.android.com/reference/android/hardware/HardwareBuffer.html?` | `@https://developer.android.com/reference/kotlin/androidx/annotation/RequiresApi(value = https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#P) https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#getHardwareBuffer()()` Returns the `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` for this image. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#getHeight()()` Returns the image height. |
| `https://developer.android.com/reference/android/media/Image.html?` | `@https://developer.android.com/reference/kotlin/androidx/camera/core/ExperimentalGetImage https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#getImage()()` Returns the android `https://developer.android.com/reference/android/media/Image.html`. |
| `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageInfo` | `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#getImageInfo()()` Returns the `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageInfo`. |
| `(https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html)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/core/ImageProxy#getImagePlanes()()` The list of pixel planes for this image. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy.PlaneProxy>` | `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#getPlanes()()` Returns the array of planes. |
| `https://developer.android.com/reference/android/hardware/SyncFence.html?` | `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#getSyncFence()()` The sync fence associated with this image, or `null` if there is no fence. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#getTimestamp()()` Returns the timestamp. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#getWidth()()` Returns the image width. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#setCropRect(android.graphics.Rect)(rect: https://developer.android.com/reference/android/graphics/Rect.html?)` Sets the crop rectangle. |
| `https://developer.android.com/reference/android/graphics/Bitmap.html` | `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#toBitmap()()` Converts `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy` to `https://developer.android.com/reference/android/graphics/Bitmap.html`. |
| `T?` | `<T> https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy#unwrapAs(java.lang.Class<T>)(type: https://developer.android.com/reference/java/lang/Class.html<T!>)` Attempts to unwrap this object into the specified underlying `https://developer.android.com/reference/kotlin/androidx/camera/common/UnsafeWrapper#unwrapAs(java.lang.Class)`. |

## Public functions

### close

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

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

Closes the underlying `https://developer.android.com/reference/android/media/Image.html`.

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

### getCropRect

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

```
fun getCropRect(): Rect
```

Returns the crop rectangle.

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

### getDataSpace

```
fun getDataSpace(): Int
```

The dataspace associated with this image.

The dataspace defines how the color components of the image should be interpreted.

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

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

### getFormat

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

```
fun getFormat(): Int
```

Returns the image format.

The image format can be one of the `https://developer.android.com/reference/android/graphics/ImageFormat.html` or `https://developer.android.com/reference/android/graphics/PixelFormat.html` constants.

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

### getHardwareBuffer

```
@RequiresApi(value = Build.VERSION_CODES.P)
fun getHardwareBuffer(): HardwareBuffer?
```

Returns the `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` for this image.

This method is primarily intended for GPU-based processing, specifically when `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageAnalysis` is configured with `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageAnalysis#OUTPUT_IMAGE_FORMAT_PRIVATE()`. In other cases, its availability depends on the Android API level (must be 28 or higher) and whether the `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy` is backed by an `https://developer.android.com/reference/android/media/Image.html` that provides a `https://developer.android.com/reference/android/hardware/HardwareBuffer.html`.

If it is supported, each call to this method will return a new `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` instance. The application must close the obtained `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` instance when it is no longer needed to ensure that resources are released. To comply with the underlying `https://developer.android.com/reference/android/media/Image.html` lifecycle, the `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` should be closed before the `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy` is closed, and must not be used after the `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy` is closed.

| Returns |
|---|---|
| `https://developer.android.com/reference/android/hardware/HardwareBuffer.html?` | the hardware buffer, or `null` if it is not available. |

### getHeight

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

```
fun getHeight(): Int
```

Returns the image height.

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

### getImage

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

```
@ExperimentalGetImage
fun getImage(): Image?
```

Returns the android `https://developer.android.com/reference/android/media/Image.html`.

If the ImageProxy is a wrapper for an android `https://developer.android.com/reference/android/media/Image.html`, it will return the `https://developer.android.com/reference/android/media/Image.html`. It is possible for an ImageProxy to wrap something that isn't an `https://developer.android.com/reference/android/media/Image.html`. 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 `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageAnalysis.Analyzer` function. Destroying the `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageAnalysis` will close the underlying `https://developer.android.com/reference/android/media/ImageReader.html`. So an `https://developer.android.com/reference/android/media/Image.html` obtained with this method will behave as such.

| Returns |
|---|---|
| `https://developer.android.com/reference/android/media/Image.html?` | the android image. |

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

### getImageInfo

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

```
fun getImageInfo(): ImageInfo
```

Returns the `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageInfo`.

### getImagePlanes

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

```
fun getImagePlanes(): (Mutable)List<ImagePlane>
```

The list of pixel planes for this image.

The number of planes is determined by the format of the image. For example, YUV images usually have three planes (Y, U, V), while RGB or JPEG images typically have a single plane.

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

### getPlanes

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

```
fun getPlanes(): Array<ImageProxy.PlaneProxy>
```

Returns the array of planes.

If the image format is `https://developer.android.com/reference/android/graphics/ImageFormat.html#PRIVATE`, the returned array may be empty because the image data is not CPU accessible.

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

### getSyncFence

```
fun getSyncFence(): SyncFence?
```

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

The sync fence is used to coordinate access to the image data, ensuring that the producer has finished writing before the consumer starts reading, or vice versa.

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

### getTimestamp

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

```
fun getTimestamp(): Long
```

Returns the timestamp.

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

### getWidth

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

```
fun getWidth(): Int
```

Returns the image width.

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

### setCropRect

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

```
fun setCropRect(rect: Rect?): Unit
```

Sets the crop rectangle.

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

### toBitmap

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

```
fun toBitmap(): Bitmap
```

Converts `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy` to `https://developer.android.com/reference/android/graphics/Bitmap.html`.

The supported `https://developer.android.com/reference/kotlin/androidx/camera/core/ImageProxy` format is `https://developer.android.com/reference/android/graphics/ImageFormat.html#YUV_420_888`, `https://developer.android.com/reference/android/graphics/ImageFormat.html#JPEG` or `https://developer.android.com/reference/android/graphics/PixelFormat.html#RGBA_8888`. If format is invalid, an `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` will be thrown. If the conversion to bitmap failed, an `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html` will be thrown.

Note that this method does not support `https://developer.android.com/reference/android/graphics/ImageFormat.html#PRIVATE` format.

| Returns |
|---|---|
| `https://developer.android.com/reference/android/graphics/Bitmap.html` | `https://developer.android.com/reference/android/graphics/Bitmap.html` instance. |

### unwrapAs

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

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

Attempts to unwrap this object into the specified underlying `https://developer.android.com/reference/kotlin/androidx/camera/common/UnsafeWrapper#unwrapAs(java.lang.Class)`.

Callers should use this method to retrieve the wrapped platform object.

### Implementation Notes

- If the requested `https://developer.android.com/reference/kotlin/androidx/camera/common/UnsafeWrapper#unwrapAs(java.lang.Class)` is not supported by this wrapper, this method must return `null`.

- Implementations designed for testing (e.g., fakes, mocks, or no-op wrappers) should return `null` to signal that no real underlying object is available.

| Parameters |
|---|---|
| `<T>` | The expected type of the underlying object. |
| `type: https://developer.android.com/reference/java/lang/Class.html<T!>` | The `https://developer.android.com/reference/java/lang/Class.html` representing the expected type of the underlying object. |

| Returns |
|---|---|
| `T?` | The underlying object cast to `https://developer.android.com/reference/kotlin/androidx/camera/common/UnsafeWrapper#unwrapAs(java.lang.Class)`, or `null` if the object cannot be unwrapped into the requested `https://developer.android.com/reference/kotlin/androidx/camera/common/UnsafeWrapper#unwrapAs(java.lang.Class)`. |