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/ImageReader#nestedclasses) \| [Methods](https://developer.android.com/reference/android/media/ImageReader#pubmethods) \| [Protected Methods](https://developer.android.com/reference/android/media/ImageReader#promethods) \| [Inherited Methods](https://developer.android.com/reference/android/media/ImageReader#inhmethods)

# ImageReader

*** ** * ** ***


`
public


class
ImageReader
`


`

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.ImageReader |

<br />

*** ** * ** ***

<br />

The ImageReader class allows direct application access to image data
rendered into a `https://developer.android.com/reference/android/view/Surface`

Several Android media API classes accept Surface objects as targets to
render to, including `https://developer.android.com/reference/android/media/MediaPlayer`, `https://developer.android.com/reference/android/media/MediaCodec`,
`https://developer.android.com/reference/android/hardware/camera2/CameraDevice`, `https://developer.android.com/reference/android/media/ImageWriter` and
`https://developer.android.com/reference/android/renderscript/Allocation`. The image
sizes and formats that can be used with each source vary, and should be
checked in the documentation for the specific API.

The image data is encapsulated in `https://developer.android.com/reference/android/media/Image` objects, and multiple such
objects can be accessed at the same time, up to the number specified by the
`maxImages` constructor parameter. New images sent to an ImageReader
through its `https://developer.android.com/reference/android/view/Surface` are queued until accessed through the `https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()`
or `https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()` call. Due to memory limits, an image source will
eventually stall or drop Images in trying to render to the Surface if the
ImageReader does not obtain and release Images at a rate equal to the
production rate.

<br />

## Summary

| ### Nested classes ||
|---|---|
| ` class` | `https://developer.android.com/reference/android/media/ImageReader.Builder` Builder class for `https://developer.android.com/reference/android/media/ImageReader` objects. |
| ` interface` | `https://developer.android.com/reference/android/media/ImageReader.OnImageAvailableListener` Callback interface for being notified that a new image is available. |

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/android/media/Image` | ` https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()() ` <br /> Acquire the latest `https://developer.android.com/reference/android/media/Image` from the ImageReader's queue, dropping older `https://developer.android.com/reference/android/media/Image`. |
| ` https://developer.android.com/reference/android/media/Image` | ` https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()() ` <br /> Acquire the next Image from the ImageReader's queue. |
| ` void` | ` https://developer.android.com/reference/android/media/ImageReader#close()() ` Free up all the resources associated with this ImageReader. |
| ` void` | ` https://developer.android.com/reference/android/media/ImageReader#discardFreeBuffers()() ` Discard any free buffers owned by this ImageReader. |
| ` int` | ` https://developer.android.com/reference/android/media/ImageReader#getDataSpace()() ` The default dataspace of `https://developer.android.com/reference/android/media/Image`. |
| ` int` | ` https://developer.android.com/reference/android/media/ImageReader#getHardwareBufferFormat()() ` The default `https://developer.android.com/reference/android/hardware/HardwareBuffer` format of `https://developer.android.com/reference/android/media/Image`. |
| ` int` | ` https://developer.android.com/reference/android/media/ImageReader#getHeight()() ` The default height of `https://developer.android.com/reference/android/media/Image`, in pixels. |
| ` int` | ` https://developer.android.com/reference/android/media/ImageReader#getImageFormat()() ` The default `https://developer.android.com/reference/android/graphics/ImageFormat` of `https://developer.android.com/reference/android/media/Image`. |
| ` int` | ` https://developer.android.com/reference/android/media/ImageReader#getMaxImages()() ` Maximum number of images that can be acquired from the ImageReader by any time (for example, with `https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()`). |
| ` https://developer.android.com/reference/android/view/Surface` | ` https://developer.android.com/reference/android/media/ImageReader#getSurface()() ` <br /> Get a `https://developer.android.com/reference/android/view/Surface` that can be used to produce `https://developer.android.com/reference/android/media/Image` for this `ImageReader`. |
| ` long` | ` https://developer.android.com/reference/android/media/ImageReader#getUsage()() ` The usage flag of images that can be produced by the ImageReader. |
| ` int` | ` https://developer.android.com/reference/android/media/ImageReader#getWidth()() ` The default width of `https://developer.android.com/reference/android/media/Image`, in pixels. |
| ` static https://developer.android.com/reference/android/media/ImageReader` | ` https://developer.android.com/reference/android/media/ImageReader#newInstance(int,%20int,%20int,%20int,%20long)(int width, int height, int format, int maxImages, long usage) ` <br /> Create a new reader for images of the desired size, format and consumer usage flag. |
| ` static https://developer.android.com/reference/android/media/ImageReader` | ` https://developer.android.com/reference/android/media/ImageReader#newInstance(int,%20int,%20int,%20int)(int width, int height, int format, int maxImages) ` <br /> Create a new reader for images of the desired size and format. |
| ` void` | ` https://developer.android.com/reference/android/media/ImageReader#setOnImageAvailableListener(android.media.ImageReader.OnImageAvailableListener,%20android.os.Handler)(https://developer.android.com/reference/android/media/ImageReader.OnImageAvailableListener listener, https://developer.android.com/reference/android/os/Handler handler) ` Register a listener to be invoked when a new image becomes available from the ImageReader. |

