CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-google-zxing--core

Core barcode encoding/decoding library supporting 17 formats including QR Code, Data Matrix, Aztec, PDF 417, and various 1D barcodes

Pending
Overview
Eval results
Files

pdf417.mddocs/reference/

PDF417 Support

Complete PDF417 2D barcode encoding and decoding support with configurable compaction modes (auto, text, byte, numeric), dimension constraints, and error correction levels. PDF417 is a stacked linear 2D symbology widely used in identification documents, transportation, inventory management, and applications requiring high data capacity (up to 1,850 alphanumeric characters).

Capabilities

PDF417Reader

Reader implementation for detecting and decoding PDF417 barcodes from binary bitmap images. Supports both single barcode detection and multiple barcode detection from a single image.

/**
 * Reader implementation for PDF417 2D barcodes.
 * Locates and decodes PDF417 codes in images.
 * Also implements MultipleBarcodeReader for detecting multiple codes.
 */
public final class PDF417Reader implements Reader, MultipleBarcodeReader {
    /**
     * Creates a new PDF417 reader.
     */
    public PDF417Reader();

    /**
     * Locates and decodes a PDF417 code in an image.
     *
     * @param image Binary bitmap representation of the image
     * @return Result containing decoded text and metadata
     * @throws NotFoundException If a PDF417 code cannot be found in the image
     * @throws ChecksumException If error correction fails
     * @throws FormatException If a PDF417 code cannot be decoded
     */
    @Override
    public Result decode(BinaryBitmap image) throws NotFoundException, ChecksumException, FormatException;

    /**
     * Locates and decodes a PDF417 code in an image with configuration hints.
     *
     * @param image Binary bitmap representation of the image
     * @param hints Map of DecodeHintType to configuration values
     *              - PURE_BARCODE (Boolean): Use fast pure barcode extraction
     *              - CHARACTER_SET (String): Expected character encoding
     *              - TRY_HARDER (Boolean): Spend more time for better accuracy
     * @return Result containing decoded text and metadata
     * @throws NotFoundException If a PDF417 code cannot be found in the image
     * @throws ChecksumException If error correction fails
     * @throws FormatException If a PDF417 code cannot be decoded
     */
    @Override
    public Result decode(BinaryBitmap image, Map<DecodeHintType,?> hints)
        throws NotFoundException, ChecksumException, FormatException;

    /**
     * Locates and decodes multiple PDF417 codes in an image.
     *
     * @param image Binary bitmap representation of the image
     * @return Array of Result objects, one for each detected barcode
     * @throws NotFoundException If no PDF417 codes are found in the image
     */
    @Override
    public Result[] decodeMultiple(BinaryBitmap image) throws NotFoundException;

    /**
     * Locates and decodes multiple PDF417 codes in an image with configuration hints.
     *
     * @param image Binary bitmap representation of the image
     * @param hints Map of DecodeHintType to configuration values
     * @return Array of Result objects, one for each detected barcode
     * @throws NotFoundException If no PDF417 codes are found in the image
     */
    @Override
    public Result[] decodeMultiple(BinaryBitmap image, Map<DecodeHintType,?> hints) throws NotFoundException;

    /**
     * Resets any internal state. Should be called between decoding attempts.
     */
    @Override
    public void reset();
}

Usage Example:

import com.google.zxing.*;
import com.google.zxing.common.*;
import com.google.zxing.pdf417.*;
import java.util.*;

// Create luminance source from image pixels
int[] pixels = ...; // RGB int array
LuminanceSource source = new RGBLuminanceSource(width, height, pixels);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

// Decode single PDF417 code
Reader reader = new PDF417Reader();
Result result = reader.decode(bitmap);

// Access decoded data
String text = result.getText();
byte[] rawBytes = result.getRawBytes();
ResultPoint[] corners = result.getResultPoints();

// Access metadata
Map<ResultMetadataType, Object> metadata = result.getResultMetadata();
String ecLevel = (String) metadata.get(ResultMetadataType.ERROR_CORRECTION_LEVEL);
Integer errorsCorrected = (Integer) metadata.get(ResultMetadataType.ERRORS_CORRECTED);
Integer erasuresCorrected = (Integer) metadata.get(ResultMetadataType.ERASURES_CORRECTED);

// Decode multiple PDF417 codes from single image
Result[] results = ((PDF417Reader) reader).decodeMultiple(bitmap);
for (Result r : results) {
    System.out.println("Found: " + r.getText());
}

