From edd66d807dbb8e688f5093cd5de9ec65e1c025e1 Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 27 Aug 2024 03:12:57 +0200 Subject: [PATCH] Fixed that toComposeImageBitmap() is not available on Android --- .../codinux/banking/ui/service/ImageService.android.kt | 8 ++++++++ .../net/codinux/banking/ui/dialogs/EnterTanDialog.kt | 7 ++----- .../kotlin/net/codinux/banking/ui/service/ImageService.kt | 5 +++++ .../codinux/banking/ui/service/ImageService.desktop.kt | 8 ++++++++ .../net/codinux/banking/ui/service/ImageService.js.kt | 8 ++++++++ 5 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 composeApp/src/androidMain/kotlin/net/codinux/banking/ui/service/ImageService.android.kt create mode 100644 composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/ImageService.kt create mode 100644 composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/service/ImageService.desktop.kt create mode 100644 composeApp/src/jsMain/kotlin/net/codinux/banking/ui/service/ImageService.js.kt diff --git a/composeApp/src/androidMain/kotlin/net/codinux/banking/ui/service/ImageService.android.kt b/composeApp/src/androidMain/kotlin/net/codinux/banking/ui/service/ImageService.android.kt new file mode 100644 index 0000000..a5cf275 --- /dev/null +++ b/composeApp/src/androidMain/kotlin/net/codinux/banking/ui/service/ImageService.android.kt @@ -0,0 +1,8 @@ +package net.codinux.banking.ui.service + +import android.graphics.BitmapFactory +import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.graphics.asImageBitmap + +actual fun createImageBitmap(imageBytes: ByteArray): ImageBitmap = + BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size).asImageBitmap() \ No newline at end of file diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/EnterTanDialog.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/EnterTanDialog.kt index ef19c9a..8cd467f 100644 --- a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/EnterTanDialog.kt +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/dialogs/EnterTanDialog.kt @@ -12,7 +12,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.graphics.Color -import androidx.compose.ui.graphics.toComposeImageBitmap import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp @@ -26,7 +25,6 @@ import net.codinux.banking.ui.model.TanChallengeReceived import net.codinux.log.Log import org.jetbrains.compose.resources.imageResource import org.jetbrains.compose.ui.tooling.preview.Preview -import org.jetbrains.skia.Image import kotlin.io.encoding.Base64 import kotlin.io.encoding.ExperimentalEncodingApi import bankmeister.composeapp.generated.resources.zoom_in @@ -140,8 +138,7 @@ fun EnterTanDialog(tanChallengeReceived: TanChallengeReceived, onDismiss: () -> challenge.tanImage?.let { tanImage -> if (tanImage.decodingSuccessful) { - val byteArray = Base64.decode(tanImage.imageBytesBase64) - val bitmap = Image.makeFromEncoded(byteArray) + val imageBytes = Base64.decode(tanImage.imageBytesBase64) Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically) { Text("Größe") @@ -154,7 +151,7 @@ fun EnterTanDialog(tanChallengeReceived: TanChallengeReceived, onDismiss: () -> } Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically) { - Image(bitmap.toComposeImageBitmap(), "Bild mit enkodierter TAN", Modifier.height(tanImageHeight.dp), contentScale = ContentScale.FillHeight) + Image(createImageBitmap(imageBytes), "Bild mit enkodierter TAN", Modifier.height(tanImageHeight.dp), contentScale = ContentScale.FillHeight) } } } diff --git a/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/ImageService.kt b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/ImageService.kt new file mode 100644 index 0000000..09cf5dc --- /dev/null +++ b/composeApp/src/commonMain/kotlin/net/codinux/banking/ui/service/ImageService.kt @@ -0,0 +1,5 @@ +package net.codinux.banking.ui.service + +import androidx.compose.ui.graphics.ImageBitmap + +expect fun createImageBitmap(imageBytes: ByteArray): ImageBitmap \ No newline at end of file diff --git a/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/service/ImageService.desktop.kt b/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/service/ImageService.desktop.kt new file mode 100644 index 0000000..0853520 --- /dev/null +++ b/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/service/ImageService.desktop.kt @@ -0,0 +1,8 @@ +package net.codinux.banking.ui.service + +import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.graphics.toComposeImageBitmap +import org.jetbrains.skia.Image + +actual fun createImageBitmap(imageBytes: ByteArray): ImageBitmap = + Image.makeFromEncoded(imageBytes).toComposeImageBitmap() \ No newline at end of file diff --git a/composeApp/src/jsMain/kotlin/net/codinux/banking/ui/service/ImageService.js.kt b/composeApp/src/jsMain/kotlin/net/codinux/banking/ui/service/ImageService.js.kt new file mode 100644 index 0000000..0853520 --- /dev/null +++ b/composeApp/src/jsMain/kotlin/net/codinux/banking/ui/service/ImageService.js.kt @@ -0,0 +1,8 @@ +package net.codinux.banking.ui.service + +import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.graphics.toComposeImageBitmap +import org.jetbrains.skia.Image + +actual fun createImageBitmap(imageBytes: ByteArray): ImageBitmap = + Image.makeFromEncoded(imageBytes).toComposeImageBitmap() \ No newline at end of file