QR Code Generator in Kotlin Using ZXing
QR codes (Quick Response) have become an integral part of the modern digital world. They are used for quickly sharing links, contact information, Wi-Fi passwords, and other data. If you are developing an application in Kotlin — whether for Android, server-side (Ktor, Spring), or desktop (Compose Desktop) — you may need to embed QR code generation functionality. In this article, we will look at how to create a simple and effective QR code generator in Kotlin using the popular ZXing (Zebra Crossing) library.
Why ZXing?
ZXing is a multifunctional library for reading and writing barcodes and QR codes. It is written in Java, making it perfectly compatible with Kotlin. Key advantages:
- Support for various formats: QR Code, Data Matrix, Aztec, PDF417, and others.
- High performance and customizability.
- Active community and regular updates.
- Easy integration into Kotlin projects.
Adding the Library
To start, add the dependency to your build.gradle.kts (if you are using Gradle):
dependencies { implementation("com.google.zxing:core:3.5.3") implementation("com.google.zxing:javase:3.5.3") // for working with BufferedImage}If you are writing a server application in Ktor or Spring, these dependencies will work. For Android, you can use the android-core module, but in most cases core is sufficient.
Basic QR Code Generator
Let's create a class QRCodeGenerator that will accept text and return a QR code image as a BufferedImage.
import com.google.zxing.BarcodeFormatimport com.google.zxing.EncodeHintTypeimport com.google.zxing.qrcode.QRCodeWriterimport com.google.zxing.common.BitMatriximport java.awt.image.BufferedImageimport java.util.*
class QRCodeGenerator {
fun generateQRCode(text: String, width: Int = 300, height: Int = 300): BufferedImage { // Setting encoding and error correction level val hints = EnumMap<EncodeHintType, Any>(EncodeHintType::class.java).apply { put(EncodeHintType.CHARACTER_SET, "UTF-8") put(EncodeHintType.ERROR_CORRECTION, com.google.zxing.qrcode.decoder.ErrorCorrectionLevel.H) // High put(EncodeHintType.MARGIN, 1) // Margins around the QR code }
val writer = QRCodeWriter() val bitMatrix: BitMatrix = writer.encode(text, BarcodeFormat.QR_CODE, width, height, hints)
// Converting BitMatrix to BufferedImage val image = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) for (x in 0 until width) { for (y in 0 until height) { image.setRGB(x, y, if (bitMatrix.get(x, y)) 0xFF000000.toInt() else 0xFFFFFFFF.toInt()) } } return image }}Explanation:
QRCodeWriter— ZXing class responsible for creating the dot matrix.BitMatrix— a binary matrix wheretruemeans a black pixel.- We set UTF-8 encoding and the maximum error correction level (H — 30% damage).
- The image is saved in ARGB format with a black and white color scheme.
Saving to a File (PNG)
To save the generated QR code to a file, use javax.imageio.ImageIO:
import java.io.Fileimport javax.imageio.ImageIO
fun saveQRCodeToFile(text: String, filePath: String) { val generator = QRCodeGenerator() val image = generator.generateQRCode(text) val outputFile = File(filePath) ImageIO.write(image, "png", outputFile) println("QR code saved: ${outputFile.ab