Core barcode encoding/decoding library supporting 17 formats including QR Code, Data Matrix, Aztec, PDF 417, and various 1D barcodes
—
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).
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());
}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;
}
}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);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 is a stacked linear barcode consisting of:
Maximum data capacity varies by error correction level:
| EC Level | Numeric | Alphanumeric | Binary | % Overhead |
|---|---|---|---|---|
| 0 | 2710 | 1850 | 1108 | ~5% |
| 1 | 2610 | 1785 | 1067 | ~10% |
| 2 | 2470 | 1685 | 1009 | ~18% (default) |
| 3 | 2320 | 1585 | 948 | ~27% |
| 4 | 2000 | 1365 | 817 | ~39% |
| 5 | 1520 | 1040 | 622 | ~56% |
| 6 | 1000 | 685 | 410 | ~75% |
| 7 | 560 | 385 | 230 | ~112% |
| 8 | 160 | 110 | 65 | ~195% |
Recommended levels:
// Enable compact format
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.PDF417_COMPACT, Boolean.TRUE);Error Correction Selection:
Dimension Configuration:
Compaction Mode:
Quality Considerations:
Application Guidelines: