how do i render a byte array to image in compose m...
# compose
t
how do i render a byte array to image in compose multiplatform
t
this depends a heck of a lot on what you mean by render
what is in the byte array?
for example,
Copy code
import androidx.compose.foundation.Image
import androidx.compose.material.Surface
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.res.loadImageBitmap
import androidx.compose.ui.window.singleWindowApplication
import java.io.ByteArrayInputStream

fun main() = singleWindowApplication {
    val painter = remember {
        val byteArray ="""
            89 50 4E 47  0D 0A 1A 0A  00 00 00 0D  49 48 44 52 
            00 00 01 00  00 00 01 00  01 03 00 00  00 66 BC 3A 
            25 00 00 00  03 50 4C 54  45 B5 D0 D0  63 04 16 EA 
            00 00 00 1F  49 44 41 54  68 81 ED C1  01 0D 00 00 
            00 C2 A0 F7  4F 6D 0E 37  A0 00 00 00  00 00 00 00 
            00 BE 0D 21  00 00 01 9A  60 E1 D5 00  00 00 00 49 
            45 4E 44 AE  42 60 82
            """
            .split(Regex("\\s+"))
            .filterNot(String::isEmpty)
            .map { c -> c.toInt(16).toByte() }
            .toByteArray()

        BitmapPainter(image = loadImageBitmap(ByteArrayInputStream(byteArray)))
    }

    Surface {
        Image(painter = painter, contentDescription = "Image loaded from in-memory byte array")
    }
}
y
peekabo is a library they have an extension function that turns a byteArray to an image bitmap they only have an implementation for android and Ios you could check it out
👍 1
t
by render i mean display, and i got a encoded byte array from a network call
t
yeah but for example you could display the bytes in the raw data, or you could decode a PNG friom it
one of the things I have somewhere in my code elsewhere is a thing to render a byte array interpreting it as a raw raster
t
what is the loadImageBitmap function
t
Copy code
androidx.compose.ui.res.loadImageBitmap
Copy code
/**
 * Load and decode [ImageBitmap] from the given [inputStream]. [inputStream] should contain encoded
 * raster image in a format supported by Skia (BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP)
 *
 * @param inputStream input stream to load an rater image. All bytes will be read from this
 * stream, but stream will not be closed after this method.
 * @return the decoded SVG image associated with the resource
 */
fun loadImageBitmap(inputStream: InputStream): ImageBitmap =
so yeah if it's in one of those formats that's the one to use