# HeifWriter

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

*** ** * ** ***

<br />

```
class HeifWriter : AutoCloseable
```

<br />

*** ** * ** ***

Writes one or more still images (of the same dimensions) into a heif file.

This class currently supports three input modes: `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#INPUT_MODE_BUFFER()`, `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#INPUT_MODE_SURFACE()`, or `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#INPUT_MODE_BITMAP()`.

The general sequence (in pseudo-code) to write a heif file using this class is as follows:

1) Construct the writer:`HeifWriter heifwriter = new HeifWriter(...);`

2) If using surface input mode, obtain the input surface:`Surface surface = heifwriter.getInputSurface();`

3) Call `start`:`heifwriter.start();`

4) Depending on the chosen input mode, add one or more images using one of these methods:`heifwriter.addYuvBuffer(...);` Or`heifwriter.addBitmap(...);` Or render to the previously obtained surface

5) Call `stop`:`heifwriter.stop(...);`

6) Close the writer:`heifwriter.close();`

Please refer to the documentations on individual methods for the exact usage.

## Summary

| ### Nested types |
|---|
| `class https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter.Builder` Builder class for constructing a HeifWriter object from specified parameters. |

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#INPUT_MODE_BITMAP() = 2` The input mode where the client adds bitmaps. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#INPUT_MODE_BUFFER() = 0` The input mode where the client adds input buffers with YUV data. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#INPUT_MODE_SURFACE() = 1` The input mode where the client renders the images to an input Surface created by the writer. |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#addBitmap(android.graphics.Bitmap)(bitmap: https://developer.android.com/reference/android/graphics/Bitmap.html)` Add one bitmap to the heif file. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#addExifData(int,byte[],int,int)( imageIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, exifData: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>, offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Add Exif data for the specified image. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#addYuvBuffer(int,byte[])(format: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, data: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>)` Add one YUV buffer to the heif file. |
| `https://developer.android.com/reference/android/view/Surface.html` | `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#getInputSurface()()` Retrieves the input surface for encoding. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#setInputEndOfStreamTimestamp(long)(timestampNs: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Set the timestamp (in nano seconds) of the last input frame to encode. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#start()()` Start the heif writer. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#stop(long)(timeoutMs: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Stop the heif writer synchronously. |

### Inherited functions

From [java.lang.AutoCloseable](https://developer.android.com/reference/java/lang/AutoCloseable.html) `abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/java/lang/AutoCloseable.html#close()()`

## Constants

### INPUT_MODE_BITMAP

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

```
const val INPUT_MODE_BITMAP = 2: Int
```

The input mode where the client adds bitmaps.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#addBitmap(android.graphics.Bitmap)` |   |

### INPUT_MODE_BUFFER

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

```
const val INPUT_MODE_BUFFER = 0: Int
```

The input mode where the client adds input buffers with YUV data.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#addYuvBuffer(int,byte[])` |   |

### INPUT_MODE_SURFACE

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

```
const val INPUT_MODE_SURFACE = 1: Int
```

The input mode where the client renders the images to an input Surface created by the writer. The input surface operates in single buffer mode. As a result, for use case where camera directly outputs to the input surface, this mode will not work because camera framework requires multiple buffers to operate in a pipeline fashion.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#getInputSurface()` |   |

## Public functions

### addBitmap

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

```
fun addBitmap(bitmap: Bitmap): Unit
```

Add one bitmap to the heif file.

| Parameters |
|---|---|
| `bitmap: https://developer.android.com/reference/android/graphics/Bitmap.html` | the bitmap to be added to the file. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if not started or not configured to use bitmap input. |

### addExifData

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

```
fun addExifData(
    imageIndex: Int,
    exifData: ByteArray<Byte>,
    offset: Int,
    length: Int
): Unit
```

Add Exif data for the specified image. The data must be a valid Exif data block, starting with "Exif\\0\\0" followed by the TIFF header (See JEITA CP-3451C Section 4.5.2.)

| Parameters |
|---|---|
| `imageIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | index of the image, must be a valid index for the max number of image specified by `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter.Builder#setMaxImages(int)`. |
| `exifData: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>` | byte buffer containing a Exif data block. |
| `offset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | offset of the Exif data block within exifData. |
| `length: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | length of the Exif data block. |

### addYuvBuffer

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

```
fun addYuvBuffer(format: Int, data: ByteArray<Byte>): Unit
```

Add one YUV buffer to the heif file.

| Parameters |
|---|---|
| `format: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The YUV format as defined in `https://developer.android.com/reference/android/graphics/ImageFormat.html`, currently only support YUV_420_888. |
| `data: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>` | byte array containing the YUV data. If the format has more than one planes, they must be concatenated. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if not started or not configured to use buffer input. |

### getInputSurface

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

```
fun getInputSurface(): Surface
```

Retrieves the input surface for encoding.

| Returns |
|---|---|
| `https://developer.android.com/reference/android/view/Surface.html` | the input surface if configured to use surface input. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if called after start or not configured to use surface input. |

### setInputEndOfStreamTimestamp

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

```
fun setInputEndOfStreamTimestamp(timestampNs: @IntRange(from = 0) Long): Unit
```

Set the timestamp (in nano seconds) of the last input frame to encode. This call is only valid for surface input. Client can use this to stop the heif writer earlier before the maximum number of images are written. If not called, the writer will only stop when the maximum number of images are written.

| Parameters |
|---|---|
| `timestampNs: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | timestamp (in nano seconds) of the last frame that will be written to the heif file. Frames with timestamps larger than the specified value will not be written. However, if a frame already started encoding when this is set, all tiles within that frame will be encoded. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if not started or not configured to use surface input. |

### start

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

```
fun start(): Unit
```

Start the heif writer. Can only be called once.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if called more than once. |

### stop

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

```
fun stop(timeoutMs: @IntRange(from = 0) Long): Unit
```

Stop the heif writer synchronously. Throws exception if the writer didn't finish writing successfully. Upon a success return: - For buffer and bitmap inputs, all images sent before stop will be written. - For surface input, images with timestamp on or before that specified in `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#setInputEndOfStreamTimestamp(long)` will be written. In case where `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#setInputEndOfStreamTimestamp(long)` was never called, stop will block until maximum number of images are received.

| Parameters |
|---|---|
| `timeoutMs: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | Maximum time (in microsec) to wait for the writer to complete, with zero indicating waiting indefinitely. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/Exception.html` | if encountered error, in which case the output file may not be valid. In particular, `https://developer.android.com/reference/java/util/concurrent/TimeoutException.html` is thrown when timed out, and `https://developer.android.com/reference/android/media/MediaCodec.CodecException.html` is thrown when encountered codec error. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/heifwriter/HeifWriter#setInputEndOfStreamTimestamp(long)` |   |