| ### Protected methods ||
|---|---|
| ` void` | ` https://developer.android.com/reference/android/media/ImageReader#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. |

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

### acquireLatestImage

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

```
public Image acquireLatestImage ()
```

<br />


Acquire the latest `https://developer.android.com/reference/android/media/Image` from the ImageReader's queue, dropping older
`https://developer.android.com/reference/android/media/Image`. Returns `null` if no new image is available.


This operation will acquire all the images possible from the ImageReader,
but `https://developer.android.com/reference/android/media/ImageReader#close()` all images that aren't the latest. This function is
recommended to use over `https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()` for most use-cases, as it's
more suited for real-time processing.


Note that `https://developer.android.com/reference/android/media/ImageReader#getMaxImages()` should be at least 2 for
`https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()` to be any different than `https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()` -
discarding all-but-the-newest `https://developer.android.com/reference/android/media/Image` requires temporarily acquiring two
`https://developer.android.com/reference/android/media/Image` at once. Or more generally, calling `https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()`
with less than two images of margin, that is
`(maxImages - currentAcquiredImages < 2)` will not discard as expected.


This operation will fail by throwing an `https://developer.android.com/reference/java/lang/IllegalStateException` if
`maxImages` have been acquired with `https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()` or
`https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()`. In particular a sequence of `https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()`
calls greater than `https://developer.android.com/reference/android/media/ImageReader#getMaxImages()` without calling `https://developer.android.com/reference/android/media/Image#close()` in-between
will exhaust the underlying queue. At such a time, `https://developer.android.com/reference/java/lang/IllegalStateException`
will be thrown until more images are
released with `https://developer.android.com/reference/android/media/Image#close()`.

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/Image` | latest frame of image data, or `null` if no image data is available. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if too many images are currently acquired |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | Until API level 36 if the format of acquired image is different than format of the ImageReader. (If format is `https://developer.android.com/reference/android/graphics/ImageFormat#PRIVATE` or API level is 37 or later, the exception is not thrown and an image is acquired.) |

### acquireNextImage

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

```
public Image acquireNextImage ()
```

<br />


Acquire the next Image from the ImageReader's queue. Returns `null` if
no new image is available.

*Warning:* Consider using `https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()` instead, as it will
automatically release older images, and allow slower-running processing routines to catch
up to the newest frame. Usage of `https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()` is recommended for
batch/background processing. Incorrectly using this function can cause images to appear
with an ever-increasing delay, followed by a complete stall where no new images seem to
appear.


