QRCode

class QRCode @JvmOverloads constructor(data: String, errorCorrectionLevel: ErrorCorrectionLevel, dataType: QRCodeDataType)

A Class/Library that helps encode data as QR Code images without any external dependencies.

Rewritten in Kotlin from the original (GitHub).

To create a QR Code you can simply do the following:

val dataToEncode = "Hello QRCode!"
val eachQRCodeSquareSize = 10 // In Pixels!
val qrCodeRenderer = QRCode(dataToEncode).render(eachQRCodeSquareSize)

You can now use qrCodeRenderer to render your QRCode into any OutputStream (as a PNG by default)

For example, to simply save it on the disk:

val qrCodeFile = File("qrcode.png")
qrCodeRenderer.writeImage(qrCodeFile.outputStream())

Or maybe have it as a byte array, to be sent as a response to a server request:

val imageBytes = ByteArrayOutputStream()
.also { qrCodeRenderer.writeImage(it) }
.toByteArray()

Author

Rafael Lins - g0dkar

Kazuhiko Arase - kazuhikoarase

See also

io.github.g0dkar.qrcode.internals.QRUtil

Parameters

data

String that will be encoded in the QR Code.

errorCorrectionLevel

The level of Error Correction that should be applied to the QR Code. Defaults to ErrorCorrectionLevel.M.

dataType

One of the available QRCodeDataType. By default, the code tries to guess which one is the best fitting one from your input data.

Constructors

Link copied to clipboard
fun QRCode(data: String, errorCorrectionLevel: ErrorCorrectionLevel = ErrorCorrectionLevel.M, dataType: QRCodeDataType = QRUtil.getDataType(data))

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun computeImageSize(cellSize: Int = DEFAULT_CELL_SIZE, margin: Int = 0, rawData: Array<Array<QRCodeSquare?>> = encode()): Int
fun computeImageSize(cellSize: Int = DEFAULT_CELL_SIZE, margin: Int = DEFAULT_MARGIN, size: Int): Int

Compute the final size of the image of this QRCode based on the given cellSize and margin.

Link copied to clipboard
fun encode(type: Int = typeForDataAndECL(data, errorCorrectionLevel), maskPattern: MaskPattern = MaskPattern.PATTERN000): Array<Array<QRCodeSquare?>>

Computes and encodes the data of this object into a QR Code. This method returns the raw data of the QR Code.

Link copied to clipboard
fun render(cellSize: Int = DEFAULT_CELL_SIZE, margin: Int = DEFAULT_MARGIN, brightColor: Int = Colors.WHITE, darkColor: Int = Colors.BLACK, marginColor: Int = Colors.WHITE): QRCodeGraphics

Renders a QR Code image based on its computed data. This function exists to ease the interop with Java :)

fun render(cellSize: Int = DEFAULT_CELL_SIZE, margin: Int = DEFAULT_MARGIN, rawData: Array<Array<QRCodeSquare?>> = encode(), qrCodeGraphics: QRCodeGraphics = newGraphics(computeImageSize(cellSize, margin, rawData)), brightColor: Int = Colors.WHITE, darkColor: Int = Colors.BLACK, marginColor: Int = Colors.WHITE): QRCodeGraphics

Renders a QR Code image based on its computed data.

Link copied to clipboard
fun renderShaded(cellSize: Int = DEFAULT_CELL_SIZE, margin: Int = DEFAULT_MARGIN, renderer: (QRCodeSquare, QRCodeGraphics) -> Unit): QRCodeGraphics

Renders a QR Code image based on its computed data. This function exists to ease the interop with Java :)

fun renderShaded(cellSize: Int = DEFAULT_CELL_SIZE, margin: Int = DEFAULT_MARGIN, rawData: Array<Array<QRCodeSquare?>> = encode(), qrCodeGraphics: QRCodeGraphics = newGraphics(computeImageSize(cellSize, margin, rawData)), renderer: (QRCodeSquare, QRCodeGraphics) -> Unit): QRCodeGraphics

Renders a QR Code image based on its computed data.

Link copied to clipboard
open override fun toString(): String