diff options
Diffstat (limited to 'wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/QRCodeComposable.kt')
-rw-r--r-- | wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/QRCodeComposable.kt | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/QRCodeComposable.kt b/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/QRCodeComposable.kt new file mode 100644 index 0000000..3fe6004 --- /dev/null +++ b/wallee-c2ec/app/src/main/java/ch/bfh/habej2/wallee_c2ec/QRCodeComposable.kt @@ -0,0 +1,56 @@ +package ch.bfh.habej2.wallee_c2ec + +import android.graphics.Bitmap +import android.graphics.Color +import androidx.compose.foundation.Image +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.runtime.Composable +import androidx.compose.ui.graphics.asImageBitmap +import androidx.compose.ui.platform.LocalConfiguration +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.min +import com.google.zxing.BarcodeFormat.QR_CODE +import com.google.zxing.qrcode.QRCodeWriter + +@Composable +fun QRCode(qrCodeContent: String) { + + Column { + + val qrCodeSize = getQrCodeSize() + val btmp = makeQrCode(qrCodeContent).asImageBitmap() + + Image( + modifier = androidx.compose.ui.Modifier + .size(qrCodeSize) + .padding(vertical = 8.dp), + bitmap = btmp, + contentDescription = "Scan the QR Code to start withdrawal", + ) + } +} + +@Composable +fun getQrCodeSize(): Dp { + val configuration = LocalConfiguration.current + val screenHeight = configuration.screenHeightDp.dp + val screenWidth = configuration.screenWidthDp.dp + return min(screenHeight, screenWidth) +} + +private fun makeQrCode(text: String, size: Int = 256): Bitmap { + val qrCodeWriter = QRCodeWriter() + val bitMatrix = qrCodeWriter.encode(text, QR_CODE, size, size) + val height = bitMatrix.height + val width = bitMatrix.width + val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565) + for (x in 0 until width) { + for (y in 0 until height) { + bmp.setPixel(x, y, if (bitMatrix.get(x, y)) Color.BLACK else Color.WHITE) + } + } + return bmp +}
\ No newline at end of file |