# 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
public final class BitmapProcessor
```

<br />

*** ** * ** ***

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

## Summary

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

| ### Public methods |
|---|---|
| `static byte[]` | `https://developer.android.com/reference/androidx/media3/session/artwork/BitmapProcessor#scaleAndCompress(android.graphics.Bitmap,int)(https://developer.android.com/reference/android/graphics/Bitmap.html bitmap, int maxDimension)` 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. |
| `static byte[]` | `https://developer.android.com/reference/androidx/media3/session/artwork/BitmapProcessor#scaleDownIfExceedsLimit(android.content.Context,byte[])(https://developer.android.com/reference/android/content/Context.html context, byte[] data)` Scales down the bitmap if it exceeds the maximum allowed dimension. |
| `static byte[]` | `https://developer.android.com/reference/androidx/media3/session/artwork/BitmapProcessor#scaleDownIfExceedsLimit(byte[],int)(byte[] data, int maxDimension)` Scales down the bitmap if it exceeds the maximum allowed dimension. |

## Public methods

### scaleAndCompress

```
public static byte[] scaleAndCompress(Bitmap bitmap, int maxDimension)
```

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 |
|---|---|
| `https://developer.android.com/reference/android/graphics/Bitmap.html bitmap` | The bitmap to process. |
| `int maxDimension` | The maximum allowed dimension in pixels. |

| Returns |
|---|---|
| `byte[]` | The compressed bitmap bytes. |

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

### scaleDownIfExceedsLimit

```
public static byte[] scaleDownIfExceedsLimit(Context context, byte[] data)
```

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 |
|---|---|
| `https://developer.android.com/reference/android/content/Context.html context` | The context to retrieve the maximum allowed dimension. |
| `byte[] data` | The raw compressed bitmap bytes. |

| Returns |
|---|---|
| `byte[]` | The optimized raw bitmap bytes. |

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

### scaleDownIfExceedsLimit

```
public static byte[] scaleDownIfExceedsLimit(byte[] data, int maxDimension)
```

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 |
|---|---|
| `byte[] data` | The raw compressed bitmap bytes. |
| `int maxDimension` | The maximum allowed dimension in pixels. |

| Returns |
|---|---|
| `byte[]` | The optimized raw bitmap bytes. |

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