QR Code Generator in Kotlin: A Complete Guide
QR codes have become an integral part of the modern digital world. They are used for quickly exchanging information: from website links to contact details and Wi-Fi passwords. In this article, we will look at how to create your own QR code generator in Kotlin using the popular ZXing library ("Zebra Crossing"). Kotlin, being a modern and concise language, is perfect for developing both server-side and mobile applications for Android. You will learn how to generate various types of QR codes, customize their appearance, and integrate them into your projects.
Why Kotlin and ZXing?
Kotlin is a statically typed language that runs on top of the JVM and is actively supported by Google for Android development. Its syntax is clean and expressive, making code more readable and less error-prone. The ZXing library is the de facto standard for working with QR codes and barcodes in the Java/Kotlin ecosystem. It supports many formats (QR Code, Data Matrix, Aztec, and others) and provides flexible customization options.
Installation and Setup
To get started, add the dependency to your build.gradle.kts file (if you are using Gradle):
// For a JVM projectimplementation("com.google.zxing:core:3.5.3")implementation("com.google.zxing:javase:3.5.3")
// For an Android project (usually core is enough)implementation("com.google.zxing:core:3.5.3")If you are working with Maven, add the corresponding dependencies to pom.xml.
Basic Example: Generating a Simple QR Code
Let's create a simple function that generates a QR code from a text string and saves it as a PNG image.
import com.google.zxing.BarcodeFormatimport com.google.zxing.qrcode.QRCodeWriterimport com.google.zxing.client.j2se.MatrixToImageWriterimport java.nio.file.Paths
fun generateQRCode(text: String, filePath: String, width: Int = 300, height: Int = 300) { val writer = QRCodeWriter() val bitMatrix = writer.encode(text, BarcodeFormat.QR_CODE, width, height) val path = Paths.get(filePath) MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path) println("QR code successfully saved to: $filePath")}
fun main() { generateQRCode("https://pythonlib.ru", "qrcode_pythonlib.png")}Code Explanation:
QRCodeWriter()— creates an instance of the writer for QR codes.encode()— encodes the string into a bit matrix (black and white image).MatrixToImageWriter.writeToPath()— a convenient method for saving the matrix to a file.
Color and Size Customization
By default, the QR code is black and white. You can change its colors using MatrixToImageConfig. Here is an example with a green code on a white background:
import com.google.zxing.client.j2se.MatrixToImageConfigimport java.awt.Colorimport java.awt.image.BufferedImage
fun generateColoredQRCode(text: String, filePath: String, foregroundColor: Color = Color.BLACK, backgroundColor: Color = Color.WHITE) { val writer = QRCodeWriter() val bitMatrix = writer.encode(text, BarcodeFormat.QR_CODE, 300, 300) val config = MatrixToImageConfig(foregroundColor.rgb, backgroundColor.rgb) val bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix, config) // Save the BufferedImage to a file val outputFile = java.io.File(filePath) javax.imageio.ImageIO.write(bufferedImage, "PNG", outputFile) println("Colored QR code saved to: $filePath")}
fun main() { generateColoredQRCode("Hello, Kotlin!", "colored_qr.png", foregroundColor = Color(0x00AA00), Also in library
Working with ZIP Archives in PHP: Complete Guide to ZipArchive
Marten for C# (.NET): Complete Guide with Examples
crossbeam for Rust: Complete Guide with Examples
Working with XML in JavaScript (Node.js): Parsing, Creation, and Validation
better-sqlite3-typescript for TypeScript: complete guide with examples