QRCode-Kotlin

License Maven Central

💚 Disponível em Português (Brasil) 💛

QRCode Kotlin Logo

Creating QRCodes in Kotlin (and Java) is harder than it should be. QRCode-Kotlin aims to bring a simple, straightforward and customizable way to create QRCodes, especially in the backend.

It is with this mission in mind that we keep doing our best to learn how developers use this library and their goals so that we can provide a better library/API for them. Please, feel free to share if and how you’re using this project ^^

Table of Contents

Installation

The library is available from Maven Central and NPM JS, so you can add it to your project as a dependency like any other:

Gradle:

implementation("io.github.g0dkar:qrcode-kotlin:4.1.1")

Maven:

<dependency>
    <groupId>io.github.g0dkar</groupId>
    <artifactId>qrcode-kotlin-jvm</artifactId> <!-- or qrcode-kotlin-android -->
    <version>4.1.1</version>
</dependency>

NodeJS:

npm install qrcode-kotlin@4.1.1

Browser:

<script src="https://cdn.jsdelivr.net/gh/g0dkar/qrcode-kotlin@main/release/qrcodeKotlin.min.js" type="application/javascript"></script>

Usage

To create QRCodes, the main class that should be used is the qrcode.render.QRCode class. It has static methods to help you create a QRCode the way you want:

// Use one of these:

val squares = QRCode.ofSquares()

val circles = QRCode.ofCircles()

val roundedSquares = QRCode.ofRoundedSquares()

With that, you’ll have a QRCodeBuilder instance. It has methods to adjust colors, size, spacing, add a logo and more! Also, make sure to check the Colors class as well.

Here’s a code to get you started:

val helloWorld = QRCode.ofSquares()
    .withColor(Colors.DEEP_SKY_BLUE) // Default is Colors.BLACK
    .withSize(10) // Default is 25
    .build("Hello world!")

// By default, QRCodes are rendered as PNGs.
val pngBytes = helloWorld.render()

FileOutputStream("hello-world.png").use { it.write(pngBytes) }

We highly recommend that you check out some examples:

The examples show pretty much all that can be done with the library! Even how to extend it so that it can create SVG QRCodes ;)

You can mix and match all those together. Try generating the library logo and banner with gradients and all in SVG ;)

Spring Framework and/or Spring Boot

As said earlier, one of the main reasons I developed this library was to use it on a backend application. So it is only natural to show how to do that :)

This Spring Framework/Boot controller method can generate QRCodes of a given content:

import org.springframework.core.io.ByteArrayResource
import org.springframework.http.HttpHeaders.CONTENT_DISPOSITION
import org.springframework.http.MediaType.IMAGE_PNG_VALUE

@GetMapping("/qrcode")
fun generateQrCode(content: String): ResponseEntity<ByteArrayResource> {
    val pngData = QRCode().ofSquares()
        .build(content)
        .render()
    val resource = ByteArrayResource(pngData, IMAGE_PNG_VALUE)

    return ResponseEntity.ok()
        .header(CONTENT_DISPOSITION, "attachment; filename=\"qrcode.png\"")
        .body(resource)
}

Changes from v3

The main changes coming from v3.3.0 are:

  1. The main package of the classes was changed from io.github.g0dkar.qrcode to simply qrcode
    • The old name doesn’t help languages that don’t have the “package” concept, and other Kotlin libraries already name their main package this way.
  2. The old QRCode class was rewritten to be easier to create better looking QRCodes . The previous QRCode class was renamed to QRCodeProcessor, with very minor API changes.
    • For most of the simple cases, the new QRCode class is compatible with the old one!
  3. A bunch of optimizations on how the QRCode is drawn. Previously, we’d had a canvas for each square, which would then be copied into the QRCode. This was changed to have just one large canvas where each square will be individually drawn directly.
  4. iOS and tvOS Support: Starting from v4.0.7 an initial implementation of the QRCodeGraphics so that iOS and tvOS are now supported. Any and all feedback are very welcome! - Thanks a lot to ruicanas for all his contributions to this feature :D

License

Copyright since 2021 Rafael M. Lins, Licensed under the MIT License.

QR Code is trademarked by Denso Wave, Inc.

Thanks and Acknowledgements

If you enjoyed the library and want to get me some coffee, use the buttons below :love_you_gesture:

Buy me a coffee over at Ko-fi!

Buy me a coffee over at PayPal!