This operation will fail by throwing an `https://developer.android.com/reference/java/lang/IllegalStateException` if
`maxImages` have been acquired with `https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()` or
`https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()`. In particular a sequence of `https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()` or
`https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()` calls greater than `https://developer.android.com/reference/android/media/ImageReader#getMaxImages()` without
calling `https://developer.android.com/reference/android/media/Image#close()` in-between will exhaust the underlying queue. At such a time,
`https://developer.android.com/reference/java/lang/IllegalStateException` will be thrown until more images are released with
`https://developer.android.com/reference/android/media/Image#close()`.

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/Image` | a new frame of image data, or `null` if no image data is available. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException` | if `maxImages` images are currently acquired |
| `https://developer.android.com/reference/java/lang/UnsupportedOperationException` | Until API level 36 if the format of acquired image is different than format of the ImageReader. (If format is `https://developer.android.com/reference/android/graphics/ImageFormat#PRIVATE` or API level is 37 or later, the exception is not thrown and an image is acquired.) |

**See also:**

- `https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()`

### close

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

```
public void close ()
```

Free up all the resources associated with this ImageReader.


After calling this method, this ImageReader can not be used. Calling
any methods on this ImageReader and Images previously provided by
`https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()` or `https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()`
will result in an `https://developer.android.com/reference/java/lang/IllegalStateException`, and attempting to read from
`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.

<br />

<br />

### discardFreeBuffers

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

```
public void discardFreeBuffers ()
```

Discard any free buffers owned by this ImageReader.


Generally, the ImageReader caches buffers for reuse once they have been
allocated, for best performance. However, sometimes it may be important to
release all the cached, unused buffers to save on memory.


Calling this method will discard all free cached buffers. This does not include any buffers
associated with Images acquired from the ImageReader, any filled buffers waiting to be
acquired, and any buffers currently in use by the source rendering buffers into the
ImageReader's Surface.


The ImageReader continues to be usable after this call, but may need to reallocate buffers
when more buffers are needed for rendering.

<br />

<br />

### getDataSpace

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

```
public int getDataSpace ()
```

The default dataspace of `https://developer.android.com/reference/android/media/Image`.

Use this function if the ImageReader instance is created by builder pattern
`ImageReader.Builder` and `https://developer.android.com/reference/android/media/ImageReader.Builder#setDefaultDataSpace(int)`.

<br />

<br />

| Returns ||
|---|---|
| `int` | the expected dataspace of an 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` |

### getHardwareBufferFormat

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

```
public int getHardwareBufferFormat ()
```

The default `https://developer.android.com/reference/android/hardware/HardwareBuffer` format of `https://developer.android.com/reference/android/media/Image`.

Use this function if the ImageReader instance is created by builder pattern
`ImageReader.Builder` and using `https://developer.android.com/reference/android/media/ImageReader.Builder#setDefaultHardwareBufferFormat(int)` and
`https://developer.android.com/reference/android/media/ImageReader.Builder#setDefaultDataSpace(int)`.

<br />

<br />

| Returns ||
|---|---|
| `int` | the expected `https://developer.android.com/reference/android/hardware/HardwareBuffer` format of an Image. Value is one of the following: - `https://developer.android.com/reference/android/hardware/HardwareBuffer#RGBA_8888` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#RGBA_FP16` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#RGBA_1010102` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#RGBX_8888` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#RGB_888` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#RGB_565` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#BLOB` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#YCBCR_420_888` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#D_16` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#D_24` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#DS_24UI8` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#D_FP32` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#DS_FP32UI8` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#S_UI8` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#YCBCR_P010` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#YCBCR_P210` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#R_8` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#R_16` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#RG_1616` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#RGBA_10101010` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#R_12` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#R_14` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#RG_1212` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#RG_1414` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#RGBA_12121212` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#RGBA_14141414` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#BGRA_1010102` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#BGRX_1010102` |

### getHeight

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

```
public int getHeight ()
```

The default height of `https://developer.android.com/reference/android/media/Image`, in pixels.

The height may be overridden by the producer sending buffers to this
ImageReader's Surface. If so, the actual height of the images can be
found using `https://developer.android.com/reference/android/media/Image#getHeight()`.

<br />

<br />

| Returns ||
|---|---|
| `int` | the expected height of an Image <br /> |

