barcode - barcode / QR code generation
Import with import "barcode.j" as barcode;. Generates scannable codes as images; label emits printer-native barcode commands instead. encode(symbology, data, opts) builds a device-independent Symbol (a module matrix for 2D, bar widths for 1D), and the renderers turn it into SVG, PNG, terminal art, or the raw matrix. Pure .j over compress (zlib)
crc(CRC-32) +encoding+ the bitwise operators, with no image library; runs on both binaries.
import "barcode.j" as barcode;
def opts as barcode.Options init barcode.defaults();
def qr as barcode.Symbol init barcode.encode("qr", "https://example.com", $opts);
def svg as string init barcode.svg($qr, $opts); # embed in HTML / email
def png as bytes init barcode.png($qr, $opts); # a monochrome PNGRunnable: examples/modules/barcode_demo.j.
Encoding
barcode.encode(symbology, data, opts) returns a Symbol. Symbologies:
| Symbology | Kind | Notes |
|---|---|---|
qr | 2D | Reed-Solomon over GF(256), EC levels L/M/Q/H (opts.ecLevel), automatic version selection 1-40, data-mask scoring; numeric / alphanumeric / byte mode chosen for compactness |
datamatrix | 2D | ECC200, square symbols 10x10 to 26x26, ASCII encodation (digit-pair packing), Reed-Solomon over GF(0x12d) |
code128 | 1D | Code set B (ASCII 32-126), auto checksum |
code93 | 1D | uppercase + digits + -. $/+%, two check characters (C, K); lowercase is encoded uppercased |
code39 | 1D | uppercase + digits + -. $/+%, * start/stop (not allowed in the data); lowercase is encoded uppercased |
ean13 | 1D | 12 or 13 digits (check digit computed if omitted) |
ean8 | 1D | 7 or 8 digits (check digit computed if omitted) |
upca | 1D | 11 or 12 digits (UPC-A; an EAN-13 with a leading 0) |
upce | 1D | 6 / 7 / 8 digits (UPC-E, zero-compressed) |
itf | 1D | Interleaved 2 of 5, even digit count |
gs1-128 | 1D | Code 128 with a leading FNC1 and parenthesised Application Identifiers, e.g. (01)09501101020917(10)ABC123 (see below) |
Symbol field | Type | Meaning |
|---|---|---|
kind | SymbolKind | SymbolKind.Matrix (2D) or SymbolKind.Linear (1D) |
size | int | matrix dimension (2D; 0 for 1D) |
matrix | list of list of bool | the 2D module grid (true = dark) |
bars | list of int | 1D bar / space run widths, starting with a bar |
text | string | the human-readable data (see below) |
text is the data as passed, except that code39 / code93 hold it uppercased and the EAN / UPC symbologies hold the full number with its check digit (ean13 given 12 digits shows 13, ean8 8, upca 12, upce the 8 digits number system + 6 + check). It is the line svg prints under a 1D symbol.
Every 1D symbology refuses empty data (a symbol of only start / stop patterns does not scan); a 2D symbol of empty data is valid. An unknown symbology, an opts.ecLevel other than "L", "M", "Q" or "H" (for qr) and invalid data throw kind "barcode".
GS1-128 element strings are validated: each AI is 2-4 digits, each value is 1-90 characters of GS1 character set 82 (digits, letters and !"%&'()*+,-./:;<=>?_; a value cannot contain (, which starts the next AI). The predefined-length AIs (00-04, 11-20, 31nn-36nn, 41x) need their fixed number of digits and AI length, and SSCC (00), GTIN (01, 02) and GLN (41x) values must end in a valid check digit. A FNC1 separates a variable-length value from the next AI; a predefined-length one needs none.
Rendering
Options field | Type | Default | Meaning |
|---|---|---|---|
scale | int | 8 | pixels (PNG) or units (SVG) per module / narrow bar, 1-100 |
height | int | 40 | bar height of a 1D code in modules, 1-1000 |
quiet | int | 4 | the light border (quiet zone) in modules, 0-16 |
ecLevel | string | "M" | QR error-correction level: "L", "M", "Q" or "H" |
foreground | string | "#000000" | the dark SVG colour (a PNG is always black) |
background | string | "#ffffff" | the light SVG colour (a PNG is always white) |
humanReadable | bool | true | print the data as a text line under a 1D SVG |
barcode.defaults() returns these defaults. Scale 8 keeps a code scannable by a phone camera off a screen; a smaller render can defeat autofocus or pick up screen moire. Lower opts.scale for a more compact image.
| Call | Returns | |
|---|---|---|
barcode.svg(symbol, opts) | string | resolution-independent SVG (embeds in HTML / email) |
barcode.png(symbol, opts) | bytes | a monochrome (grayscale) PNG, hand-encoded over compress + crc |
barcode.terminal(symbol) | string | Unicode half-block art for the CLI / REPL (2D only), wrapped in a 4-module quiet zone so it stays camera-scannable |
barcode.matrix(symbol) | list of list of bool | the raw 2D cells (e.g. to feed label.image) |
A scale, quiet or height outside its range is an error (kind "barcode") when the symbol is rendered. A colour containing ", <, >, & or a space is an error too. A PNG over 67,108,864 pixels (8192 x 8192) is a "limit" error, raised before any row is built.
Verification
The overlay (modules/barcode_test.j) checks the Reed-Solomon against the canonical QR vector, the format / version BCH against known values, mode codewords (including the QR spec's numeric worked example), the DataMatrix 10x10 matrix against zint, and 1D bar patterns against independent reference encoders; and the Go suite (cmd/jennifer/barcode_test.go) renders PNGs, decodes them with Go's standard library (proving the hand-rolled PNG is byte-correct), and, where zbarimg is available, optically scans them to confirm they read.
Scope
- QR versions 1-40, all four EC levels, with numeric / alphanumeric / byte mode chosen for compactness. A byte-mode payload with non-ASCII bytes gets an ECI(26) UTF-8 declaration, so it decodes as UTF-8 in a strict reader. Kanji mode, other ECI assignments, and structured-append are not covered. Large versions (v30+) are slow to render (the mask-penalty scoring runs in the tree-walking interpreter), not incorrect.
- DataMatrix ECC200 square symbols 10x10 to 26x26 (single data region), ASCII encodation. Rectangular symbols, larger sizes (region interleaving), and the C40 / Text / X12 / EDIFACT / Base256 encodations are not supported; a letter-heavy payload is valid but larger than an optimal-encodation encoder would produce.
- The GF(256) / Reed-Solomon math lives in a private
barcode_ecc.inc.j(included, parameterised by primitive polynomial and generator base, so QR (0x11d) and DataMatrix (0x12d) share it). - No general image library: the only raster need is a monochrome bitmap, which the PNG encoder covers directly.
- Aztec / PDF417 / MaxiCode are not included.
See also
- label.md - printer-native barcode commands (the other half).
- compress.md / crc.md - the PNG encoder's zlib and CRC-32.
- modules/index.md - the module catalog and import rules.
Academy talks
| Talk | What it shows |
|---|---|
| Crypto, tokens and certificates with Jennifer | a QR code for TOTP enrolment |
| Images, barcodes and labels with Jennifer | QR, Code 128, EAN-13, GS1-128 and DataMatrix symbols |
API reference
Every exported function, struct and constant of barcode, with its signature, generated from the module's doc comments: the barcode API reference.