barcode API reference
Generate scannable barcodes as images (not printer commands - the complement to label, which emits printer-native barcode commands). encode(data, symbology, opts) -> Symbol builds a device-independent representation - a black/white module matrix for 2D codes, a run of bar widths for 1D codes - and the renderers turn a Symbol into output: svg (resolution-independent, embeds in HTML / email), png (a monochrome PNG hand-encoded over compress + crc, no image library), terminal (Unicode half-block art), and matrix (the raw cells).
Symbologies: 2D qr (Reed-Solomon over GF(256), EC levels L/M/Q/H, automatic version selection 1-40, data-mask scoring, and numeric / alphanumeric / byte mode chosen for compactness) and datamatrix (ECC200, square symbols 10x10 to 26x26); 1D code128, code93, ean13, ean8, upca, upce, itf, code39, and gs1-128 (FNC1 + Application Identifiers). A 1D SVG also carries a human-readable text line (Options.humanReadable). Pure .j over compress (zlib) + crc (CRC-32) + encoding + the bitwise operators; runs on both binaries.
Import with import "barcode.j" as barcode;. See the barcode guide for prose and examples.
Functions
barcode.defaults()
Sensible default rendering options (scale 8, quiet 4, EC level M, black on white). Scale 8 (8 px per module / narrow bar) keeps a code comfortably scannable off a screen by a phone camera, where a smaller render invites focus and moire trouble; drop opts.scale for a more compact image.
Returns {Options} - the defaults
barcode.encode(data as string, symbology as string, opts as Options)
Encode data as a symbol of the given symbology. 2D: "qr", "datamatrix". 1D: "code128", "code93", "ean13", "ean8", "upca", "upce", "itf", "code39", "gs1-128".
Parameters
data{string}- the payloadsymbology{string}- the symbologyopts{Options}- rendering / EC options (usesecLevelfor QR)
Returns {Symbol} - the encoded symbol
Throws
{Error}- kind "barcode" on an unknown symbology or invalid data
barcode.matrix(symbol as Symbol)
The raw cells of a 2D symbol.
Parameters
symbol{Symbol}- a matrix symbol
Returns {list of list of bool} - the module grid (true = dark)
barcode.png(symbol as Symbol, opts as Options)
Render a symbol as a monochrome (grayscale) PNG.
Parameters
symbol{Symbol}- the symbolopts{Options}- scale / quiet / height
Returns {bytes} - the PNG image
barcode.svg(symbol as Symbol, opts as Options)
Render a symbol as an SVG string.
Parameters
symbol{Symbol}- the symbolopts{Options}- scale / quiet / height / colours
Returns {string} - the SVG document
barcode.terminal(symbol as Symbol)
Render a symbol as Unicode half-block art for a terminal (2D only).
Parameters
symbol{Symbol}- a matrix symbol
Returns {string} - the terminal rendering
Structs
barcode.Options
Rendering options.
| Field | Type | Description |
|---|---|---|
scale | int | pixels (PNG) or units (SVG) per module / narrow bar |
height | int | bar height for 1D codes (module units) |
quiet | int | quiet-zone width in modules / narrow bars |
ecLevel | string | QR error-correction level: "L", "M", "Q", or "H" |
foreground | string | the dark colour (SVG only; PNG is always black), e.g. "#000000" |
background | string | the light colour (SVG only; PNG is always white), e.g. "#ffffff" |
humanReadable | bool | render the data as a text line under a 1D barcode (SVG only; on by default) |
barcode.Symbol
A device-independent encoded symbol.
| Field | Type | Description |
|---|---|---|
kind | SymbolKind | Matrix (2D) or Linear (1D) |
size | int | the 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 encoded data |
Enums
barcode.SymbolKind
The kind of an encoded symbol: Matrix (a 2D module grid, e.g. QR) or Linear (a 1D run of bar / space widths).