### getImageFormat

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

```
public int getImageFormat ()
```

The default `https://developer.android.com/reference/android/graphics/ImageFormat` of `https://developer.android.com/reference/android/media/Image`.

Some color formats may be overridden by the producer sending buffers to
this ImageReader's Surface if the default color format allows. ImageReader
guarantees that all `https://developer.android.com/reference/android/media/Image` acquired from ImageReader
(for example, with `https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()`) will have a "compatible"
format to what was specified in `https://developer.android.com/reference/android/media/ImageReader#newInstance(int,%20int,%20int,%20int)`.
As of now, each format is only compatible to itself.
The actual format of the images can be found using `https://developer.android.com/reference/android/media/Image#getFormat()`.

Use this function if the ImageReader instance is created by factory method
`newInstance` function or by builder pattern `ImageReader.Builder` and using
`https://developer.android.com/reference/android/media/ImageReader.Builder#setImageFormat(int)`.

<br />

<br />

| Returns ||
|---|---|
| `int` | the expected format of an Image <br /> |

**See also:**

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

### getMaxImages

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

```
public int getMaxImages ()
```

Maximum number of images that can be acquired from the ImageReader by any time (for example,
with `https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()`).

An image is considered acquired after it's returned by a function from ImageReader, and
until the Image is `https://developer.android.com/reference/android/media/Image#close()` to release the image back to the ImageReader.

Attempting to acquire more than `maxImages` concurrently will result in the
acquire function throwing a `https://developer.android.com/reference/java/lang/IllegalStateException`. Furthermore,
while the max number of images have been acquired by the ImageReader user, the producer
enqueueing additional images may stall until at least one image has been released.

<br />

<br />

| Returns ||
|---|---|
| `int` | Maximum number of images for this ImageReader. <br /> |

**See also:**

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

### getSurface

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

```
public Surface getSurface ()
```

<br />

Get a `https://developer.android.com/reference/android/view/Surface` that can be used to produce `https://developer.android.com/reference/android/media/Image` for this
`ImageReader`.

Until valid image data is rendered into this `https://developer.android.com/reference/android/view/Surface`, the
`https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()` method will return `null`. Only one source
can be producing data into this Surface at the same time, although the
same `https://developer.android.com/reference/android/view/Surface` can be reused with a different API once the first source is
disconnected from the `https://developer.android.com/reference/android/view/Surface`.

Please note that holding on to the Surface object returned by this method is not enough
to keep its parent ImageReader from being reclaimed. In that sense, a Surface acts like a
`https://developer.android.com/reference/java/lang/ref/WeakReference` to the ImageReader that provides it.

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/view/Surface` | A `https://developer.android.com/reference/android/view/Surface` to use for a drawing target for various APIs. <br /> |

### getUsage

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

```
public long getUsage ()
```

The usage flag of images that can be produced by the ImageReader.

<br />

| Returns ||
|---|---|
| `long` | The usage flag of the images for this ImageReader. Value is either `0` or a combination of the following: - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_CPU_READ_RARELY` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_CPU_READ_OFTEN` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_CPU_WRITE_RARELY` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_CPU_WRITE_OFTEN` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_GPU_SAMPLED_IMAGE` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_GPU_COLOR_OUTPUT` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_COMPOSER_OVERLAY` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_PROTECTED_CONTENT` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_VIDEO_ENCODE` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_GPU_DATA_BUFFER` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_SENSOR_DIRECT_DATA` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_GPU_CUBE_MAP` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_GPU_MIPMAP_COMPLETE` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_FRONT_BUFFER` |

### getWidth

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

```
public int getWidth ()
```

The default width of `https://developer.android.com/reference/android/media/Image`, in pixels.

The width may be overridden by the producer sending buffers to this
ImageReader's Surface. If so, the actual width of the images can be
found using `https://developer.android.com/reference/android/media/Image#getWidth()`.

<br />

<br />

| Returns ||
|---|---|
| `int` | the expected width of an Image <br /> |

