Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Nested Classes](https://developer.android.com/reference/android/media/Image#nestedclasses) \| [Methods](https://developer.android.com/reference/android/media/Image#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/media/Image#inhmethods)

# Image

*** ** * ** ***

` public abstract class Image `   
` extends https://developer.android.com/reference/java/lang/Object ` ` implements https://developer.android.com/reference/java/lang/AutoCloseable `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | android.media.Image |

<br />

*** ** * ** ***

<br />

A single complete image buffer to use with a media source such as a `https://developer.android.com/reference/android/media/MediaCodec` or a `https://developer.android.com/reference/android/hardware/camera2/CameraDevice`.

This class allows for efficient direct application access to the pixel data of the Image through one or more `https://developer.android.com/reference/java/nio/ByteBuffer`. Each buffer is encapsulated in a `https://developer.android.com/reference/android/media/Image.Plane` that describes the layout of the pixel data in that plane. Due to this direct access, and unlike the `https://developer.android.com/reference/android/graphics/Bitmap` class, Images are not directly usable as UI resources.

Since Images are often directly produced or consumed by hardware components, they are a limited resource shared across the system, and should be closed as soon as they are no longer needed.

For example, when using the `https://developer.android.com/reference/android/media/ImageReader` class to read out Images from various media sources, not closing old Image objects will prevent the availability of new Images once `https://developer.android.com/reference/android/media/ImageReader#getMaxImages()` is reached. When this happens, the function acquiring new Images will typically throw an `https://developer.android.com/reference/java/lang/IllegalStateException`.

<br />

**See also:**

- `https://developer.android.com/reference/android/media/ImageReader`

## Summary

| ### Nested classes ||
|---|---|
| ` class` | `https://developer.android.com/reference/android/media/Image.Plane` <br /> A single color plane of image data. |

| ### Public methods ||
|---|---|
| ` abstract void` | ` https://developer.android.com/reference/android/media/Image#close()() ` Free up this frame for reuse. |
| ` https://developer.android.com/reference/android/graphics/Rect` | ` https://developer.android.com/reference/android/media/Image#getCropRect()() ` Get the crop rectangle associated with this frame. |
| ` int` | ` https://developer.android.com/reference/android/media/Image#getDataSpace()() ` Get the dataspace associated with this frame. |
| ` https://developer.android.com/reference/android/hardware/SyncFence` | ` https://developer.android.com/reference/android/media/Image#getFence()() ` Get the SyncFence object associated with this frame. |
| ` abstract int` | ` https://developer.android.com/reference/android/media/Image#getFormat()() ` Get the format for this image. |
| ` https://developer.android.com/reference/android/hardware/HardwareBuffer` | ` https://developer.android.com/reference/android/media/Image#getHardwareBuffer()() ` Get the `https://developer.android.com/reference/android/hardware/HardwareBuffer` handle of the input image intended for GPU and/or hardware access. |
| ` abstract int` | ` https://developer.android.com/reference/android/media/Image#getHeight()() ` The height of the image in pixels. |
| ` abstract https://developer.android.com/reference/android/media/Image.Plane` | ` https://developer.android.com/reference/android/media/Image#getPlanes()() ` Get the array of pixel planes for this Image. |
| ` abstract long` | ` https://developer.android.com/reference/android/media/Image#getTimestamp()() ` Get the timestamp associated with this frame. |
| ` abstract int` | ` https://developer.android.com/reference/android/media/Image#getWidth()() ` The width of the image in pixels. |
| ` void` | ` https://developer.android.com/reference/android/media/Image#setCropRect(android.graphics.Rect)(https://developer.android.com/reference/android/graphics/Rect cropRect) ` Set the crop rectangle associated with this frame. |
| ` void` | ` https://developer.android.com/reference/android/media/Image#setDataSpace(int)(int dataSpace) ` Set the dataspace associated with this frame. |
| ` void` | ` https://developer.android.com/reference/android/media/Image#setFence(android.hardware.SyncFence)(https://developer.android.com/reference/android/hardware/SyncFence fence) ` Set the fence file descriptor with this frame. |
| ` void` | ` https://developer.android.com/reference/android/media/Image#setTimestamp(long)(long timestamp) ` Set the timestamp associated with this frame. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.
From interface ` https://developer.android.com/reference/java/lang/AutoCloseable ` ` abstract void` ` https://developer.android.com/reference/java/lang/AutoCloseable#close()() `

Closes this resource, relinquishing any underlying resources.

## Public methods

### close

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract void close ()
```

Free up this frame for reuse.

After calling this method, calling any methods on this `Image` will result in an `https://developer.android.com/reference/java/lang/IllegalStateException`, and attempting to read from or write to `https://developer.android.com/reference/java/nio/ByteBuffer` returned by an earlier `https://developer.android.com/reference/android/media/Image.Plane#getBuffer()` call will have undefined behavior. If the image was obtained from `https://developer.android.com/reference/android/media/ImageWriter` via `https://developer.android.com/reference/android/media/ImageWriter#dequeueInputImage()`, after calling this method, any image data filled by the application will be lost and the image will be returned to `https://developer.android.com/reference/android/media/ImageWriter` for reuse. Images given to `https://developer.android.com/reference/android/media/ImageWriter#queueInputImage(android.media.Image)` are automatically closed.

<br />

<br />

### getCropRect

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Rect getCropRect ()
```

Get the crop rectangle associated with this frame.

The crop rectangle specifies the region of valid pixels in the image, using coordinates in the largest-resolution plane.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/graphics/Rect` | <br /> |

### getDataSpace

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public int getDataSpace ()
```

Get the dataspace associated with this frame.

<br />

| Returns ||
|---|---|
| `int` | Value is either `0` or a combination of the following: - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_DEPTH` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_DYNAMIC_DEPTH` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_HEIF` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_HEIF_ULTRAHDR` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_JPEG_R` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_UNKNOWN` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_SCRGB_LINEAR` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_SRGB` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_SCRGB` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_DISPLAY_P3` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_BT2020_HLG` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_BT2020_PQ` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_ADOBE_RGB` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_JFIF` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_BT601_625` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_BT601_525` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_BT2020` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_BT709` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_DCI_P3` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_SRGB_LINEAR` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_DISPLAY_BT2020` |

### getFence

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public SyncFence getFence ()
```

Get the SyncFence object associated with this frame.

This function returns an invalid SyncFence after `https://developer.android.com/reference/android/media/Image#getPlanes()` on the image dequeued from `https://developer.android.com/reference/android/media/ImageWriter` via `https://developer.android.com/reference/android/media/ImageWriter#dequeueInputImage()`.

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/hardware/SyncFence` | The SyncFence for this frame. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if there is an error when a SyncFence object returns. |

**See also:**

- `https://developer.android.com/reference/android/hardware/SyncFence`

### getFormat

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract int getFormat ()
```

Get the format for this image. This format determines the number of ByteBuffers needed to represent the image, and the general layout of the pixel data in each ByteBuffer.

The format is one of the values from `https://developer.android.com/reference/android/graphics/ImageFormat`, `https://developer.android.com/reference/android/graphics/PixelFormat`, or `https://developer.android.com/reference/android/hardware/HardwareBuffer`. The mapping between the formats and the planes is as follows (any formats not listed will have 1 plane):

| Format | Plane count | Layout details |
|---|---|---|
| `https://developer.android.com/reference/android/graphics/ImageFormat#JPEG` | 1 | Compressed data, so row and pixel strides are 0. To uncompress, use `https://developer.android.com/reference/android/graphics/BitmapFactory#decodeByteArray(byte[],%20int,%20int)`. |
| `https://developer.android.com/reference/android/graphics/ImageFormat#YUV_420_888` | 3 | A luminance plane followed by the Cb and Cr chroma planes. The chroma planes have half the width and height of the luminance plane (4:2:0 subsampling). Each pixel sample in each plane has 8 bits. Each plane has its own row stride and pixel stride. |
| `https://developer.android.com/reference/android/graphics/ImageFormat#YUV_422_888` | 3 | A luminance plane followed by the Cb and Cr chroma planes. The chroma planes have half the width and the full height of the luminance plane (4:2:2 subsampling). Each pixel sample in each plane has 8 bits. Each plane has its own row stride and pixel stride. |
| `https://developer.android.com/reference/android/graphics/ImageFormat#YUV_444_888` | 3 | A luminance plane followed by the Cb and Cr chroma planes. The chroma planes have the same width and height as that of the luminance plane (4:4:4 subsampling). Each pixel sample in each plane has 8 bits. Each plane has its own row stride and pixel stride. |
| `https://developer.android.com/reference/android/graphics/ImageFormat#FLEX_RGB_888` | 3 | A R (red) plane followed by the G (green) and B (blue) planes. All planes have the same widths and heights. Each pixel sample in each plane has 8 bits. Each plane has its own row stride and pixel stride. |
| `https://developer.android.com/reference/android/graphics/ImageFormat#FLEX_RGBA_8888` | 4 | A R (red) plane followed by the G (green), B (blue), and A (alpha) planes. All planes have the same widths and heights. Each pixel sample in each plane has 8 bits. Each plane has its own row stride and pixel stride. |
| `https://developer.android.com/reference/android/graphics/ImageFormat#RAW_SENSOR` | 1 | A single plane of raw sensor image data, with 16 bits per color sample. The details of the layout need to be queried from the source of the raw sensor data, such as `https://developer.android.com/reference/android/hardware/camera2/CameraDevice`. |
| `https://developer.android.com/reference/android/graphics/ImageFormat#RAW_PRIVATE` | 1 | A single plane of raw sensor image data of private layout. The details of the layout is implementation specific. Row stride and pixel stride are undefined for this format. Calling `https://developer.android.com/reference/android/media/Image.Plane#getRowStride()` or `https://developer.android.com/reference/android/media/Image.Plane#getPixelStride()` on RAW_PRIVATE image will cause UnSupportedOperationException being thrown. |
| `https://developer.android.com/reference/android/graphics/ImageFormat#HEIC` | 1 | Compressed data, so row and pixel strides are 0. To uncompress, use `https://developer.android.com/reference/android/graphics/BitmapFactory#decodeByteArray(byte[],%20int,%20int)`. |
| `https://developer.android.com/reference/android/graphics/ImageFormat#YCBCR_P010` | 3 | P010 is a 4:2:0 YCbCr semiplanar format comprised of a WxH Y plane followed by a Wx(H/2) Cb and Cr planes. Each sample is represented by a 16-bit little-endian value, with the lower 6 bits set to zero. Since this is guaranteed to be a semi-planar format, the Cb plane can also be treated as an interleaved Cb/Cr plane. |
| `https://developer.android.com/reference/android/graphics/ImageFormat#YCBCR_P210` | 3 | P210 is a 4:2:2 YCbCr semiplanar format comprised of a WxH Y plane followed by a WxH Cb and Cr planes. Each sample is represented by a 16-bit little-endian value, with the lower 6 bits set to zero. Since this is guaranteed to be a semi-planar format, the Cb plane can also be treated as an interleaved Cb/Cr plane. |

<br />

<br />

| Returns ||
|---|---|
| `int` | <br /> |

**See also:**

- `https://developer.android.com/reference/android/graphics/ImageFormat`
- `https://developer.android.com/reference/android/graphics/PixelFormat`
- `https://developer.android.com/reference/android/hardware/HardwareBuffer`

### getHardwareBuffer

Added in [API level 28](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public HardwareBuffer getHardwareBuffer ()
```

Get the `https://developer.android.com/reference/android/hardware/HardwareBuffer` handle of the input image intended for GPU and/or hardware access.

The returned `https://developer.android.com/reference/android/hardware/HardwareBuffer` shall not be used after `https://developer.android.com/reference/android/media/Image#close()` has been called.

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/hardware/HardwareBuffer` | the HardwareBuffer associated with this Image or null if this Image doesn't support this feature. (Unsupported use cases include Image instances obtained through `https://developer.android.com/reference/android/media/MediaCodec`, and on versions prior to Android P, `https://developer.android.com/reference/android/media/ImageWriter`). <br /> |

### getHeight

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract int getHeight ()
```

The height of the image in pixels. For formats where some color channels are subsampled, this is the height of the largest-resolution plane.

<br />

| Returns ||
|---|---|
| `int` | <br /> |

### getPlanes

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract Plane[] getPlanes ()
```

Get the array of pixel planes for this Image. The number of planes is determined by the format of the Image. The application will get an empty array if the image format is `https://developer.android.com/reference/android/graphics/ImageFormat#PRIVATE`, because the image pixel data is not directly accessible. The application can check the image format by calling `https://developer.android.com/reference/android/media/Image#getFormat()`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/Image.Plane` | <br /> |

### getTimestamp

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract long getTimestamp ()
```

Get the timestamp associated with this frame.

The timestamp is measured in nanoseconds, and is normally monotonically increasing. The timestamps for the images from different sources may have different timebases therefore may not be comparable. The specific meaning and timebase of the timestamp depend on the source providing images. See `https://developer.android.com/reference/android/hardware/Camera`, `https://developer.android.com/reference/android/hardware/camera2/CameraDevice`, `https://developer.android.com/reference/android/media/MediaPlayer` and `https://developer.android.com/reference/android/media/MediaCodec` for more details.

<br />

<br />

| Returns ||
|---|---|
| `long` | <br /> |

### getWidth

Added in [API level 19](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public abstract int getWidth ()
```

The width of the image in pixels. For formats where some color channels are subsampled, this is the width of the largest-resolution plane.

<br />

| Returns ||
|---|---|
| `int` | <br /> |

### setCropRect

Added in [API level 21](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setCropRect (Rect cropRect)
```

Set the crop rectangle associated with this frame.

The crop rectangle specifies the region of valid pixels in the image, using coordinates in the largest-resolution plane.

<br />

| Parameters ||
|---|---|
| `cropRect` | `Rect` <br /> |

### setDataSpace

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setDataSpace (int dataSpace)
```

Set the dataspace associated with this frame.

If dataspace for an image is not set, dataspace value depends on `https://developer.android.com/reference/android/view/Surface` that is provided in the `https://developer.android.com/reference/android/media/ImageWriter` constructor.

<br />

<br />

| Parameters ||
|---|---|
| `dataSpace` | `int`: The Dataspace to be set for this image. Value is either `0` or a combination of the following: - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_DEPTH` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_DYNAMIC_DEPTH` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_HEIF` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_HEIF_ULTRAHDR` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_JPEG_R` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_UNKNOWN` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_SCRGB_LINEAR` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_SRGB` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_SCRGB` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_DISPLAY_P3` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_BT2020_HLG` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_BT2020_PQ` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_ADOBE_RGB` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_JFIF` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_BT601_625` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_BT601_525` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_BT2020` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_BT709` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_DCI_P3` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_SRGB_LINEAR` - `https://developer.android.com/reference/android/hardware/DataSpace#DATASPACE_DISPLAY_BT2020` |

### setFence

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setFence (SyncFence fence)
```

Set the fence file descriptor with this frame.

<br />

| Parameters ||
|---|---|
| `fence` | `SyncFence`: The fence file descriptor to be set for this frame. This value cannot be `null`. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOException` | if there is an error when setting a SyncFence. |

**See also:**

- `https://developer.android.com/reference/android/hardware/SyncFence`

### setTimestamp

Added in [API level 23](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void setTimestamp (long timestamp)
```

Set the timestamp associated with this frame.

The timestamp is measured in nanoseconds, and is normally monotonically increasing. The timestamps for the images from different sources may have different timebases therefore may not be comparable. The specific meaning and timebase of the timestamp depend on the source providing images. See `https://developer.android.com/reference/android/hardware/Camera`, `https://developer.android.com/reference/android/hardware/camera2/CameraDevice`, `https://developer.android.com/reference/android/media/MediaPlayer` and `https://developer.android.com/reference/android/media/MediaCodec` for more details.

For images dequeued from `https://developer.android.com/reference/android/media/ImageWriter` via `https://developer.android.com/reference/android/media/ImageWriter#dequeueInputImage()`, it's up to the application to set the timestamps correctly before sending them back to the `https://developer.android.com/reference/android/media/ImageWriter`, or the timestamp will be generated automatically when `https://developer.android.com/reference/android/media/ImageWriter#queueInputImage(android.media.Image)` is called.

<br />

<br />

| Parameters ||
|---|---|
| `timestamp` | `long`: The timestamp to be set for this image. <br /> |