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

*** ** * ** ***


```
public interface ImageProxy extends ImageWrapper, AutoCloseable
```

<br />

*** ** * ** ***

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

## Summary

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

| ### Public methods |
|---|---|
| `abstract void` | `https://developer.android.com/reference/androidx/camera/core/ImageProxy#close()()` Closes the underlying `https://developer.android.com/reference/android/media/Image.html`. |
| `abstract @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Rect.html` | `https://developer.android.com/reference/androidx/camera/core/ImageProxy#getCropRect()()` Returns the crop rectangle. |
| `default int` | `https://developer.android.com/reference/androidx/camera/core/ImageProxy#getDataSpace()()` The dataspace associated with this image. |
| `abstract int` | `https://developer.android.com/reference/androidx/camera/core/ImageProxy#getFormat()()` Returns the image format. |
| `default @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/hardware/HardwareBuffer.html` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = https://developer.android.com/reference/android/os/Build.VERSION_CODES.html#P) https://developer.android.com/reference/androidx/camera/core/ImageProxy#getHardwareBuffer()()` Returns the `https://developer.android.com/reference/android/hardware/HardwareBuffer.html` for this image. |
| `abstract int` | `https://developer.android.com/reference/androidx/camera/core/ImageProxy#getHeight()()` Returns the image height. |
| `abstract @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/media/Image.html` | `@https://developer.android.com/reference/androidx/camera/core/ExperimentalGetImage https://developer.android.com/reference/androidx/camera/core/ImageProxy#getImage()()` Returns the android `https://developer.android.com/reference/android/media/Image.html`. |
| `abstract @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/camera/core/ImageInfo` | `https://developer.android.com/reference/androidx/camera/core/ImageProxy#getImageInfo()()` Returns the `https://developer.android.com/reference/androidx/camera/core/ImageInfo`. |
| `default @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/List.html<@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/camera/common/ImagePlane>` | `https://developer.android.com/reference/androidx/camera/core/ImageProxy#getImagePlanes()()` The list of pixel planes for this image. |
| `abstract @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html ImageProxy.PlaneProxy[]` | `https://developer.android.com/reference/androidx/camera/core/ImageProxy#getPlanes()()` Returns the array of planes. |
| `default @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/hardware/SyncFence.html` | `https://developer.android.com/reference/androidx/camera/core/ImageProxy#getSyncFence()()` The sync fence associated with this image, or `null` if there is no fence. |
| `default long` | `https://developer.android.com/reference/androidx/camera/core/ImageProxy#getTimestamp()()` Returns the timestamp. |
| `abstract int` | `https://developer.android.com/reference/androidx/camera/core/ImageProxy#getWidth()()` Returns the image width. |
| `abstract void` | `https://developer.android.com/reference/androidx/camera/core/ImageProxy#setCropRect(android.graphics.Rect)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/graphics/Rect.html rect)` Sets the crop rectangle. |
| `default @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Bitmap.html` | `https://developer.android.com/reference/androidx/camera/core/ImageProxy#toBitmap()()` Converts `https://developer.android.com/reference/androidx/camera/core/ImageProxy` to `https://developer.android.com/reference/android/graphics/Bitmap.html`. |
| `default @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html T` | `<T> https://developer.android.com/reference/androidx/camera/core/ImageProxy#unwrapAs(java.lang.Class<T>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Class.html<T> type)` Attempts to unwrap this object into the specified underlying `https://developer.android.com/reference/androidx/camera/common/UnsafeWrapper#unwrapAs(java.lang.Class)`. |

## Public methods

### close

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

```
abstract void close()
```

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)

```
abstract @NonNull Rect getCropRect()
```

Returns the crop rectangle.

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

### getDataSpace

```
default int getDataSpace()
```

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/androidx/camera/common/ImageDataSpace`.

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

### getFormat

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

```
abstract int getFormat()
```

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)
default @Nullable HardwareBuffer getHardwareBuffer()
```

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/androidx/camera/core/ImageAnalysis` is configured with `https://developer.android.com/reference/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/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/androidx/camera/core/ImageProxy` is closed, and must not be used after the `https://developer.android.com/reference/androidx/camera/core/ImageProxy` is closed.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html 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)

```
abstract int getHeight()
```

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
abstract @Nullable Image getImage()
```

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/androidx/camera/core/ImageAnalysis.Analyzer` function. Destroying the `https://developer.android.com/reference/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://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html 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)

```
abstract @NonNull ImageInfo getImageInfo()
```

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

### getImagePlanes

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

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

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)

```
abstract @NonNull ImageProxy.PlaneProxy[] getPlanes()
```

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

```
default @Nullable SyncFence getSyncFence()
```

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)

```
default long getTimestamp()
```

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)

```
abstract int getWidth()
```

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)

```
abstract void setCropRect(@Nullable Rect rect)
```

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)

```
default @NonNull Bitmap toBitmap()
```

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

The supported `https://developer.android.com/reference/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://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html 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)

```
default @Nullable T <T> unwrapAs(@NonNull Class<T> type)
```

Attempts to unwrap this object into the specified underlying `https://developer.android.com/reference/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/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. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Class.html<T> type` | The `https://developer.android.com/reference/java/lang/Class.html` representing the expected type of the underlying object. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html T` | The underlying object cast to `https://developer.android.com/reference/androidx/camera/common/UnsafeWrapper#unwrapAs(java.lang.Class)`, or `null` if the object cannot be unwrapped into the requested `https://developer.android.com/reference/androidx/camera/common/UnsafeWrapper#unwrapAs(java.lang.Class)`. |