Ean13Encoder

Pure-Kotlin EAN-13 / UPC-A barcode encoder.

Encodes a 12- or 13-digit numeric string into the 95-module bar/space pattern of an EAN-13 symbol. The output is a Code128Barcode so the existing 1D rendering path (alternating bar/space module widths, starting with a bar) can draw it unchanged.

Check digit. A 12-digit input is treated as the first twelve digits and the mod-10 check digit is computed and appended. A 13-digit input is treated as a complete symbol and its trailing check digit is verified; a mismatch is rejected.

UPC-A. UPC-A is the 12-digit subset of EAN-13: a 12-digit UPC-A code is the same symbol as the EAN-13 code formed by prefixing a leading 0. Callers wanting UPC-A pass the 12 UPC digits to encodeUpcA, which prepends the 0 and encodes the resulting EAN-13.

Structure. An EAN-13 symbol is exactly 95 modules: a 3-module start guard (bar-space-bar), six left-hand digits (7 modules each), a 5-module centre guard (space-bar-space-bar-space), six right-hand digits (7 modules each), and a 3-module end guard (bar-space-bar). The thirteenth (leading) digit is not drawn as bars — it is encoded implicitly by the L/G parity pattern chosen for the six left-hand digits.

Properties

Link copied to clipboard
const val TOTAL_MODULES: Int = 95

Modules in a complete EAN-13 symbol: 3 + 6·7 + 5 + 6·7 + 3.

Functions

Link copied to clipboard

Encodes a 12- or 13-digit EAN-13 payload.

Link copied to clipboard

Encodes a 12-digit UPC-A payload as the equivalent EAN-13 symbol.