roka-qr
Zero-dependency QR code encoder + decoder for Rust, with built-in PNG and PBM image I/O.
Implements ISO/IEC 18004 end-to-end: byte / alphanumeric / numeric mode decoding, all four error-correction levels (L/M/Q/H), all 40 versions, plus encoding and decoding from PNG or PBM images.
Highlights
- Zero external crate dependencies.
stdonly. - Encode + decode in one crate — fills a gap on crates.io.
- Self-contained image I/O — PNG encode + decode (including DEFLATE inflate), PBM P1 / P4. No
imageorflate2needed. - Round-trip tested against
qrencodeandzbarimg. - No
unsafe. 120+ tests, including RFC test vectors for the components (SHA-1, HMAC, RS, BCH).
Quick start
Encode a string and write it as PNG:
use ;
let code = new
.ec_level
.build?;
let png_bytes = code
.render
.scale
.quiet_zone
.build
.to_png;
write?;
# Ok::
Scan a QR code from a PNG file:
use Reader;
let bytes = read?;
let code = from_png?;
println!;
# Ok::
When to use this crate
- You want both encoding and decoding of QR codes in one place. The popular
qrcodecrate only encodes;rqrronly decodes. - You can't (or don't want to) pull in
image+flate2+ a separate QR decoder. - You're building security-sensitive software (auth, payments) and want a fully audit-friendly QR layer.
Constraints
- No camera/perspective correction. Input images must already be aligned and binarized. For phone-camera input, run them through a deskew/threshold step first.
- Decode supports byte / alphanumeric / numeric modes; encode only emits byte mode (sufficient for arbitrary 8-bit payloads). Kanji mode is not supported.
Performance
Indicative numbers on M2 (release); see BUDGETS.md:
| Operation | Time |
|---|---|
Encoder::build (otpauth URI, EcLevel::M) |
~55 µs |
Encoder::build (otpauth URI, EcLevel::H) |
~105 µs |
Bitmap::to_png (V6 at scale 8) |
~117 µs |
Bitmap::to_png (V11 at scale 8) |
~234 µs |
Reader::from_pbm (V6 otpauth M) |
~98 µs |
Reader::from_png (V6 otpauth M) |
~274 µs |
| Round-trip (URI → PNG → URI) | ~400 µs |
Regression gates live in tests/perf_gate.rs.
License
Dual-licensed under Apache 2.0 or MIT, at your option.
Part of the roka project by GOLIA Inc.