I want to set an `<image>` a JPG I generated...
# webassembly
s
I want to set an
<image>
a JPG I generated in my WebAssembly. Using ChatGPT I got as far as the code below, but I don't understand how I can convert my ByteArray (containing JPG bytes) to a Blob that the HTMLImageElement will accept.
Copy code
htmlImageElement?.let { imageElement ->

    metadata.getExifThumbnailBytes()?.let { imageBytes ->

        val uint8Array: Uint8Array = imageBytes.toUint8Array()

        val blob = Blob(
            arrayOf(uint8Array), // FIXME
            BlobPropertyBag("image/jpeg")
        )

        val url = URL.Companion.createObjectURL(blob)

        imageElement.src = url
    }
}
1
r
Kotlin/Wasm doesn't support standard Kotlin array types in external declarations. You need to create a
JsArray
object.
s
How can I convert my ByteArray into a JsArray?
r
The Blob constructor first parameter is a single element array of bytearrays. So you have to create two arrays.
I'm not sure what is your
Uint8Array
- if it's external type you don't have to convert it.
Is it
org.khronos.webgl.Uint8Array
?
r
If so, it is external.
s
I converted it because I assumed Blob will need that.
r
I would try adding similar function:
Copy code
fun<T: JsAny> jsArrayOf(vararg elements: T): JsArray<T> {
    val array = JsArray<T>()
    for (i in elements.indices) {
        array[i] = elements[i]
    }
    return array
}
👍 1
And then use
jsArrayOf(uint8Array)
as Blob parameter
s
🎉 And that's the correct solution 😄
Thank you. 🙏
https://stefanoltmann.github.io/MetadataViewer/ is now capable of displaying the embedded thumbnail. 🎉
m
The correct link now probably is https://stefanoltmann.github.io/exif-viewer/
s
Yes, thank you.