💚 Disponível em Português (Brasil) 💛
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 ^^
~115KB
and it does what it says on the tin.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>
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 ;)
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)
}
The main changes coming from v3.3.0
are:
io.github.g0dkar.qrcode
to simply qrcode
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.
QRCode
class is compatible with the old one!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 :DCopyright since 2021 Rafael M. Lins, Licensed under the MIT License.
QR Code is trademarked by Denso Wave, Inc.
If you enjoyed the library and want to get me some coffee, use the buttons below