//qrcode-kotlin/io.github.g0dkar.qrcode/QRCode

QRCode

class QRCode@JvmOverloadsconstructor(data: String, errorCorrectionLevel: ErrorCorrectionLevel = ErrorCorrectionLevel.M, dataType: QRCodeDataType = QRUtil.getDataType(data))

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")
qrCodeFile.outputStream().use { qrCodeRenderer.writeImage(it) }

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

Parameters

common

   
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.

See also

 
ErrorCorrectionLevel
QRUtil.getDataType

Constructors

   
QRCode [common]
@JvmOverloads
constructor(data: String, errorCorrectionLevel: ErrorCorrectionLevel = ErrorCorrectionLevel.M, dataType: QRCodeDataType = QRUtil.getDataType(data))

Types

Name Summary
Companion [common]
object Companion

Properties

Name Summary
qrCodeGraphicsFactory [common]
var qrCodeGraphicsFactory: QRCodeGraphicsFactory

Functions

Name Summary
computeImageSize [common]
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.
encode [common]
@JvmOverloads
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.
render [common]
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 :)
[common]
@JvmOverloads
fun render(cellSize: Int = DEFAULT_CELL_SIZE, margin: Int = DEFAULT_MARGIN, rawData: Array<Array<QRCodeSquare?>> = encode(), qrCodeGraphics: QRCodeGraphics = qrCodeGraphicsFactory.newGraphicsSquare( 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.
renderShaded [common]
@JvmOverloads
fun renderShaded(cellSize: Int = DEFAULT_CELL_SIZE, margin: Int = DEFAULT_MARGIN, rawData: Array<Array<QRCodeSquare?>> = encode(), qrCodeGraphics: QRCodeGraphics = qrCodeGraphicsFactory.newGraphicsSquare( computeImageSize( cellSize, margin, rawData ) ), renderer: (QRCodeSquare, QRCodeGraphics) -> Unit): QRCodeGraphics
Renders a QR Code image based on its computed data.
toString [common]
open override fun toString(): String