Generating codes¶
QR codes¶
import io.github.yuroyami.kiteqr.Qr
import io.github.yuroyami.kiteqr.qrcode.decoder.ErrorCorrectionLevel
val matrix = Qr.encode(
contents = "https://example.com",
ecLevel = ErrorCorrectionLevel.Q, // L < M < Q < H redundancy
characterSet = null, // e.g. "UTF-8", "Shift_JIS" to force an ECI
version = null, // pin 1..40, or null to auto-size
)
Qr.encode returns a tight module BitMatrix: one cell per module, no quiet
zone, no scaling. Render it with toSvg() / toPng() or the
Compose binding.
Numeric, alphanumeric, byte and Kanji (Shift_JIS) modes all work, as does the full ECI charset family. Mode selection uses the ordinary chooser.
Beyond the four parameters¶
Those four arguments are everything Qr.encode exposes. For anything else, use
QRCodeWriter or Encoder and pass an EncodeHintType map yourself.
QRCodeWriter scales the matrix and adds a margin. Encoder returns a QRCode.
| Hint | Effect |
|---|---|
QR_MASK_PATTERN |
Pins the mask (0..7) instead of letting the encoder score them. |
QR_COMPACT |
Switches to byte mode and runs MinimalEncoder. |
GS1_FORMAT |
Encodes a GS1 payload. |
ERROR_CORRECTION |
EC level. QRCodeWriter defaults to L, where Qr.encode defaults to M. |
MARGIN |
Quiet zone, in modules, for the writers that scale. |
MinimalEncoder is the optimizer that searches for the smallest encoding. It
runs only when QR_COMPACT is set. Qr.encode never sets it, so the facade
does not use it.
import io.github.yuroyami.kiteqr.BarcodeFormat
import io.github.yuroyami.kiteqr.EncodeHintType
import io.github.yuroyami.kiteqr.qrcode.QRCodeWriter
val matrix = QRCodeWriter().encode(
"https://example.com", BarcodeFormat.QR_CODE, 480, 480,
mapOf(
EncodeHintType.QR_MASK_PATTERN to 3,
EncodeHintType.QR_COMPACT to true,
EncodeHintType.MARGIN to 2,
),
)
Note the difference in contract. Qr.encode gives you a tight matrix.
QRCodeWriter and MultiFormatWriter give you one already scaled to the
requested width and height, with a margin. Do not pass a scaled matrix to
toSvg() / toPng() without setting quietZone = 0, or you get two margins.
Other barcode formats¶
For Aztec, Data Matrix, PDF417 and the 1D families, use MultiFormatWriter,
which returns a ready-scaled BitMatrix:
import io.github.yuroyami.kiteqr.BarcodeFormat
import io.github.yuroyami.kiteqr.MultiFormatWriter
val ean = MultiFormatWriter().encode("9780201379624", BarcodeFormat.EAN_13, 480, 120)
val dm = MultiFormatWriter().encode("payload", BarcodeFormat.DATA_MATRIX, 240, 240)
Per-format options go in the hints map (EncodeHintType.MARGIN,
ERROR_CORRECTION, CHARACTER_SET, and the format-specific ones).