What library can I use to load images (from a Byte...
# multiplatform
m
What library can I use to load images (from a ByteArray, JPEG/PNG/etc) into some common format? scrimage relies on javax.image which doesn't exist on android unfortunately.
s
Overall, what are you trying to achieve? Coil3 is a more focused library that loads and displays images from a variety of sources and formats. It supports multiplatform, as well. https://coil-kt.github.io/coil/
m
You don’t need any library for that. On Android:
Copy code
fun imageBitmapFromBytes(encodedImageData: ByteArray): ImageBitmap {
	return BitmapFactory.decodeByteArray(encodedImageData, 0, encodedImageData.size).asImageBitmap()
}
On all other platforms:
Copy code
fun imageBitmapFromBytes(encodedImageData: ByteArray): ImageBitmap {
	return Image.makeFromEncoded(encodedImageData).use { it.toComposeImageBitmap() }
}
☝🏼 1
☝️ 1
j
I think we made a jump in assumption that this was for Compose UI
m
Yes, that was my assumption 😉. Is there still anything else?
j
Sure, regular non-UI multiplatform code
Skiko can load images, for example.
You can also link against things like libpng or whatever formats you want directly, or a library which abstracts them (like ffmpeg).
m
That’s what I implicitly use in my code above in the non-Android version.
e
there's not going to be a good replacement for the full API of scrimage, so the best option will likely depend on what @martmists is trying to do
m
I'm trying to load images as pixel data for my NDArray implementation, and I haven't found a good way to load a given jpeg in a very portable way. I did see BitmapFactory.decodeByteArray but also a lot of reports that it only worked with PNG images.
j
It works with all formats supported by the OS
e
what it supports depends on the OS version but it should support more than PNG
m
I'm currently looking into android.graphics.ImageDecoder which should also support `File`s directly (I believe)
e
on Android sure, it's all the same underneath