generate qr code kotlin code example
Example 1: qr code generation function kotlin
//Code
private fun generateQRCode(text: String): Bitmap {
val width = 500
val height = 500
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val codeWriter = MultiFormatWriter()
try {
val bitMatrix = codeWriter.encode(text, BarcodeFormat.QR_CODE, width, height)
for (x in 0 until width) {
for (y in 0 until height) {
bitmap.setPixel(x, y, if (bitMatrix[x, y]) Color.BLACK else Color.WHITE)
}
}
} catch (e: WriterException) {
Log.d(TAG, "generateQRCode: ${e.message}")
}
return bitmap
}
Example 2: qr code generation function kotlin
//Usage
val bitmap = generateQRCode("Sample Text")
imageView.setImageBitmap(bitmap)