Skip to content

Instantly share code, notes, and snippets.

@dev-niiaddy
Last active January 23, 2023 09:28
Embed
What would you like to do?
A function to encode text as a QR code using BitmapPainter and zxing-core for use in jetpack compose with Image composable.
@Composable
fun rememberQrBitmapPainter(
content: String,
size: Dp = 150.dp,
padding: Dp = 0.dp
): BitmapPainter {
val density = LocalDensity.current
val sizePx = with(density) { size.roundToPx() }
val paddingPx = with(density) { padding.roundToPx() }
var bitmap by remember(content) {
mutableStateOf<Bitmap?>(null)
}
LaunchedEffect(bitmap) {
if (bitmap != null) return@LaunchedEffect
launch(Dispatchers.IO) {
val qrCodeWriter = QRCodeWriter()
val encodeHints = mutableMapOf<EncodeHintType, Any?>()
.apply {
this[EncodeHintType.MARGIN] = paddingPx
}
val bitmapMatrix = try {
qrCodeWriter.encode(
content, BarcodeFormat.QR_CODE,
sizePx, sizePx, encodeHints
)
} catch (ex: WriterException) {
null
}
val matrixWidth = bitmapMatrix?.width ?: sizePx
val matrixHeight = bitmapMatrix?.height ?: sizePx
val newBitmap = Bitmap.createBitmap(
bitmapMatrix?.width ?: sizePx,
bitmapMatrix?.height ?: sizePx,
Bitmap.Config.ARGB_8888,
)
for (x in 0 until matrixWidth) {
for (y in 0 until matrixHeight) {
val shouldColorPixel = bitmapMatrix?.get(x, y) ?: false
val pixelColor = if (shouldColorPixel) Color.BLACK else Color.WHITE
newBitmap.setPixel(x, y, pixelColor)
}
}
bitmap = newBitmap
}
}
return remember(bitmap) {
val currentBitmap = bitmap ?: Bitmap.createBitmap(
sizePx, sizePx,
Bitmap.Config.ARGB_8888,
).apply { eraseColor(Color.TRANSPARENT) }
BitmapPainter(currentBitmap.asImageBitmap())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment