//qrcode-kotlin/qrcode.raw/QRCodeProcessor
class QRCodeProcessor@JvmOverloadsconstructor(data: String, errorCorrectionLevel: ErrorCorrectionLevel = ErrorCorrectionLevel.M, dataType: QRCodeDataType = QRUtil.getDataType(data), val graphicsFactory: QRCodeGraphicsFactory = QRCodeGraphicsFactory())
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()
Rafael Lins - g0dkar
Kazuhiko Arase - kazuhikoarase
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. |
ErrorCorrectionLevel |
QRUtil.getDataType |
QRCodeProcessor | [common] @JvmOverloads constructor(data: String, errorCorrectionLevel: ErrorCorrectionLevel = ErrorCorrectionLevel.M, dataType: QRCodeDataType = QRUtil.getDataType(data), graphicsFactory: QRCodeGraphicsFactory = QRCodeGraphicsFactory()) |
Name | Summary |
---|---|
Companion | [common] object Companion |
Name | Summary |
---|---|
graphicsFactory | [common] val graphicsFactory: QRCodeGraphicsFactory |
Name | Summary |
---|---|
computeImageSize | [common] fun computeImageSize(cellSize: Int = DEFAULT_CELL_SIZE, margin: Int = 0, rawData: QRCodeRawData = 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): QRCodeRawData 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: QRCodeRawData = encode(), qrCodeGraphics: QRCodeGraphics = graphicsFactory.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: QRCodeRawData = encode(), qrCodeGraphics: QRCodeGraphics = graphicsFactory.newGraphicsSquare( computeImageSize( cellSize, margin, rawData, ), ), renderer: (Int, Int, QRCodeSquare, QRCodeGraphics) -> Unit): QRCodeGraphics Renders a QR Code image based on its computed data. |
toString | [common] open override fun toString(): String |