qrcode
import "std/data" (included in import "std") · namespace std.data
QR codes (ISO/IEC 18004, versions 1 to 40) for payment links, two-factor setup, tickets and labels, drawn as SVG, as PNG, as an Image, in a PDF (PdfPage.qrCode), or as text in a terminal.
grav
import "std"
fn main() {
let qr = QrCode.encode("https://grav.dev")
print("version ${qr.version}, ${qr.size}x${qr.size} modules, mask ${qr.mask}")
print(qr.text(1))
let svg = qr.svg()
print(svg[..90])
let png = qr.png(4)
print("PNG ${Image.decode(png).width} px wide")
print(QrCode.encode("12345678901234567890", QrEcc.High).version)
}version 2, 25x25 modules, mask 6
▄▄▄▄▄▄▄ ▄▄▄▄ ▄▄▄ ▄▄▄▄▄▄▄
█ ▄▄▄ █ █▀ ▄███▀ █ ▄▄▄ █
█ ███ █ ▄ ██▀▄▀█▀ █ ███ █
█▄▄▄▄▄█ ▄ █▀█▀█ █ █▄▄▄▄▄█
▄ ▄▄▄▄▄▄█▀▄▄▄██▀▄ ▄ ▄▄▄
▄▀▄▀ ▄ ██▀ ▀ ██▄█▄█▀█▀▀▄
▀▀ ▄▄▄█▀██ ▄█▄▄▀▄█ ▀▀▀█
██▄ ██▄ ███ ▀ ▄██ ▄ █▄▄█▄
█ ▄▀▀▀▄ ▀▀▄█▀▀▄▄███▄▀█▄▀
▄▄▄▄▄▄▄ █▀█▀▄▄▄ █ ▄ █ ▀▀▄
█ ▄▄▄ █ █▄▀█ █ ██▄▄▄█ █
█ ███ █ ▀██▄▀█ ▄▀▀ ▄▄▄██
█▄▄▄▄▄█ ▄▀▄███ ▄▄▄▀▀▀▄▀▀█
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 33 33" shape-rendering="crispEdges"><
PNG 132 px wide
2- Encoding: the text goes into the densest single mode that holds it: numeric for digits only, alphanumeric for upper-case letters, digits and
$%*+-./:, otherwise bytes as UTF-8. It uses the smallest version that fits.encode(text, ecc, minVersion, boostEcc)sets the error correction:QrEcc.Low(7% of the code can be damaged),Medium(15%, the default),Quartile(25%) orHigh(30%).boostEccraises the level when that costs no extra size. Text too long for version 40 is aValueError. - Masks are chosen by the standard's penalty rules.
encodeWithMaskfixes one. The modules match other conforming encoders bit for bit, tested against python-qrcode. - Drawing:
svg(border, dark, light)gives one path with a rectangle per run of dark modules, which scales to any size.image(scale, border)andpng(scale, border)drawscalepixels per module.text(border, invert)uses half blocks, two module rows per line.get(x, y)reads a single module. - Keep the 4-module quiet zone (the default border) when the code will be printed or scanned from a screen.
API
enum QrEcc
How much of a QR code can be damaged and still read: Low 7%, Medium 15%, Quartile 25%, High 30% (more correction makes a bigger code for the same text).
Values
QrEcc.LowQrEcc.MediumQrEcc.QuartileQrEcc.High
class QrCode
A QR code: a square of dark and light modules.
Constructors
new QrCode()
Static members
static fn encode(text: string, ecc: QrEcc = QrEcc.Medium, minVersion: int = 1, boostEcc: bool = false) -> QrCode— The code fortextat the given error correction, in the smallest version that holds it (fromminVersion); ValueError when even version 40 is too small (about 2,900 bytes at Low).boostEccraises the correction when that costs no size.static fn encodeBytes(data: Bytes, ecc: QrEcc = QrEcc.Medium) -> QrCode— The code for raw bytes (byte mode).static fn encodeWithMask(text: string, ecc: QrEcc, mask: int, minVersion: int = 1) -> QrCode— encode with a fixed mask pattern (0 to 7), for tests and special printers.
Fields
size: int— Modules a side (21 for version 1, 4 more each version, 177 for version 40).version: int— 1 to 40.mask: int— The mask pattern chosen (0 to 7).ecc: QrEcc
Methods
fn get(x: int, y: int) -> bool— Whether the module at (x, y) is dark (outside the code: false).fn svg(border: int = 4, dark: string = "#000", light: string = "#fff") -> string— An SVG of the code with aborderof light modules (4 is what the standard asks), one path for all dark modules; it scales to any size.fn image(scale: int = 8, border: int = 4) -> Image— The code as a grey Image,scalepixels a module, with aborder.fn png(scale: int = 8, border: int = 4) -> Bytes— The code as a PNG.fn text(border: int = 2, invert: bool = false) -> string— The code drawn with block characters, two module rows a line (dark on a light terminal:invertfor a dark one).fn toString() -> string— The modules as rows of '#' and '.', for tests and debugging.