DEV Community

loizenai
loizenai

Posted on

Kotlin – How to read/write QR Code with ZXing

https://grokonez.com/kotlin/kotlin-read-write-qr-code-zxing

Kotlin – How to read/write QR Code with ZXing

In this tutorial, we're gonna look at Kotlin examples that read and write QR Code using ZXing.

I. Dependency

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.2</version>
</dependency>

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.2</version>
</dependency>

II. Write Data to QR Code


package com.javasampleapproach.kotlin.qrcode

import java.io.IOException
import java.nio.file.FileSystems
import java.nio.file.Path
import com.google.zxing.BarcodeFormat
import com.google.zxing.WriterException
import com.google.zxing.client.j2se.MatrixToImageWriter
import com.google.zxing.common.BitMatrix
import com.google.zxing.qrcode.QRCodeWriter

@Throws(WriterException::class, IOException::class)
fun main(args: Array?) {

    val qrCodeWriter = QRCodeWriter()
    val bitMatrix = qrCodeWriter.encode(
            "JavaSampleApproach\nJava Technology, Spring Framework",
            BarcodeFormat.QR_CODE,
            350, 350) // width x height

    val path = FileSystems.getDefault().getPath("JSA-QRCode.png")
    MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path)
}

A QR Code is generated:
JSA-QRCode

III. Read Data from QR Code

More at:

https://grokonez.com/kotlin/kotlin-read-write-qr-code-zxing

Kotlin – How to read/write QR Code with ZXing

Top comments (0)