Function

toPixelMap

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

ImageBitmapToPixelMapSample

/**
 * Sample showing how to obtain a [PixelMap] to query pixel information from an underlying
 * [ImageBitmap]
 */
@Sampled
fun ImageBitmapToPixelMapSample() {
    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)
        }
    }
}

/**
 * [ImageBitmap.readPixels] sample that shows how to create a consumer defined IntArray to store
 * pixel information and create a PixelMap for querying information within the buffer
 */
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: