QRCode

class QRCode @JvmOverloads constructor(val data: String, squareSize: Int = DEFAULT_SQUARE_SIZE, canvasSize: Int = DEFAULT_QRCODE_SIZE, val xOffset: Int = DEFAULT_X_OFFSET, val yOffset: Int = DEFAULT_Y_OFFSET, val colorFn: QRCodeColorFunction = DefaultColorFunction(), val shapeFn: QRCodeShapeFunction = DefaultShapeFunction(squareSize, innerSpace = 0), var graphicsFactory: QRCodeGraphicsFactory = QRCodeGraphicsFactory(), val errorCorrectionLevel: ErrorCorrectionLevel = ErrorCorrectionLevel.LOW, val informationDensity: Int = QRCodeProcessor.infoDensityForDataAndECL(data, errorCorrectionLevel), val maskPattern: MaskPattern = MaskPattern.PATTERN000, doBefore: QRCode.(QRCodeGraphics, Int, Int) -> Unit = EMPTY_FN, doAfter: QRCode.(QRCodeGraphics, Int, Int) -> Unit = EMPTY_FN)(source)

A simple class to create easily create aesthetic pleasing QRCodes.

It'll create a QRCodeProcessor and build a custom render function on top of it.

It includes things like:

  • QR Codes with a logo at the center

  • QR Codes with dots instead of squares

  • Colorful QR Codes (including linear gradient colors)

If you have a suggestion for a nice QR Code style, feel free to open a PR, or an Issue with your suggestion :)

Author

Rafael Lins - g0dkar

See also

Constructors

Link copied to clipboard
constructor(data: String, squareSize: Int = DEFAULT_SQUARE_SIZE, canvasSize: Int = DEFAULT_QRCODE_SIZE, xOffset: Int = DEFAULT_X_OFFSET, yOffset: Int = DEFAULT_Y_OFFSET, colorFn: QRCodeColorFunction = DefaultColorFunction(), shapeFn: QRCodeShapeFunction = DefaultShapeFunction(squareSize, innerSpace = 0), graphicsFactory: QRCodeGraphicsFactory = QRCodeGraphicsFactory(), errorCorrectionLevel: ErrorCorrectionLevel = ErrorCorrectionLevel.LOW, informationDensity: Int = QRCodeProcessor.infoDensityForDataAndECL(data, errorCorrectionLevel), maskPattern: MaskPattern = MaskPattern.PATTERN000, doBefore: QRCode.(QRCodeGraphics, Int, Int) -> Unit = EMPTY_FN, doAfter: QRCode.(QRCodeGraphics, Int, Int) -> Unit = EMPTY_FN)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Size of the canvas where the QRCode will be drawn into (the final image will be a square of canvasSize by canvasSize)

Link copied to clipboard

Function that will handle color processing (which color is "light" and which is "dark") - Defaults to DefaultColorFunction.

Link copied to clipboard

Data that will be encoded.

Link copied to clipboard

Error Correction Level to add to embed in the QRCode. The higher the ErrorCorrectionLevel is, the higher will be the QRCode tolerance to damage - Defaults to ErrorCorrectionLevel.LOW.

Link copied to clipboard

The QRCodeGraphics (aka "canvas") where all the drawing will happen

Link copied to clipboard
Link copied to clipboard

Information Density (formerly known as typeNum). A number that represents how much data this QRCode can hold - Defaults to a value computed by QRCodeProcessor.infoDensityForDataAndECL.

Link copied to clipboard

Which mask pattern to apply to the QRCode. Slightly change the squares. Mostly for aesthetics.

Link copied to clipboard

The underlying QRCodeProcessor object that will do all calculations

Link copied to clipboard

Raw QRCode data computed by QRCodeProcessor

Link copied to clipboard

Function that will handle drawing the shapes of each square - Defaults to DefaultShapeFunction with innerSpace = 0.

Link copied to clipboard
Link copied to clipboard

Offset drawing the QRCode by this amount on the X axis (horizontal) - Defaults to 0 (zero)

Link copied to clipboard

Offset drawing the QRCode by this amount on the Y axis (vertical) - Defaults to 0 (zero)

Functions

Link copied to clipboard
fun fitIntoArea(width: Int, height: Int): QRCode

Computes a squareSize to make sure the QRCode can fit into an area of width by height pixels

Link copied to clipboard
fun render(qrCodeGraphics: QRCodeGraphics = graphics, xOffset: Int = this.xOffset, yOffset: Int = this.yOffset): QRCodeGraphics

Executes all the drawing of the QRCode and returns the QRCodeGraphics of the complete QRCode.

Link copied to clipboard
fun renderToBytes(qrCodeGraphics: QRCodeGraphics = graphics, xOffset: Int = this.xOffset, yOffset: Int = this.yOffset, format: String = "PNG"): ByteArray

Calls render and then returns the bytes of a format (default = PNG) render of the QRCode.

Link copied to clipboard
fun reset()

Completely resets the QRCode drawing. After this, you can call renderToBytes or render to redraw the whole QRCode. Useful when you want, for example, a transparent background QRCode to add to a larger image and then the same QRCode drawn on top of a custom background.

Link copied to clipboard
fun resize(size: Int): QRCode

Computes a squareSize to make sure the QRCode can fit into an area of width by height pixels

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