# @forgesworn/qr > QR code encoder and decoder with no dependencies, for browsers, workers and Node. Versions 1-40, levels L/M/Q/H, numeric, alphanumeric and byte modes with optimal mixed segmentation, optional UTF-8 ECI, SVG, canvas and terminal output. The decoder reads RGBA or greyscale camera frames, including rotated, tilted, mirrored and inverted codes. Tests compare 148 symbols module for module with an independent encoder, read output back with an independent decoder, require the decoder to read at least as many synthetic distorted frames as that decoder, and hold a floor on a set of 184 real photographs, none of which may decode to the wrong text. ESM only, Node.js 22.13+. ## Install ```bash npm install @forgesworn/qr ``` ## API - `encode(data: string | Uint8Array, options?: EncodeOptions): QrCode` - options `ecc` ('L'|'M'|'Q'|'H', default 'M'), `version` (1-40), `minVersion`, `mask` (0-7), `mode` ('numeric'|'alphanumeric'|'byte': one segment of that mode; default: the fewest-bits mix of segments), `eci` (boolean, applies when there is a byte segment). Throws `QrCapacityError` (a RangeError) when the data does not fit; a `version` below `minVersion` throws a RangeError. - `QrCode` - `{ version, ecc, mask, mode, segments, size, modules }`; `segments` is `{ mode, count }[]` in order and `mode` the widest of them; `modules` is a row-major Uint8Array, 1 = dark, no quiet zone. - `isDark(qr, x, y): boolean` - false outside the symbol. - `toSvg(qr, { margin = 4, dark = '#000', light = '#fff' | null, title? }): string` - `drawCanvas(canvas, qr, { width = 8 px per module, margin = 4, dark, light | null }): void` - any HTMLCanvasElement or OffscreenCanvas; resizes it to width square. - `toTerminal(qr, { margin = 4, ink = 'light' | 'dark' }): string` - half-block text; 'light' ink for dark terminal backgrounds. - `decode(image: { data, width, height }, { rowStride?, channels?: 1 | 4, inversion?: 'normal' | 'inverted' | 'both' }): DecodeResult | undefined` - data is RGBA (ImageData) or greyscale; result `{ text, bytes, version, ecc, mask, eci, corrected, mirrored, inverted, location, structuredAppend?, fnc1? }`; `text` follows the ECI, else UTF-8, Shift_JIS or Latin-1 by content, and kanji and hanzi segments read as text; `structuredAppend` is `{ index, total, parity }`, `fnc1` is `{ position: 'first' | 'second', applicationIndicator? }`; undefined when no readable code is found. - `createDecoder()` - independent reusable camera decoder with `decode(image, options?)` and `reset()`. Tries the previous region first, falls back to the full frame in the same call, reads fresh pixels and never caches payloads. With several codes it favours the previous region; location estimates can differ slightly from stateless decoding. - `loadWasm(bytes)` and `wasmUrl` from `@forgesworn/qr/wasm` - optional bundled WebAssembly backend. Caller loads bytes; no automatic fetching. The returned instance has `encode`, `decode`, `decodeModules`, `createDecoder`, `reset`, `dispose`, `memoryBytes`. Camera sessions track independent regions and share native input storage; parent reset forgets their regions and disposal invalidates them. Outputs survive disposal. The packaged ABI 2 module uses host character sets with the same text rules as TypeScript; standalone Rust/default WASM retain their table-free limitations. - `decodeModules(modules, size)` - reads a row-major module grid (no quiet zone), straight or mirrored. - The decoder is built to return `undefined` rather than a wrong text: a block corrected past the standard's capacity is refused. Work on a frame is capped, but a frame with no code costs more than one with a code, so throttle camera loops. ## Example ```ts import { encode, toSvg } from '@forgesworn/qr' const svg = toSvg(encode('https://example.com/', { ecc: 'Q' })) import { decode } from '@forgesworn/qr' const text = decode(ctx.getImageData(0, 0, w, h))?.text ``` ## Common mistakes - ESM only: `import`, never `require`. - `decode` takes pixels (`{ data, width, height }`, RGBA or grey), not an `` or `