Added in [API level 8](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/android/graphics/YuvImage#pubctors) \| [Methods](https://developer.android.com/reference/android/graphics/YuvImage#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/android/graphics/YuvImage#inhmethods)

# YuvImage

*** ** * ** ***


`
public


class
YuvImage
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


`

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

<br />

*** ** * ** ***

YuvImage contains YUV data and provides a method that compresses a region of
the YUV data to a Jpeg. The YUV data should be provided as a single byte
array irrespective of the number of image planes in it.
Currently only ImageFormat.NV21 and ImageFormat.YUY2 are supported.

To compress a rectangle region in the YUV data, users have to specify the
region by left, top, width and height.

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/android/graphics/YuvImage#YuvImage(byte[],%20int,%20int,%20int,%20int[])(byte[] yuv, int format, int width, int height, int[] strides) ` Construct an YuvImage. |
| ` https://developer.android.com/reference/android/graphics/YuvImage#YuvImage(byte[],%20int,%20int,%20int,%20int[],%20android.graphics.ColorSpace)(byte[] yuv, int format, int width, int height, int[] strides, https://developer.android.com/reference/android/graphics/ColorSpace colorSpace) ` Construct an YuvImage. |

| ### Public methods ||
|---|---|
| ` boolean` | ` https://developer.android.com/reference/android/graphics/YuvImage#compressToJpeg(android.graphics.Rect,%20int,%20java.io.OutputStream)(https://developer.android.com/reference/android/graphics/Rect rectangle, int quality, https://developer.android.com/reference/java/io/OutputStream stream) ` Compress a rectangle region in the YuvImage to a jpeg. |
| ` boolean` | ` https://developer.android.com/reference/android/graphics/YuvImage#compressToJpegR(android.graphics.YuvImage,%20int,%20java.io.OutputStream)(https://developer.android.com/reference/android/graphics/YuvImage sdr, int quality, https://developer.android.com/reference/java/io/OutputStream stream) ` Compress the HDR image into JPEG/R format. |
| ` https://developer.android.com/reference/android/graphics/ColorSpace` | ` https://developer.android.com/reference/android/graphics/YuvImage#getColorSpace()() ` |
| ` int` | ` https://developer.android.com/reference/android/graphics/YuvImage#getHeight()() ` |
| ` int[]` | ` https://developer.android.com/reference/android/graphics/YuvImage#getStrides()() ` |
| ` int` | ` https://developer.android.com/reference/android/graphics/YuvImage#getWidth()() ` |
| ` byte[]` | ` https://developer.android.com/reference/android/graphics/YuvImage#getYuvData()() ` |
| ` int` | ` https://developer.android.com/reference/android/graphics/YuvImage#getYuvFormat()() ` |

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

## Public constructors

### YuvImage

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

```
public YuvImage (byte[] yuv, 
                int format, 
                int width, 
                int height, 
                int[] strides)
```

Construct an YuvImage. Use SRGB for as default `https://developer.android.com/reference/android/graphics/ColorSpace`.

<br />

| Parameters ||
|---|---|
| `yuv` | `byte`: The YUV data. In the case of more than one image plane, all the planes must be concatenated into a single byte array. <br /> |
| `format` | `int`: The YUV data format as defined in `https://developer.android.com/reference/android/graphics/ImageFormat`. <br /> |
| `width` | `int`: The width of the YuvImage. <br /> |
| `height` | `int`: The height of the YuvImage. <br /> |
| `strides` | `int`: (Optional) Row bytes of each image plane. If yuv contains padding, the stride of each image must be provided. If strides is null, the method assumes no padding and derives the row bytes by format and width itself. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if format is not support; width or height \<= 0; or yuv is null. |

### YuvImage

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

```
public YuvImage (byte[] yuv, 
                int format, 
                int width, 
                int height, 
                int[] strides, 
                ColorSpace colorSpace)
```

Construct an YuvImage.

<br />

| Parameters ||
|---|---|
| `yuv` | `byte`: The YUV data. In the case of more than one image plane, all the planes must be concatenated into a single byte array. This value cannot be `null`. <br /> |
| `format` | `int`: The YUV data format as defined in `https://developer.android.com/reference/android/graphics/ImageFormat`. <br /> |
| `width` | `int`: The width of the YuvImage. <br /> |
| `height` | `int`: The height of the YuvImage. <br /> |
| `strides` | `int`: (Optional) Row bytes of each image plane. If yuv contains padding, the stride of each image must be provided. If strides is null, the method assumes no padding and derives the row bytes by format and width itself. <br /> |
| `colorSpace` | `ColorSpace`: The YUV image color space as defined in `https://developer.android.com/reference/android/graphics/ColorSpace`. If the parameter is null, SRGB will be set as the default value. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if format is not support; width or height \<= 0; or yuv is null. |

## Public methods

### compressToJpeg

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

```
public boolean compressToJpeg (Rect rectangle, 
                int quality, 
                OutputStream stream)
```

Compress a rectangle region in the YuvImage to a jpeg.
For image format, only ImageFormat.NV21 and ImageFormat.YUY2 are supported.
For color space, only SRGB is supported.

<br />

| Parameters ||
|---|---|
| `rectangle` | `Rect`: The rectangle region to be compressed. The medthod checks if rectangle is inside the image. Also, the method modifies rectangle if the chroma pixels in it are not matched with the luma pixels in it. <br /> |
| `quality` | `int`: Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. <br /> |
| `stream` | `OutputStream`: OutputStream to write the compressed data. <br /> |

| Returns ||
|---|---|
| `boolean` | True if the compression is successful. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if rectangle is invalid; color space or image format is not supported; quality is not within \[0, 100\]; or stream is null. |

### compressToJpegR

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

```
public boolean compressToJpegR (YuvImage sdr, 
                int quality, 
                OutputStream stream)
```

Compress the HDR image into JPEG/R format.

Sample usage:
hdr_image.compressToJpegR(sdr_image, 90, stream);

For the SDR image, only YUV_420_888 image format is supported, and the following
color spaces are supported:
ColorSpace.Named.SRGB,
ColorSpace.Named.DISPLAY_P3

For the HDR image, only YCBCR_P010 image format is supported, and the following
color spaces are supported:
ColorSpace.Named.BT2020_HLG,
ColorSpace.Named.BT2020_PQ

<br />

| Parameters ||
|---|---|
| `sdr` | `YuvImage`: The SDR image, only ImageFormat.YUV_420_888 is supported. This value cannot be `null`. <br /> |
| `quality` | `int`: Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. <br /> |
| `stream` | `OutputStream`: OutputStream to write the compressed data. This value cannot be `null`. <br /> |

| Returns ||
|---|---|
| `boolean` | True if the compression is successful. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException` | if input images are invalid; quality is not within \[0, 100\]; or stream is null. |

### getColorSpace

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

```
public ColorSpace getColorSpace ()
```

<br />

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/android/graphics/ColorSpace` | the color space of the image. This value cannot be `null`. <br /> |

### getHeight

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

```
public int getHeight ()
```

<br />

<br />

| Returns ||
|---|---|
| `int` | the height of the image. <br /> |

### getStrides

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

```
public int[] getStrides ()
```

<br />

<br />

| Returns ||
|---|---|
| `int[]` | the number of row bytes in each image plane. <br /> |

### getWidth

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

```
public int getWidth ()
```

<br />

<br />

| Returns ||
|---|---|
| `int` | the width of the image. <br /> |

### getYuvData

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

```
public byte[] getYuvData ()
```

<br />

<br />

| Returns ||
|---|---|
| `byte[]` | the YUV data. <br /> |

### getYuvFormat

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

```
public int getYuvFormat ()
```

<br />

<br />

| Returns ||
|---|---|
| `int` | the YUV format as defined in `https://developer.android.com/reference/android/graphics/ImageFormat`. <br /> |