### newInstance

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

```
public static ImageReader newInstance (int width, 
                int height, 
                int format, 
                int maxImages, 
                long usage)
```

<br />


Create a new reader for images of the desired size, format and consumer usage flag.


The `maxImages` parameter determines the maximum number of `https://developer.android.com/reference/android/media/Image` objects that
can be be acquired from the `ImageReader` simultaneously. Requesting more buffers will
use up more memory, so it is important to use only the minimum number necessary for the use
case.


The valid sizes and formats depend on the source of the image data.


The format and usage flag combination describes how the buffer will be used by
consumer end-points. For example, if the application intends to send the images to
`https://developer.android.com/reference/android/media/MediaCodec` or `https://developer.android.com/reference/android/media/MediaRecorder` for hardware video
encoding, the format and usage flag combination needs to be
`https://developer.android.com/reference/android/graphics/ImageFormat#PRIVATE` and `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_VIDEO_ENCODE`. When an
`https://developer.android.com/reference/android/media/ImageReader` object is created with a valid size and such format/usage flag
combination, the application can send the `https://developer.android.com/reference/android/media/Image` to an `https://developer.android.com/reference/android/media/ImageWriter` that
is created with the input `https://developer.android.com/reference/android/view/Surface` provided by the
`https://developer.android.com/reference/android/media/MediaCodec` or `https://developer.android.com/reference/android/media/MediaRecorder`.


If the `format` is `https://developer.android.com/reference/android/graphics/ImageFormat#PRIVATE`, the created `https://developer.android.com/reference/android/media/ImageReader`
will produce images that are not directly accessible by the application. The application can
still acquire images from this `https://developer.android.com/reference/android/media/ImageReader`, and send them to the
`https://developer.android.com/reference/android/hardware/camera2/CameraDevice` for reprocessing, or to the
`https://developer.android.com/reference/android/media/MediaCodec` / `https://developer.android.com/reference/android/media/MediaRecorder` for hardware video
encoding via `https://developer.android.com/reference/android/media/ImageWriter` interface. However, the `https://developer.android.com/reference/android/media/Image#getPlanes()` will return an empty array for `https://developer.android.com/reference/android/graphics/ImageFormat#PRIVATE` format
images. The application can check if an existing reader's format by calling
`https://developer.android.com/reference/android/media/ImageReader#getImageFormat()`.


`https://developer.android.com/reference/android/graphics/ImageFormat#PRIVATE` format `https://developer.android.com/reference/android/media/ImageReader` are more
efficient to use when application access to image data is not necessary, compared to
ImageReaders using other format such as `https://developer.android.com/reference/android/graphics/ImageFormat#YUV_420_888`.


Note that not all format and usage flag combinations are supported by the
`https://developer.android.com/reference/android/media/ImageReader`. Below are the supported combinations by the `https://developer.android.com/reference/android/media/ImageReader`
(assuming the consumer end-points support the such image consumption, e.g., hardware video
encoding).

| Format | Compatible usage flags |
|---|---|
| non-`https://developer.android.com/reference/android/graphics/ImageFormat#PRIVATE` formats defined by `https://developer.android.com/reference/android/graphics/ImageFormat` or `https://developer.android.com/reference/android/graphics/PixelFormat` | `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_CPU_READ_RARELY` or `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_CPU_READ_OFTEN` |
| `https://developer.android.com/reference/android/graphics/ImageFormat#PRIVATE` | `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_VIDEO_ENCODE` or `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_GPU_SAMPLED_IMAGE`, or combined |

Using other combinations may result in `https://developer.android.com/reference/java/lang/IllegalArgumentException`. Additionally, specifying `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_CPU_WRITE_RARELY` or `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_CPU_WRITE_OFTEN` and writing to the ImageReader's buffers might break assumptions made by some producers, and should be used with caution.

<br />


