qr code generation function kotlin code example
Example 1: qr code generation function kotlin
//Dependency
implementation 'com.google.zxing:core:3.4.0'
Example 2: qr code generation function kotlin
//Usage
val bitmap = generateQRCode("Sample Text")
imageView.setImageBitmap(bitmap)
Example 3: 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
}