msomu
01/19/2024, 8:27 AMandroidx.compose.ui.graphics.ImageBitmap
into a org.jetbrains.skia.Bitmap
using asSkiaBitmap
I’m getting this error Unresolved reference: asSkiaBitmap
Any help on resolving how to use asSkiaBitmap()
function in the commonMain
module?msomu
01/19/2024, 8:29 AMval apiEndpoint = "https://$hostName/$referenceName.png"
val fileType = "image/png"
val cloudflareSecret = "$cf-secret"
val response = httpClient.submitFormWithBinaryData(url = apiEndpoint,
formData = formData {
append("image",bitmap.asSkiaBitmap(), Headers.build {
append(HttpHeaders.ContentType, fileType)
append("X-CF-Secret", cloudflareSecret)
})
}, block = {
method = HttpMethod.Put
})
This is the ktor code I’m trying to execute and asSkiaBitmap()
is throwing an compile time error.Alexander Zhirkevich
01/19/2024, 9:23 AMorg.jetbrains.skia
can't be used on Android. it is visible in commonMain by mistake. if you want to use such convertions in common you have to write expect with actual Android bitmap and actual Skia bitmapmsomu
01/20/2024, 4:06 PMandroidx.compose.ui.graphics.ImageBitmap
to base64 string on Android using this code, how can I achieve the same in iOS?
I’m able to convert the imageBitmap to org.jetbrains.skia.Bitmap
using asSkiaBitmap()
after the Bitmap how can I convert it into a Base64 encodedString?
Any help or guidance? Thanks a lot.
actual fun ImageBitmap.toBase64(): String {
val bitmap = this.asAndroidBitmap()
val byteArrayOutputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream)
val byteArray = byteArrayOutputStream.toByteArray()
return Base64.encodeToString(byteArray, Base64.DEFAULT)
}
John O'Reilly
01/20/2024, 4:14 PMJohn O'Reilly
01/20/2024, 4:15 PMBase64.Default.decode()
in common codeJohn O'Reilly
01/20/2024, 4:15 PMmsomu
01/20/2024, 4:18 PMMichael Paus
01/20/2024, 6:50 PMimport androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asSkiaBitmap
import org.jetbrains.skia.EncodedImageFormat
import org.jetbrains.skia.Image
import kotlin.io.encoding.Base64
import kotlin.io.encoding.ExperimentalEncodingApi
@OptIn(ExperimentalEncodingApi::class)
fun ImageBitmap.toBase64(): String {
val encodedBytes = Image.makeFromBitmap(this.asSkiaBitmap()).encodeToData(EncodedImageFormat.PNG, 100)?.bytes
return Base64.Default.encode(encodedBytes!!)
}
I haven’t tested it so keep your fingers crossed when you first try it 😉.Zach Klippenstein (he/him) [MOD]
01/20/2024, 6:51 PM