PDF417Writer

Writer implementation for encoding PDF417 barcodes. Generates a BitMatrix representation that can be rendered to an image with configurable compaction modes, dimensions, and error correction levels.

/**
 * Writer implementation for PDF417 2D barcodes.
 * Encodes text into PDF417 BitMatrix with configurable compaction mode,
 * dimensions, and error correction level.
 */
public final class PDF417Writer implements Writer {
    /**
     * Creates a new PDF417 writer.
     */
    public PDF417Writer();

    /**
     * Encodes contents into a PDF417 BitMatrix with default settings.
     * Uses error correction level 2 and 30-pixel margin.
     *
     * @param contents String content to encode
     * @param format Must be BarcodeFormat.PDF_417
     * @param width Preferred width in pixels
     * @param height Preferred height in pixels
     * @return BitMatrix representation of the PDF417 code
     * @throws WriterException If encoding fails
     * @throws IllegalArgumentException If format is not PDF_417
     */
    @Override
    public BitMatrix encode(String contents, BarcodeFormat format, int width, int height)
        throws WriterException;

    /**
     * Encodes contents into a PDF417 BitMatrix with configuration hints.
     *
     * @param contents String content to encode
     * @param format Must be BarcodeFormat.PDF_417
     * @param width Preferred width in pixels
     * @param height Preferred height in pixels
     * @param hints Map of EncodeHintType to configuration values
     *              - PDF417_COMPACT (Boolean): Use compact variant (no right row indicators)
     *              - PDF417_COMPACTION (Compaction): AUTO, TEXT, BYTE, or NUMERIC
     *              - PDF417_DIMENSIONS (Dimensions): Min/max rows and columns constraints
     *              - MARGIN (Integer): Margin size in pixels (default: 30)
     *              - ERROR_CORRECTION (Integer): Error correction level 0-8 (default: 2)
     *              - CHARACTER_SET (String): Character encoding (e.g., "UTF-8")
     *              - PDF417_AUTO_ECI (Boolean): Automatically insert ECI segments
     * @return BitMatrix representation of the PDF417 code
     * @throws WriterException If encoding fails
     * @throws IllegalArgumentException If parameters are invalid
     */
    @Override
    public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
                          Map<EncodeHintType,?> hints) throws WriterException;
}

Usage Example:

import com.google.zxing.*;
import com.google.zxing.common.*;
import com.google.zxing.pdf417.*;
import com.google.zxing.pdf417.encoder.*;
import java.util.*;

// Encode with default settings
Writer writer = new PDF417Writer();
BitMatrix matrix = writer.encode(
    "https://example.com/product/12345",
    BarcodeFormat.PDF_417,
    400,
    200
);

// Encode with text compaction for better efficiency
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.PDF417_COMPACTION, Compaction.TEXT);
hints.put(EncodeHintType.ERROR_CORRECTION, 4); // Higher error correction
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

BitMatrix matrix2 = writer.encode(
    "PRODUCT INFORMATION HERE",
    BarcodeFormat.PDF_417,
    500,
    150,
    hints
);

// Encode with dimension constraints
hints.clear();
hints.put(EncodeHintType.PDF417_DIMENSIONS, new Dimensions(4, 8, 10, 20));
// Min 4 columns, max 8 columns, min 10 rows, max 20 rows

BitMatrix matrix3 = writer.encode(
    "Constrained barcode data",
    BarcodeFormat.PDF_417,
    400,
    200,
    hints
);

// Encode compact PDF417 (smaller, no right indicators)
hints.clear();
hints.put(EncodeHintType.PDF417_COMPACT, Boolean.TRUE);
hints.put(EncodeHintType.MARGIN, 10); // Smaller margin

BitMatrix compact = writer.encode(
    "Compact format",
    BarcodeFormat.PDF_417,
    300,
    100,
    hints
);

// Convert BitMatrix to image pixels
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        pixels[y * width + x] = matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF;
    }
}

Compaction

Enumeration for PDF417 compaction modes defining how data is encoded. Different modes optimize for different character sets, providing better compression for specific data types.

/**
 * Enumeration for PDF417 compaction (encoding) modes.
 * Each mode provides optimal encoding for different data types.
 */
public enum Compaction {
    /**
     * Automatic mode selection based on input data.
     * The encoder analyzes content and switches between modes
     * dynamically for optimal compression. Recommended for most use cases.
     */
    AUTO,

