@ashaffah/qrcodegen
v1.0.0
Published
QR Code generator library
Downloads
58
Readme
@ashaffah/qrcodegen
A TypeScript QR Code generator library with zero runtime dependencies. Supports SVG output and raw pixel buffer for custom rendering pipelines.
Based on the QR Code generator library by Project Nayuki (MIT License).
Installation
npm install @ashaffah/qrcodegenUsage
Generate an SVG
import { QrCode, toSvgString } from "@ashaffah/qrcodegen";
const qr = QrCode.encodeText("Hello, World!", QrCode.Ecc.MEDIUM);
const svg = toSvgString(qr);
// Write to file
import fs from "node:fs";
fs.writeFileSync("qrcode.svg", svg);Encode Binary Data
import { QrCode } from "@ashaffah/qrcodegen";
const data = [0x48, 0x65, 0x6c, 0x6c, 0x6f]; // "Hello" in bytes
const qr = QrCode.encodeBinary(data, QrCode.Ecc.HIGH);Advanced: Custom Segments
import { QrCode, QrSegment } from "@ashaffah/qrcodegen";
const segments = QrSegment.makeSegments("HELLO WORLD");
const qr = QrCode.encodeSegments(segments, QrCode.Ecc.LOW, 1, 40, -1, true);API
QrCode
The main class for generating QR Codes.
Static Methods
| Method | Description |
| ------------------------------------------------------------------------------ | ----------------------------------------------------------------------- |
| QrCode.encodeText(text, ecl) | Encode a Unicode string. Automatically selects the smallest QR version. |
| QrCode.encodeBinary(data, ecl) | Encode raw bytes (max 2953 bytes). |
| QrCode.encodeSegments(segs, ecl, minVersion?, maxVersion?, mask?, boostEcl?) | Mid-level API for fine-grained control over encoding. |
Properties
| Property | Type | Description |
| ---------------------- | -------- | -------------------------------------------------------------- |
| size | number | Width/height in modules (21–177). Equal to version * 4 + 17. |
| version | number | QR Code version (1–40). |
| mask | number | Mask pattern used (0–7). |
| errorCorrectionLevel | Ecc | The error correction level of this QR Code. |
Methods
| Method | Description |
| ----------------- | ----------------------------------------------------------------- |
| getModule(x, y) | Returns true if the module at (x, y) is dark, false if light. |
QrCode.Ecc — Error Correction Levels
| Level | Tolerance | Constant |
| -------- | --------- | --------------------- |
| Low | ~7% | QrCode.Ecc.LOW |
| Medium | ~15% | QrCode.Ecc.MEDIUM |
| Quartile | ~25% | QrCode.Ecc.QUARTILE |
| High | ~30% | QrCode.Ecc.HIGH |
Higher error correction increases QR Code size but improves scan reliability in damaged or obscured conditions.
toSvgString(qr, border?, lightColor?, darkColor?)
Converts a QrCode object to an SVG string.
| Parameter | Type | Default | Description |
| ------------ | -------- | ----------- | ------------------------------------ |
| qr | QrCode | — | The QR Code to render. |
| border | number | 4 | Quiet zone size in modules. |
| lightColor | string | "#FFFFFF" | Background color (CSS color string). |
| darkColor | string | "#000000" | Foreground color (CSS color string). |
const svg = toSvgString(qr, 4, "#FFFFFF", "#000000");QrSegment
Mid-level API for manually constructing encoding segments.
| Method | Description |
| ---------------------------------- | ------------------------------------------------------------- |
| QrSegment.makeBytes(data) | Encode raw bytes in byte mode. |
| QrSegment.makeNumeric(digits) | Encode a numeric string (digits 0–9 only). |
| QrSegment.makeAlphanumeric(text) | Encode uppercase alphanumeric text. |
| QrSegment.makeSegments(text) | Automatically choose the optimal segment mode for a string. |
| QrSegment.makeEci(assignVal) | Create an Extended Channel Interpretation designator segment. |
Development
# Install dependencies
npm install
# Build the library
npm run build
# Type-check tests
npm testOutput is emitted to dist/ with TypeScript declarations and source maps included.
License
MIT — Copyright (c) Ashraf Ashaffah.
This project is based on the QR Code generator library by Project Nayuki, also under the MIT License.
