g0dkar/qrcode-kotlin

QRCode in fixed size?

Closed this issue · 1 comments

I need for a mini IoT project that the qrcode must have a fixed resolution for example 320X320, is that possible ?

g0dkar commented

Heya! Sorry for taking long to answer, somehow I didn't get notified :(

To answer you, yes you can have a fixed size QRCode! I've added a new example showing how to do that 👍

Try this out:

import io.github.g0dkar.qrcode.QRCode
import java.io.FileOutputStream

class ExactSizeQRCode {
    fun createQRCode(content: String, size: Int) {
        FileOutputStream("kotlin-exact-$size.png").use {
            val qrCode = QRCode(content)
            val qrCodeData = qrCode.encode()

            // qrCodeData[0].size = How many columns (X axis) the finished QRCode will have
            // qrCodeData.size = How many rows (Y axis) the finished QRCode will have
            // QRCodes are square so rows = columns ;)
            val sizePerCell: Int = size / qrCodeData.size

            val renderedImage = qrCode.render(cellSize = sizePerCell, margin = 0, rawData = qrCodeData)

            val finishedResult = if (renderedImage.width != size) {
                // Many times the image will be a bit smaller or a bit bigger because sizePerCell is an Int
                // This adds a few TRANSPARENT pixels of border around it (if smaller)
                // Or reduces the image by however many pixels needed, so it'll be the exact size
                val adjustedImage = qrCode.qrCodeGraphicsFactory.newGraphics(size, size)
                val adjustedX = (size / 2) - (renderedImage.width / 2)
                val adjustedY = (size / 2) - (renderedImage.width / 2)

                println("Adjusting image with x=$adjustedX, y=$adjustedY pixels...")

                adjustedImage.apply { drawImage(renderedImage, adjustedX, adjustedY) }
            } else {
                // Exact size image already! Yay!
                renderedImage
            }

            finishedResult.writeImage(it)
        }
    }
}

fun main() {
    ExactSizeQRCode()
        .createQRCode("Hello, 320px!", 320)
}