If the `https://developer.android.com/reference/android/media/ImageReader` is used as an output target for a `https://developer.android.com/reference/android/hardware/camera2/CameraDevice`, and if the usage flag contains
`https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_VIDEO_ENCODE`, the timestamps of the
`https://developer.android.com/reference/android/media/Image` produced by the `https://developer.android.com/reference/android/media/ImageReader` won't be in the same timebase as
`https://developer.android.com/reference/android/os/SystemClock#elapsedRealtimeNanos()`, even if
`https://developer.android.com/reference/android/hardware/camera2/CameraCharacteristics#SENSOR_INFO_TIMESTAMP_SOURCE` is
`https://developer.android.com/reference/android/hardware/camera2/CameraMetadata#SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME`.
Instead, the timestamps will be roughly in the same timebase as in
`https://developer.android.com/reference/android/os/SystemClock#uptimeMillis()`, so that A/V synchronization could work for
video recording. In this case, the timestamps from the `https://developer.android.com/reference/android/media/ImageReader` with
`https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_VIDEO_ENCODE` usage flag may not be directly comparable with
timestamps of other streams or capture result metadata.

<br />

<br />

| Parameters ||
|---|---|
| `width` | `int`: The default width in pixels of the Images that this reader will produce. Value is 1 or greater <br /> |
| `height` | `int`: The default height in pixels of the Images that this reader will produce. Value is 1 or greater <br /> |
| `format` | `int`: The format of the Image that this reader will produce. This must be one of the `https://developer.android.com/reference/android/graphics/ImageFormat` or `https://developer.android.com/reference/android/graphics/PixelFormat` constants. Note that not all formats are supported, like ImageFormat.NV21. <br /> |
| `maxImages` | `int`: The maximum number of images the user will want to access simultaneously. This should be as small as possible to limit memory use. Once maxImages Images are obtained by the user, one of them has to be released before a new Image will become available for access through `https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()` or `https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()`. Must be greater than 0. Value is 1 or greater <br /> |
| `usage` | `long`: The intended usage of the images produced by this ImageReader. See the usages on `https://developer.android.com/reference/android/hardware/HardwareBuffer` for a list of valid usage bits. See also `https://developer.android.com/reference/android/hardware/HardwareBuffer#isSupported(int,%20int,%20int,%20int,%20long)` for checking if a combination is supported. If it's not supported this will throw an `https://developer.android.com/reference/java/lang/IllegalArgumentException`. Value is either `0` or a combination of the following: - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_CPU_READ_RARELY` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_CPU_READ_OFTEN` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_CPU_WRITE_RARELY` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_CPU_WRITE_OFTEN` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_GPU_SAMPLED_IMAGE` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_GPU_COLOR_OUTPUT` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_COMPOSER_OVERLAY` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_PROTECTED_CONTENT` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_VIDEO_ENCODE` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_GPU_DATA_BUFFER` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_SENSOR_DIRECT_DATA` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_GPU_CUBE_MAP` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_GPU_MIPMAP_COMPLETE` - `https://developer.android.com/reference/android/hardware/HardwareBuffer#USAGE_FRONT_BUFFER` |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/ImageReader` | This value cannot be `null`. <br /> |

**See also:**

- `https://developer.android.com/reference/android/media/Image`
- `https://developer.android.com/reference/android/hardware/HardwareBuffer`

### newInstance

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

```
public static ImageReader newInstance (int width, 
                int height, 
                int format, 
                int maxImages)
```

<br />


Create a new reader for images of the desired size and format.


The `maxImages` parameter determines the maximum number of
`https://developer.android.com/reference/android/media/Image` objects that can be be acquired from the
`ImageReader` simultaneously. Requesting more buffers will use up
more memory, so it is important to use only the minimum number necessary
for the use case.


The valid sizes and formats depend on the source of the image data.


