# BitmapProcessor

Artifact: [androidx.media3:media3-session](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/session/artwork/BitmapProcessor.java+class:androidx.media3.session.artwork.BitmapProcessor)

*** ** * ** ***


```
@UnstableApi
class BitmapProcessor
```

<br />

*** ** * ** ***

Utility class for processing bitmaps, such as downsizing and re-compression.

## Summary

| ### Nested types |
|---|
| `class https://developer.android.com/reference/kotlin/androidx/media3/session/artwork/BitmapProcessor.BitmapProcessingException : https://developer.android.com/reference/java/lang/Exception.html` Exception thrown when bitmap processing fails. |

| ### Public functions |
|---|---|
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | `https://developer.android.com/reference/kotlin/androidx/media3/session/artwork/BitmapProcessor#scaleAndCompress(android.graphics.Bitmap,int)(bitmap: https://developer.android.com/reference/android/graphics/Bitmap.html!, maxDimension: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Scales the given `https://developer.android.com/reference/android/graphics/Bitmap.html` down proportionally if it exceeds the maximum allowed dimension and compresses it to a byte array. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | `https://developer.android.com/reference/kotlin/androidx/media3/session/artwork/BitmapProcessor#scaleDownIfExceedsLimit(android.content.Context,byte[])(context: https://developer.android.com/reference/android/content/Context.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>!)` Scales down the bitmap if it exceeds the maximum allowed dimension. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | `https://developer.android.com/reference/kotlin/androidx/media3/session/artwork/BitmapProcessor#scaleDownIfExceedsLimit(byte[],int)(data: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!, maxDimension: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Scales down the bitmap if it exceeds the maximum allowed dimension. |

## Public functions

### scaleAndCompress

```
java-static fun scaleAndCompress(bitmap: Bitmap!, maxDimension: Int): Array<Byte>!
```

Scales the given `https://developer.android.com/reference/android/graphics/Bitmap.html` down proportionally if it exceeds the maximum allowed dimension and compresses it to a byte array.

The input `bitmap` is NOT recycled.

| Parameters |
|---|---|
| `bitmap: https://developer.android.com/reference/android/graphics/Bitmap.html!` | The bitmap to process. |
| `maxDimension: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The maximum allowed dimension in pixels. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | The compressed bitmap bytes. |

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/session/artwork/BitmapProcessor.BitmapProcessingException` | If compression fails. |

### scaleDownIfExceedsLimit

```
java-static fun scaleDownIfExceedsLimit(context: Context!, data: ByteArray<Byte>!): Array<Byte>!
```

Scales down the bitmap if it exceeds the maximum allowed dimension.

If the bitmap dimensions are within bounds, the original bytes are returned to avoid unnecessary decompression/compression overhead. If the bitmap is larger, it is downscaled proportionally and compressed (PNG if it has transparency, JPEG 85% quality otherwise).

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html!` | The context to retrieve the maximum allowed dimension. |
| `data: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | The raw compressed bitmap bytes. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | The optimized raw bitmap bytes. |

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/session/artwork/BitmapProcessor.BitmapProcessingException` | If the bitmap data is invalid or processing fails. |

### scaleDownIfExceedsLimit

```
java-static fun scaleDownIfExceedsLimit(data: ByteArray<Byte>!, maxDimension: Int): Array<Byte>!
```

Scales down the bitmap if it exceeds the maximum allowed dimension.

If the bitmap dimensions are within bounds, the original bytes are returned to avoid unnecessary decompression/compression overhead. If the bitmap is larger, it is downscaled proportionally and compressed (PNG if it has transparency, JPEG 85% quality otherwise).

| Parameters |
|---|---|
| `data: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | The raw compressed bitmap bytes. |
| `maxDimension: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The maximum allowed dimension in pixels. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-byte/index.html>!` | The optimized raw bitmap bytes. |

| Throws |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/session/artwork/BitmapProcessor.BitmapProcessingException` | If the bitmap data is invalid or processing fails. |