BitmapProcessor


@UnstableApi
class BitmapProcessor


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

Summary

Nested types

Exception thrown when bitmap processing fails.

Public functions

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

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

java-static Array<Byte>!

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

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

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

maxDimension: Int

The maximum allowed dimension in pixels.

Returns
Array<Byte>!

The compressed bitmap bytes.

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: Context!

The context to retrieve the maximum allowed dimension.

data: ByteArray<Byte>!

The raw compressed bitmap bytes.

Returns
Array<Byte>!

The optimized raw bitmap bytes.

Throws
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: ByteArray<Byte>!

The raw compressed bitmap bytes.

maxDimension: Int

The maximum allowed dimension in pixels.

Returns
Array<Byte>!

The optimized raw bitmap bytes.

Throws
androidx.media3.session.artwork.BitmapProcessor.BitmapProcessingException

If the bitmap data is invalid or processing fails.