BitmapProcessor


@UnstableApi
public final class BitmapProcessor


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

Summary

Nested types

Exception thrown when bitmap processing fails.

Public methods

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

Scales the given Bitmap down proportionally if it exceeds the maximum allowed dimension and compresses it to a byte array.

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

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

static byte[]
scaleDownIfExceedsLimit(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 Bitmap down proportionally if it exceeds the maximum allowed dimension and compresses it to a byte array.

The input bitmap is NOT recycled.

Parameters
Bitmap bitmap

The bitmap to process.

int maxDimension

The maximum allowed dimension in pixels.

Returns
byte[]

The compressed bitmap bytes.

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
Context context

The context to retrieve the maximum allowed dimension.

byte[] data

The raw compressed bitmap bytes.

Returns
byte[]

The optimized raw bitmap bytes.

Throws
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
androidx.media3.session.artwork.BitmapProcessor.BitmapProcessingException

If the bitmap data is invalid or processing fails.