If the `format` is `https://developer.android.com/reference/android/graphics/ImageFormat#PRIVATE`, the created
`https://developer.android.com/reference/android/media/ImageReader` will produce images that are not directly accessible
by the application. The application can still acquire images from this
`https://developer.android.com/reference/android/media/ImageReader`, and send them to the
`https://developer.android.com/reference/android/hardware/camera2/CameraDevice` for reprocessing via
`https://developer.android.com/reference/android/media/ImageWriter` interface. However, the `https://developer.android.com/reference/android/media/Image#getPlanes()` will return an empty array for `https://developer.android.com/reference/android/graphics/ImageFormat#PRIVATE` format images. The application can check if an existing reader's
format by calling `https://developer.android.com/reference/android/media/ImageReader#getImageFormat()`.


`https://developer.android.com/reference/android/graphics/ImageFormat#PRIVATE` format `https://developer.android.com/reference/android/media/ImageReader` are more efficient to use when application access to image
data is not necessary, compared to ImageReaders using other format such
as `https://developer.android.com/reference/android/graphics/ImageFormat#YUV_420_888`.

<br />

<br />

| Parameters ||
|---|---|
| `width` | `int`: The default width in pixels of the Images that this reader will produce. Value is 1 or greater <br /> |
| `height` | `int`: The default height in pixels of the Images that this reader will produce. Value is 1 or greater <br /> |
| `format` | `int`: The format of the Image that this reader will produce. This must be one of the `https://developer.android.com/reference/android/graphics/ImageFormat` or `https://developer.android.com/reference/android/graphics/PixelFormat` constants. Note that not all formats are supported, like ImageFormat.NV21. <br /> |
| `maxImages` | `int`: The maximum number of images the user will want to access simultaneously. This should be as small as possible to limit memory use. Once maxImages Images are obtained by the user, one of them has to be released before a new Image will become available for access through `https://developer.android.com/reference/android/media/ImageReader#acquireLatestImage()` or `https://developer.android.com/reference/android/media/ImageReader#acquireNextImage()`. Must be greater than 0. Value is 1 or greater <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/media/ImageReader` | This value cannot be `null`. <br /> |

**See also:**

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

### setOnImageAvailableListener

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

```
public void setOnImageAvailableListener (ImageReader.OnImageAvailableListener listener, 
                Handler handler)
```

Register a listener to be invoked when a new image becomes available
from the ImageReader.

<br />

| Parameters ||
|---|---|
| `listener` | `ImageReader.OnImageAvailableListener`: The listener that will be run. <br /> |
| `handler` | `Handler`: The handler on which the listener should be invoked, or null if the listener should be invoked on the calling thread's looper. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | If no handler specified and the calling thread has no looper. |

## Protected methods

### finalize

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

```
protected void finalize ()
```

Called by the garbage collector on an object when garbage collection
determines that there are no more references to the object.
A subclass overrides the `finalize` method to dispose of
system resources or to perform other cleanup.


The general contract of `finalize` is that it is invoked
if and when the Java virtual
machine has determined that there is no longer any
means by which this object can be accessed by any thread that has
not yet died, except as a result of an action taken by the
finalization of some other object or class which is ready to be
finalized. The `finalize` method may take any action, including
making this object available again to other threads; the usual purpose
of `finalize`, however, is to perform cleanup actions before
the object is irrevocably discarded. For example, the finalize method
for an object that represents an input/output connection might perform
explicit I/O transactions to break the connection before the object is
permanently discarded.


The `finalize` method of class `Object` performs no
special action; it simply returns normally. Subclasses of
`Object` may override this definition.


The Java programming language does not guarantee which thread will
invoke the `finalize` method for any given object. It is
guaranteed, however, that the thread that invokes finalize will not
be holding any user-visible synchronization locks when finalize is
invoked. If an uncaught exception is thrown by the finalize method,
the exception is ignored and finalization of that object terminates.


After the `finalize` method has been invoked for an object, no
further action is taken until the Java virtual machine has again
determined that there is no longer any means by which this object can
be accessed by any thread that has not yet died, including possible
actions by other objects or classes which are ready to be finalized,
at which point the object may be discarded.


The `finalize` method is never invoked more than once by a Java
virtual machine for any given object.


Any exception thrown by the `finalize` method causes
the finalization of this object to be halted, but is otherwise
ignored.

<br />

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/Throwable` |   |