{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

ImageBitmapKt

public final class ImageBitmapKt


Summary

Public methods

static final @NonNull ImageBitmap
ImageBitmap(
    int width,
    int height,
    @NonNull ImageBitmapConfig config,
    boolean hasAlpha,
    @NonNull ColorSpace colorSpace
)
static final @NonNull PixelMap
toPixelMap(
    @NonNull ImageBitmap receiver,
    int startX,
    int startY,
    int width,
    int height,
    @NonNull int[] buffer,
    int bufferOffset,
    int stride
)

Convenience method to extract pixel information from the given ImageBitmap into a PixelMap that supports for querying pixel information based on

Public methods

ImageBitmap

public static final @NonNull ImageBitmap ImageBitmap(
    int width,
    int height,
    @NonNull ImageBitmapConfig config,
    boolean hasAlpha,
    @NonNull ColorSpace colorSpace
)

toPixelMap

public static final @NonNull PixelMap toPixelMap(
    @NonNull ImageBitmap receiver,
    int startX,
    int startY,
    int width,
    int height,
    @NonNull int[] buffer,
    int bufferOffset,
    int stride
)

Convenience method to extract pixel information from the given ImageBitmap into a PixelMap that supports for querying pixel information based on

Note this method can block so it is recommended to not invoke this method in performance critical code paths

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.PixelMap
import androidx.compose.ui.graphics.toPixelMap

val imageBitmap = createImageBitmap()

// Sample a 3 by 2 subsection of the given ImageBitmap
// starting at the coordinate (48, 49)
val pixelmap = imageBitmap.toPixelMap(
    startX = 48,
    startY = 49,
    width = 3,
    height = 2
)

// create a histogram to count the number of occurrences of a color within the specified
// subsection of the provided ImageBitmap
val histogram = HashMap<Color, Int>()
for (x in 0 until pixelmap.width) {
    for (y in 0 until pixelmap.height) {
        val color = pixelmap[x, y]
        val colorCount = histogram[color] ?: 0
        histogram[color] = (colorCount + 1)
    }
}
Parameters
int startX

The x-coordinate of the first pixel to read from the ImageBitmap

int startY

The y-coordinate of the first pixel to read from the ImageBitmap

int width

The number of pixels to read from each row

int height

The number of rows to read

@NonNull int[] buffer

The array to store the ImageBitmap's colors. By default this allocates an IntArray large enough to store all the pixel information. Consumers of this API are advised to use the smallest IntArray necessary to extract relevant pixel information

int bufferOffset

The first index to write into the buffer array, this defaults to 0

int stride

The number of entries in buffer to skip between rows (must be >= width

See also
readPixels