Core barcode encoding/decoding library supporting 17 formats including QR Code, Data Matrix, Aztec, PDF 417, and various 1D barcodes
—
Complete support for 8 different 1D barcode formats including Code 39, Code 93, Code 128, Codabar, EAN-8, EAN-13, UPC-A, UPC-E, ITF (Interleaved 2 of 5), and RSS (GS1 DataBar) variants. Each format has dedicated reader and writer implementations with format-specific configuration options. 1D barcodes are linear symbologies widely used in retail, logistics, healthcare, and manufacturing.
Abstract base class for all 1D barcode readers. Provides common functionality for scanning horizontal rows and detecting 1D patterns.
/**
* Abstract base class for reading 1D barcodes.
* Implements common row scanning logic and pattern detection.
* Subclasses implement format-specific decoding.
*/
public abstract class OneDReader implements Reader {
/**
* Locates and decodes a 1D barcode in an image.
*
* @param image Binary bitmap representation of the image
* @return Result containing decoded text and metadata
* @throws NotFoundException If no barcode is found in the image
* @throws ChecksumException If barcode checksum validation fails
* @throws FormatException If barcode format rules are violated
*/
@Override
public Result decode(BinaryBitmap image) throws NotFoundException, ChecksumException, FormatException;
/**
* Locates and decodes a 1D barcode with configuration hints.
*
* @param image Binary bitmap representation of the image
* @param hints Map of DecodeHintType to configuration values
* @return Result containing decoded text and metadata
* @throws NotFoundException If no barcode is found in the image
* @throws ChecksumException If barcode checksum validation fails
* @throws FormatException If barcode format rules are violated
*/
@Override
public Result decode(BinaryBitmap image, Map<DecodeHintType,?> hints)
throws NotFoundException, ChecksumException, FormatException;
/**
* Resets any internal state.
*/
@Override
public void reset();
}Multi-format 1D barcode reader that attempts to decode using all supported 1D formats or a subset specified via hints.
/**
* Multi-format 1D barcode reader implementation.
* Attempts to decode using all 1D formats or formats specified in hints.
* Extends OneDReader to leverage common 1D scanning logic.
*/
public final class MultiFormatOneDReader extends OneDReader {
/**
* Creates a MultiFormatOneDReader with default settings.
* Will attempt all supported 1D formats.
*/
public MultiFormatOneDReader();
/**
* Creates a MultiFormatOneDReader with configuration hints.
*
* @param hints Map of DecodeHintType to configuration values
* - POSSIBLE_FORMATS: Limit to specific 1D formats
* - TRY_HARDER: Spend more time for better accuracy
*/
public MultiFormatOneDReader(Map<DecodeHintType,?> hints);
}Variable-length alphanumeric format supporting uppercase letters, digits, and limited special characters. Widely used in non-retail applications including automotive, defense, and healthcare.
/**
* Reader for Code 39 barcodes.
* Supports optional check digit and extended mode for full ASCII.
*/
public final class Code39Reader extends OneDReader {
/**
* Creates Code39Reader with default settings (no check digit, no extended mode).
*/
public Code39Reader();
/**
* Creates Code39Reader with check digit validation option.
*
* @param usingCheckDigit If true, validates and strips check digit
*/
public Code39Reader(boolean usingCheckDigit);
/**
* Creates Code39Reader with full configuration.
*
* @param usingCheckDigit If true, validates and strips check digit
* @param extendedMode If true, interprets extended Code 39 (full ASCII)
*/
public Code39Reader(boolean usingCheckDigit, boolean extendedMode);
}
/**
* Writer for Code 39 barcodes.
* Encodes uppercase letters, digits, and symbols: - . $ / + % SPACE
*/
public final class Code39Writer implements Writer {
/**
* Creates Code39Writer.
*/
public Code39Writer();
/**
* Encodes contents into Code 39 BitMatrix.
*
* @param contents String to encode (uppercase A-Z, 0-9, and - . $ / + % space)
* @param format Must be BarcodeFormat.CODE_39
* @param width Preferred width in pixels
* @param height Preferred height in pixels
* @return BitMatrix representation
* @throws IllegalArgumentException If contents contains invalid characters
*/
@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height);
}Usage Example:
// Reading Code 39
Reader reader = new Code39Reader(true, false); // With check digit
Result result = reader.decode(bitmap);
// Writing Code 39
Writer writer = new Code39Writer();
BitMatrix matrix = writer.encode("ABC-123", BarcodeFormat.CODE_39, 300, 100);Compact alphanumeric format more efficient than Code 39. Supports full ASCII character set through extended mode. Built-in error checking with two check digits.
/**
* Reader for Code 93 barcodes.
* Automatically validates built-in check digits.
*/
public final class Code93Reader extends OneDReader {
/**
* Creates Code93Reader.
*/
public Code93Reader();
}
/**
* Writer for Code 93 barcodes.
* Automatically adds required check digits.
*/
public final class Code93Writer implements Writer {
/**
* Creates Code93Writer.
*/
public Code93Writer();
}High-density variable-length format supporting full ASCII character set. Widely used in shipping and packaging. Excellent data efficiency with automatic mode switching.
/**
* Reader for Code 128 barcodes.
* Supports automatic mode switching between A, B, and C.
*/
public final class Code128Reader extends OneDReader {
/**
* Creates Code128Reader.
*/
public Code128Reader();
}
/**
* Writer for Code 128 barcodes.
* Automatically selects optimal encoding modes.
* Supports GS1-128 format via hints.
*/
public final class Code128Writer implements Writer {
/**
* Creates Code128Writer.
*/
public Code128Writer();
/**
* Encodes contents with hints support.
*
* @param hints Supports:
* - FORCE_CODE_SET (Integer): Force specific code set (A=0, B=1, C=2)
* - GS1_FORMAT (Boolean): Use GS1-128 format with FNC1
* - CODE128_COMPACT (Boolean): Use compact encoding
*/
@Override
public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
Map<EncodeHintType,?> hints);
}Variable-length numeric format with start/stop characters. Commonly used in libraries, blood banks, and package tracking. Supports digits and symbols: - $ : / . +
/**
* Reader for Codabar barcodes.
*/
public final class CodaBarReader extends OneDReader {
/**
* Creates CodaBarReader.
*/
public CodaBarReader();
}
/**
* Writer for Codabar barcodes.
* Requires start/stop characters (A, B, C, or D).
*/
public final class CodaBarWriter implements Writer {
/**
* Creates CodaBarWriter.
*/
public CodaBarWriter();
}Fixed 8-digit product barcode (International Article Number, shortened). Used for small product packages. Last digit is check digit.
/**
* Reader for EAN-8 barcodes.
* Automatically validates check digit.
*/
public final class EAN8Reader extends UPCEANReader {
/**
* Creates EAN8Reader.
*/
public EAN8Reader();
}
/**
* Writer for EAN-8 barcodes.
* Automatically calculates and appends check digit if not provided.
*/
public final class EAN8Writer implements Writer {
/**
* Creates EAN8Writer.
*/
public EAN8Writer();
}Fixed 13-digit product barcode (International Article Number, standard). Widely used for retail products worldwide. Last digit is check digit.
/**
* Reader for EAN-13 barcodes.
* Automatically validates check digit.
*/
public final class EAN13Reader extends UPCEANReader {
/**
* Creates EAN13Reader.
*/
public EAN13Reader();
}
/**
* Writer for EAN-13 barcodes.
* Automatically calculates and appends check digit if not provided.
*/
public final class EAN13Writer implements Writer {
/**
* Creates EAN13Writer.
*/
public EAN13Writer();
}Fixed 12-digit product barcode (Universal Product Code, standard). Primary barcode format for retail in North America. Last digit is check digit.
/**
* Reader for UPC-A barcodes.
* Actually wraps EAN13Reader since UPC-A is a subset of EAN-13.
*/
public final class UPCAReader implements Reader {
/**
* Creates UPCAReader.
*/
public UPCAReader();
}
/**
* Writer for UPC-A barcodes.
* Automatically calculates and appends check digit if not provided.
*/
public final class UPCAWriter implements Writer {
/**
* Creates UPCAWriter.
*/
public UPCAWriter();
}Compressed 6-digit version of UPC-A (zero-suppressed format). Used for small product packages. Expands to full UPC-A when decoded.
/**
* Reader for UPC-E barcodes.
* Automatically expands to full UPC-A format.
*/
public final class UPCEReader extends UPCEANReader {
/**
* Creates UPCEReader.
*/
public UPCEReader();
}
/**
* Writer for UPC-E barcodes.
* Automatically compresses from UPC-A format.
*/
public final class UPCEWriter implements Writer {
/**
* Creates UPCEWriter.
*/
public UPCEWriter();
}Numeric-only format requiring even number of digits. Used in warehouse and distribution. High density for numeric data, interleaves pairs of digits.
/**
* Reader for ITF barcodes.
* Decodes interleaved digit pairs.
*/
public final class ITFReader extends OneDReader {
/**
* Creates ITFReader.
*/
public ITFReader();
}
/**
* Writer for ITF barcodes.
* Requires even number of digits.
*/
public final class ITFWriter implements Writer {
/**
* Creates ITFWriter.
*/
public ITFWriter();
}GS1 standard formats for small items and supplementary data. More compact than traditional UPC/EAN. Supports additional information like expiration dates and batch numbers.
/**
* Reader for RSS-14 (GS1 DataBar) barcodes.
* Decodes 14-digit GTIN values.
*/
public final class RSS14Reader extends AbstractRSSReader {
/**
* Creates RSS14Reader.
*/
public RSS14Reader();
}
/**
* Reader for RSS Expanded (GS1 DataBar Expanded) barcodes.
* Supports variable-length data with Application Identifiers.
*/
public final class RSSExpandedReader extends AbstractRSSReader {
/**
* Creates RSSExpandedReader.
*/
public RSSExpandedReader();
}| Format | Type | Char Set | Check Digit | Use Case |
|---|---|---|---|---|
| Code 39 | Variable | Alphanumeric | Optional | Automotive, Defense, Healthcare |
| Code 93 | Variable | Full ASCII | Built-in (2) | Logistics, Canadian Post |
| Code 128 | Variable | Full ASCII | Built-in | Shipping, Packaging, GS1-128 |
| Codabar | Variable | Numeric + Symbols | Optional | Libraries, Blood Banks |
| EAN-8 | Fixed (8) | Numeric | Required | Small Products |
| EAN-13 | Fixed (13) | Numeric | Required | Retail Products (International) |
| UPC-A | Fixed (12) | Numeric | Required | Retail Products (North America) |
| UPC-E | Fixed (6/8) | Numeric | Required | Small Products |
| ITF | Variable (even) | Numeric | Optional | Warehouse, Cartons |
| RSS-14 | Fixed (14) | Numeric (GTIN) | Built-in | Small Items, Coupons |
| RSS Expanded | Variable | Alphanumeric | Built-in | Grocery, Healthcare |
Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
// Limit to specific 1D formats
hints.put(DecodeHintType.POSSIBLE_FORMATS,
Arrays.asList(BarcodeFormat.CODE_128, BarcodeFormat.EAN_13));
// Enable more thorough scanning
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
// Assume Code 39 has check digit
hints.put(DecodeHintType.ASSUME_CODE_39_CHECK_DIGIT, Boolean.TRUE);
// Return Codabar start/stop characters
hints.put(DecodeHintType.RETURN_CODABAR_START_END, Boolean.TRUE);
// Assume GS1 format (Code 128)
hints.put(DecodeHintType.ASSUME_GS1, Boolean.TRUE);
// Allowed EAN/UPC extension lengths
hints.put(DecodeHintType.ALLOWED_EAN_EXTENSIONS, new int[]{2, 5});Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
// Set margin/quiet zone
hints.put(EncodeHintType.MARGIN, 10);
// Force specific Code 128 code set
hints.put(EncodeHintType.FORCE_CODE_SET, 1); // Force Code Set B
// Enable GS1-128 format
hints.put(EncodeHintType.GS1_FORMAT, Boolean.TRUE);
// Enable compact Code 128
hints.put(EncodeHintType.CODE128_COMPACT, Boolean.TRUE);Format Selection:
Quality Requirements:
Reading Tips:
Encoding Best Practices: