QR Code Generator — Kotlin source
Turn any text or URL into a downloadable QR code. Full ISO encoder with all error-correction levels, versions 1-40, SVG and PNG export, colors and quiet-zone control — runs entirely in your browser.
This is the Kotlin implementation — the same logic the interactive tool runs, in a shareable, citable form.
// qr-code-generator — Kotlin port: QR format information (BCH).
//
// Display snippet: assembling the module matrix is too long to show here;
// this ports formatInfoString(ecLevel, mask) from the TypeScript lib
// (src/lib/qr.ts), split into the EC-bits lookup and the BCH remainder.
// The full encoder (modes, Reed-Solomon EC, masking, matrix) lives in the
// TypeScript/Go sources.
//
// The 15-bit format string protects 5 data bits — 2 error-correction level
// bits (L=01 M=00 Q=11 H=10) followed by the 3-bit mask pattern — with a
// BCH(15,5) code using generator 0x537, then XORs the result with the
// fixed 0x5412 pattern so an all-zero string is never valid.
/** Error-correction level indicator bits (ISO/IEC 18004 table C.1); null = unknown. */
fun ecFormatBits(ecLevel: Char): Int? = when (ecLevel) {
'L' -> 0b01
'M' -> 0b00
'Q' -> 0b11
'H' -> 0b10
else -> null
}
/** 15-bit format info string (MSB first) for (ecLevel, mask); null on unknown level. */
fun formatInfo(ecLevel: Char, mask: Int): String? =
ecFormatBits(ecLevel)?.let { formatInfoBch((it shl 3) or mask) }
/** 15-bit format info string (MSB first) for a 5-bit data value. */
fun formatInfoBch(data5: Int): String {
var v = data5 shl 10
val g = 0x537 // BCH(15,5) generator polynomial
for (i in 14 downTo 10) {
if (v and (1 shl i) != 0) v = v xor (g shl (i - 10)) // polynomial long division
}
val full = ((data5 shl 10) or (v and 0x3FF)) xor 0x5412 // apply the mask pattern
return full.toString(2).padStart(15, '0')
}
// Example:
// formatInfo('L', 0) -> "111011111000100" (0x77C4, matching the published tables)
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →