barcode API reference
Generate scannable barcodes as images (not printer commands - the complement to label, which emits printer-native barcode commands). encode(symbology, data, 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.
Contents
| Name | What it is |
|---|---|
defaults | Sensible default rendering options (scale 8, quiet 4, EC level M, black on white). |
encode | Encode data as a symbol of the given symbology. |
matrix | The raw cells of a 2D symbol. |
png | Render a symbol as a monochrome (grayscale) PNG. |
svg | Render a symbol as an SVG string. |
terminal | Render a symbol as Unicode half-block art for a terminal (2D only). |
Options | struct: Rendering options. |
Symbol | struct: A device-independent encoded symbol. |
SymbolKind | enum: The kind of an encoded symbol: Matrix (a 2D module grid, e.g. |
Functions
defaults
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
encode
barcode.encode(symbology as string, data 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".
symbology- the symbology
data- the payload
opts- rendering / EC options (uses
ecLevelfor QR) - Returns
Symbol - the encoded symbol
- Throws
Error - kind "barcode" on an unknown symbology, an unknown QR EC level, empty 1D data, or invalid data
matrix
barcode.matrix(symbol as Symbol)The raw cells of a 2D symbol.
symbol- a matrix symbol
- Returns
list of list of bool - the module grid (true = dark)
png
barcode.png(symbol as Symbol, opts as Options)Render a symbol as a monochrome (grayscale) PNG.
symbol- the symbol
opts- scale / quiet / height
- Returns
bytes - the PNG image
svg
barcode.svg(symbol as Symbol, opts as Options)Render a symbol as an SVG string.
symbol- the symbol
opts- scale / quiet / height / colours
- Returns
string - the SVG document
terminal
barcode.terminal(symbol as Symbol)Render a symbol as Unicode half-block art for a terminal (2D only).
symbol- a matrix symbol
- Returns
string - the terminal rendering
Structs
Options
Rendering options.
def struct Options {
scale as int,
height as int,
quiet as int,
ecLevel as string,
foreground as string,
background as string,
humanReadable as bool
};scale- pixels (PNG) or units (SVG) per module / narrow bar
height- bar height for 1D codes (module units)
quiet- quiet-zone width in modules / narrow bars
ecLevel- QR error-correction level: "L", "M", "Q", or "H"
foreground- the dark colour (SVG only; PNG is always black), e.g. "#000000"
background- the light colour (SVG only; PNG is always white), e.g. "#ffffff"
humanReadable- render the data as a text line under a 1D barcode (SVG only; on by default)
Symbol
A device-independent encoded symbol.
def struct Symbol {
kind as SymbolKind,
size as int,
matrix as list of list of bool,
bars as list of int,
text as string
};kindMatrix(2D) orLinear(1D)size- the matrix dimension (2D; 0 for 1D)
matrix- the 2D module grid (true = dark)
bars- 1D bar/space run widths, starting with a bar
text- the human-readable data: as passed, except that Code 39 / Code 93 are uppercased and EAN / UPC carry their full number with the check digit
Enums
SymbolKind
The kind of an encoded symbol: Matrix (a 2D module grid, e.g. QR) or Linear (a 1D run of bar / space widths).