    /**
     * Text compaction mode.
     * Optimized for ASCII text, uppercase/lowercase letters, and common punctuation.
     * Encodes approximately 2 characters per codeword.
     * Best for: Text documents, addresses, names, descriptions.
     */
    TEXT,

    /**
     * Byte compaction mode.
     * Encodes raw 8-bit bytes without interpretation.
     * Encodes approximately 1.2 bytes per codeword.
     * Best for: Binary data, arbitrary bytes, mixed character sets.
     */
    BYTE,

    /**
     * Numeric compaction mode.
     * Optimized for sequences of decimal digits.
     * Encodes approximately 2.9 digits per codeword.
     * Best for: Numbers, serial numbers, product codes, quantities.
     */
    NUMERIC
}

Usage Example:

import com.google.zxing.*;
import com.google.zxing.pdf417.encoder.Compaction;
import java.util.*;

Writer writer = new PDF417Writer();
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);

// Automatic mode (recommended)
hints.put(EncodeHintType.PDF417_COMPACTION, Compaction.AUTO);
BitMatrix auto = writer.encode("Mixed123Data!", BarcodeFormat.PDF_417, 400, 200, hints);

// Text mode for text-heavy content
hints.put(EncodeHintType.PDF417_COMPACTION, Compaction.TEXT);
BitMatrix text = writer.encode("SHIPPING INFORMATION", BarcodeFormat.PDF_417, 400, 200, hints);

// Numeric mode for numbers
hints.put(EncodeHintType.PDF417_COMPACTION, Compaction.NUMERIC);
BitMatrix numeric = writer.encode("1234567890", BarcodeFormat.PDF_417, 400, 200, hints);

// Byte mode for binary or mixed data
hints.put(EncodeHintType.PDF417_COMPACTION, Compaction.BYTE);
BitMatrix bytes = writer.encode("Binary\u0000Data", BarcodeFormat.PDF_417, 400, 200, hints);

Dimensions

Data object specifying minimum and maximum row and column constraints for PDF417 barcodes. Used to control the aspect ratio and overall size of the generated barcode.

/**
 * Dimensions constraint object for PDF417 barcodes.
 * Specifies minimum and maximum number of rows and columns
 * to control barcode shape and aspect ratio.
 */
public final class Dimensions {
    /**
     * Creates dimension constraints for PDF417 encoding.
     *
     * @param minCols Minimum number of data columns (1-30)
     * @param maxCols Maximum number of data columns (1-30)
     * @param minRows Minimum number of rows (3-90)
     * @param maxRows Maximum number of rows (3-90)
     * @throws IllegalArgumentException If constraints are invalid or impossible
     */
    public Dimensions(int minCols, int maxCols, int minRows, int maxRows);

    /**
     * Gets the minimum number of data columns.
     *
     * @return Minimum columns
     */
    public int getMinCols();

    /**
     * Gets the maximum number of data columns.
     *
     * @return Maximum columns
     */
    public int getMaxCols();

    /**
     * Gets the minimum number of rows.
     *
     * @return Minimum rows
     */
    public int getMinRows();

    /**
     * Gets the maximum number of rows.
     *
     * @return Maximum rows
     */
    public int getMaxRows();
}

Usage Example:

import com.google.zxing.*;
import com.google.zxing.pdf417.encoder.Dimensions;
import java.util.*;

Writer writer = new PDF417Writer();
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);

// Create wide, short barcode (minimum 10 cols, maximum 20 cols, 3-10 rows)
Dimensions wideShort = new Dimensions(10, 20, 3, 10);
hints.put(EncodeHintType.PDF417_DIMENSIONS, wideShort);
BitMatrix wide = writer.encode("Data for wide barcode", BarcodeFormat.PDF_417, 600, 100, hints);

// Create narrow, tall barcode (minimum 3 cols, maximum 6 cols, 10-30 rows)
Dimensions narrowTall = new Dimensions(3, 6, 10, 30);
hints.put(EncodeHintType.PDF417_DIMENSIONS, narrowTall);
BitMatrix tall = writer.encode("Data for tall barcode", BarcodeFormat.PDF_417, 200, 400, hints);

// Create square-ish barcode (8-12 cols, 8-12 rows)
Dimensions square = new Dimensions(8, 12, 8, 12);
hints.put(EncodeHintType.PDF417_DIMENSIONS, square);
BitMatrix sqr = writer.encode("Square barcode data", BarcodeFormat.PDF_417, 400, 400, hints);

