Skip to content
Jennifer Programming Language

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.
jennifer
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 PNG

Runnable: examples/modules/barcode_demo.j.

Encoding

barcode.encode(symbology, data, opts) returns a Symbol. Symbologies:

SymbologyKindNotes
qr2DReed-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
datamatrix2DECC200, square symbols 10x10 to 26x26, ASCII encodation (digit-pair packing), Reed-Solomon over GF(0x12d)
code1281DCode set B (ASCII 32-126), auto checksum
code931Duppercase + digits + -. $/+%, two check characters (C, K); lowercase is encoded uppercased
code391Duppercase + digits + -. $/+%, * start/stop (not allowed in the data); lowercase is encoded uppercased
ean131D12 or 13 digits (check digit computed if omitted)
ean81D7 or 8 digits (check digit computed if omitted)
upca1D11 or 12 digits (UPC-A; an EAN-13 with a leading 0)
upce1D6 / 7 / 8 digits (UPC-E, zero-compressed)
itf1DInterleaved 2 of 5, even digit count
gs1-1281DCode 128 with a leading FNC1 and parenthesised Application Identifiers, e.g. (01)09501101020917(10)ABC123 (see below)
Symbol fieldTypeMeaning
kindSymbolKindSymbolKind.Matrix (2D) or SymbolKind.Linear (1D)
sizeintmatrix dimension (2D; 0 for 1D)
matrixlist of list of boolthe 2D module grid (true = dark)
barslist of int1D bar / space run widths, starting with a bar
textstringthe 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 fieldTypeDefaultMeaning
scaleint8pixels (PNG) or units (SVG) per module / narrow bar, 1-100
heightint40bar height of a 1D code in modules, 1-1000
quietint4the light border (quiet zone) in modules, 0-16
ecLevelstring"M"QR error-correction level: "L", "M", "Q" or "H"
foregroundstring"#000000"the dark SVG colour (a PNG is always black)
backgroundstring"#ffffff"the light SVG colour (a PNG is always white)
humanReadablebooltrueprint 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.

CallReturns
barcode.svg(symbol, opts)stringresolution-independent SVG (embeds in HTML / email)
barcode.png(symbol, opts)bytesa monochrome (grayscale) PNG, hand-encoded over compress + crc
barcode.terminal(symbol)stringUnicode 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 boolthe 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

Academy talks

TalkWhat it shows
Crypto, tokens and certificates with Jennifera QR code for TOTP enrolment
Images, barcodes and labels with JenniferQR, 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.