// Fixed size barcode (exactly 6 columns, 15-20 rows)
Dimensions fixed = new Dimensions(6, 6, 15, 20);
hints.put(EncodeHintType.PDF417_DIMENSIONS, fixed);
BitMatrix fixedWidth = writer.encode("Fixed width barcode", BarcodeFormat.PDF_417, 300, 300, hints);

PDF417 Structure

Symbol Organization

PDF417 is a stacked linear barcode consisting of:

  • Rows: 3 to 90 rows of stacked codewords
  • Columns: 1 to 30 data columns per row
  • Codewords: 17-module wide patterns (4 bars, 4 spaces)
  • Start Pattern: Left-side start delimiter
  • Stop Pattern: Right-side stop delimiter
  • Row Indicators: Left and right indicators containing row number and error correction info

Capacity

Maximum data capacity varies by error correction level:

EC LevelNumericAlphanumericBinary% Overhead
0271018501108~5%
1261017851067~10%
2247016851009~18% (default)
323201585948~27%
420001365817~39%
515201040622~56%
61000685410~75%
7560385230~112%
816011065~195%

Recommended levels:

  • Level 2 (default): Good balance for most applications
  • Level 3-4: High reliability applications (documents, tickets)
  • Level 5-6: Maximum reliability (outdoor, damaged items)

Compaction Mode Details

Text Compaction

  • Sub-modes: Alpha, Lower, Mixed, Punctuation
  • Efficiency: ~2 characters per codeword
  • Character set: Full ASCII range via mode switching
  • Best for: Names, addresses, general text

Byte Compaction

  • Encoding: 6 bytes in 5 codewords (or 1 byte in 2 codewords for short sequences)
  • Efficiency: ~1.2 bytes per codeword
  • Character set: Arbitrary 8-bit bytes
  • Best for: Binary data, international text, mixed content

Numeric Compaction

  • Encoding: 44 decimal digits in 15 codewords
  • Efficiency: ~2.9 digits per codeword
  • Character set: Digits 0-9 only
  • Best for: Serial numbers, quantities, numeric identifiers

Compact vs Standard Format

Standard PDF417

  • Includes left and right row indicator columns
  • Provides redundancy for damaged barcodes
  • ~10-15% larger than compact format
  • Use when: Reliability is critical, space is not constrained

Compact PDF417

  • Omits right row indicator column
  • Smaller overall size
  • Slightly less error recovery capability
  • Use when: Space is limited, print quality is good
// Enable compact format
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.PDF417_COMPACT, Boolean.TRUE);

Best Practices

Error Correction Selection:

  • Use level 2 (default) for most applications
  • Use level 4-5 for documents, identification, tickets
  • Use level 6-7 for damaged items, outdoor use
  • Never use level 0-1 unless space is extremely constrained

Dimension Configuration:

  • Let encoder auto-select dimensions when possible
  • Use dimension constraints to fit specific label sizes
  • Prefer wider, shorter barcodes for landscape orientations
  • Prefer narrower, taller barcodes for portrait orientations

Compaction Mode:

  • Use AUTO (default) for mixed content
  • Use TEXT when data is primarily alphabetic
  • Use NUMERIC for all-numeric data (maximum efficiency)
  • Use BYTE for binary data or international characters

Quality Considerations:

  • Ensure adequate quiet zone (2 modules minimum)
  • Use high contrast printing (black on white)
  • Print at least 2 pixels per module
  • Test with actual scanners before production
  • Consider lighting conditions in deployment environment

Application Guidelines:

  • ID Cards/Passports: High EC level (5-6), compact format
  • Shipping Labels: Standard format, medium EC (2-3)
  • Tickets/Boarding Passes: Compact format, high EC (4-5)
  • Inventory Tags: Standard format, medium EC (2-3)
  • Documents: High EC (4-6), text compaction

See Also

  • Core Reading and Writing - Basic reading and writing operations
  • Configuration Hints - PDF417_COMPACT, PDF417_COMPACTION, PDF417_DIMENSIONS hints
  • Multiple Barcode Detection - PDF417Reader supports decodeMultiple()
  • Barcode Formats - Comparison with other 2D formats
  • Exception Handling - Handling ChecksumException and FormatException

Install with Tessl CLI

npx tessl i tessl/maven-com-google-zxing--core

docs

index.md

tile.json