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

datamatrix.mddocs/reference/

Data Matrix Support

Complete Data Matrix 2D barcode encoding and decoding support with configurable symbol shapes (square, rectangular, or automatic selection). Data Matrix codes are widely used in electronics manufacturing, healthcare, and logistics for marking small items with high-density data storage and excellent error correction.

Capabilities

DataMatrixReader

Reader implementation for detecting and decoding Data Matrix barcodes from binary bitmap images. Supports both standard detection with corner finder patterns and fast pure barcode extraction for clean images.

/**
 * Reader implementation for Data Matrix 2D barcodes.
 * Locates and decodes Data Matrix codes in images using corner pattern detection
 * or pure barcode extraction for clean images.
 */
public final class DataMatrixReader implements Reader {
    /**
     * Creates a new Data Matrix reader.
     */
    public DataMatrixReader();

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

    /**
     * Locates and decodes a Data Matrix 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 Data Matrix code cannot be found in the image
     * @throws ChecksumException If error correction fails
     * @throws FormatException If a Data Matrix code cannot be decoded
     */
    @Override
    public Result decode(BinaryBitmap image, Map<DecodeHintType,?> hints)
        throws NotFoundException, ChecksumException, FormatException;

    /**
     * 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.datamatrix.*;
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 Data Matrix code
Reader reader = new DataMatrixReader();
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);

// Decode with hints for pure barcode
Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");

Result result2 = reader.decode(bitmap, hints);

DataMatrixWriter

Writer implementation for encoding Data Matrix barcodes. Generates a BitMatrix representation that can be rendered to an image with configurable symbol shape and size constraints.

/**
 * Writer implementation for Data Matrix 2D barcodes.
 * Encodes text into Data Matrix BitMatrix with configurable shape
 * and size constraints.
 */
public final class DataMatrixWriter implements Writer {
    /**
     * Creates a new Data Matrix writer.
     */
    public DataMatrixWriter();

    /**
     * Encodes contents into a Data Matrix BitMatrix with default settings.
     * Uses automatic shape selection (square preferred).
     *
     * @param contents String content to encode
     * @param format Must be BarcodeFormat.DATA_MATRIX
     * @param width Preferred width in pixels
     * @param height Preferred height in pixels
     * @return BitMatrix representation of the Data Matrix code
     * @throws IllegalArgumentException If contents is empty or format is not DATA_MATRIX
     */
    @Override
    public BitMatrix encode(String contents, BarcodeFormat format, int width, int height);

    /**
     * Encodes contents into a Data Matrix BitMatrix with configuration hints.
     *
     * @param contents String content to encode (cannot be empty)
     * @param format Must be BarcodeFormat.DATA_MATRIX
     * @param width Preferred width in pixels (must be non-negative)
     * @param height Preferred height in pixels (must be non-negative)
     * @param hints Map of EncodeHintType to configuration values
     *              - DATA_MATRIX_SHAPE (SymbolShapeHint): FORCE_NONE, FORCE_SQUARE, or FORCE_RECTANGLE
     *              - MIN_SIZE (Dimension): Minimum symbol size (deprecated)
     *              - MAX_SIZE (Dimension): Maximum symbol size (deprecated)
     *              - CHARACTER_SET (String): Character encoding (e.g., "UTF-8")
     *              - DATA_MATRIX_COMPACT (Boolean): Use compact minimal encoding
     *              - GS1_FORMAT (Boolean): Use GS1 format with FNC1 character
     *              - FORCE_C40 (Boolean): Force C40 encoding mode
     * @return BitMatrix representation of the Data Matrix code
     * @throws IllegalArgumentException If parameters are invalid
     */
    @Override
    public BitMatrix encode(String contents, BarcodeFormat format, int width, int height,
                          Map<EncodeHintType,?> hints);
}

Usage Example:

import com.google.zxing.*;
import com.google.zxing.common.*;
import com.google.zxing.datamatrix.*;
import com.google.zxing.datamatrix.encoder.SymbolShapeHint;
import java.util.*;

// Encode with default settings (square preferred)
Writer writer = new DataMatrixWriter();
BitMatrix matrix = writer.encode(
    "https://example.com",
    BarcodeFormat.DATA_MATRIX,
    200,
    200
);

// Encode with forced rectangular shape
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_RECTANGLE);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

BitMatrix matrix2 = writer.encode(
    "Product ID: 12345",
    BarcodeFormat.DATA_MATRIX,
    300,
    150,
    hints
);

// Encode with forced square shape
hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE);

BitMatrix matrix3 = writer.encode(
    "Small data",
    BarcodeFormat.DATA_MATRIX,
    100,
    100,
    hints
);

// Encode with GS1 format for product identification
hints.clear();
hints.put(EncodeHintType.GS1_FORMAT, Boolean.TRUE);
hints.put(EncodeHintType.DATA_MATRIX_COMPACT, Boolean.TRUE);

BitMatrix gs1Matrix = writer.encode(
    "01095011010209171719050810ABCD1234", // GS1 AI format
    BarcodeFormat.DATA_MATRIX,
    200,
    200,
    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;
    }
}

SymbolShapeHint

Enumeration for specifying preferred Data Matrix symbol shape. Used during encoding to control whether the resulting symbol should be square, rectangular, or automatically selected based on data size.

/**
 * Enumeration for Data Matrix symbol shape preference.
 * Controls whether generated symbols are square, rectangular, or auto-selected.
 */
public enum SymbolShapeHint {
    /**
     * No shape preference - automatic selection based on data size.
     * Square symbols are preferred when multiple options are available.
     */
    FORCE_NONE,

    /**
     * Force square symbols only.
     * Will select smallest square symbol that fits the data.
     * Examples: 10x10, 12x12, 14x14, 16x16, 18x18, 20x20, etc.
     */
    FORCE_SQUARE,

    /**
     * Force rectangular symbols only.
     * Will select smallest rectangular symbol that fits the data.
     * Examples: 8x18, 8x32, 12x26, 12x36, 16x36, 16x48, etc.
     */
    FORCE_RECTANGLE
}

Usage Example:

import com.google.zxing.*;
import com.google.zxing.datamatrix.encoder.SymbolShapeHint;
import java.util.*;

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

// Automatic shape selection (default)
hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_NONE);
BitMatrix auto = writer.encode("Data", BarcodeFormat.DATA_MATRIX, 200, 200, hints);

// Force square for aesthetic or space reasons
hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_SQUARE);
BitMatrix square = writer.encode("Data", BarcodeFormat.DATA_MATRIX, 200, 200, hints);
// Will use 10x10, 12x12, 14x14, etc.

// Force rectangular for better fit in narrow spaces
hints.put(EncodeHintType.DATA_MATRIX_SHAPE, SymbolShapeHint.FORCE_RECTANGLE);
BitMatrix rect = writer.encode("Data", BarcodeFormat.DATA_MATRIX, 300, 150, hints);
// Will use 8x18, 8x32, 12x26, etc.

Data Matrix Symbol Sizes

Data Matrix codes support various symbol sizes with different capacities:

Square Symbols

SizeNumericAlphanumericBinaryUse Case
10x10631Minimal data
12x121063Small labels
14x1416106Product marking
16x16241610Standard use
18x18362516Medium data
20x20483222Common size
22x22624328Extended data
24x24765234Large capacity
26x26986744Very large
32x3215210469Maximum square
36x3619213288Extended square
40x40235161107Large square
44x44288198132Very large square
48x48348239159Huge square
52x52408280186Maximum square
64x64560385256Extended capacity
72x72736505336Very high capacity
80x80912626416Extremely large
88x881152792527Maximum data
96x961392956636Huge data
104x10416321120744Very huge data
120x12021001443960Maximum square
132x132260817921191Extremely huge
144x144311621381423Maximum capacity

Rectangular Symbols

SizeNumericAlphanumericBinaryUse Case
8x181063Narrow labels
8x3220138Long narrow
12x26322214Medium narrow
12x36443020Extended narrow
16x36644328Wide medium
16x48986744Very wide

Encoding Modes

Data Matrix supports multiple encoding modes for optimal data compression:

ASCII Mode

  • Default mode for ASCII characters (0-127)
  • Efficient for digits (2 per byte) and uppercase letters
  • Best for: Product IDs, serial numbers, basic text

C40 Mode

  • Optimized for uppercase alphanumeric text
  • Efficient encoding: 3 characters in 2 bytes
  • Best for: Part numbers, tracking codes, uppercase identifiers

Text Mode

  • Optimized for lowercase text
  • Similar efficiency to C40 for lowercase
  • Best for: Descriptive text, sentences

X12 Mode

  • Optimized for ANSI X12 EDI character set
  • Specialized for electronic data interchange
  • Best for: EDI documents, shipping labels

EDIFACT Mode

  • Optimized for ASCII values 32-94
  • Efficient 4 characters in 3 bytes encoding
  • Best for: EDIFACT messages, specialized applications

Base 256 Mode

  • Raw 8-bit binary data
  • One byte per character
  • Best for: Binary data, Unicode text, arbitrary bytes

Advanced Configuration

GS1 Format

Data Matrix supports GS1 Application Identifiers for product identification:

Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.GS1_FORMAT, Boolean.TRUE);
hints.put(EncodeHintType.DATA_MATRIX_COMPACT, Boolean.TRUE);

// Encode GS1 data with AIs
// (01) GTIN, (17) Expiry Date, (10) Batch Number
BitMatrix gs1 = writer.encode(
    "01095011010209171719050810BATCH123",
    BarcodeFormat.DATA_MATRIX,
    200,
    200,
    hints
);

Compact Encoding

Use minimal encoding for optimal data compression:

Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.DATA_MATRIX_COMPACT, Boolean.TRUE);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

// Uses advanced mode switching for smallest possible symbol
BitMatrix compact = writer.encode(
    "Mixed123Data",
    BarcodeFormat.DATA_MATRIX,
    150,
    150,
    hints
);

Error Correction

Data Matrix uses Reed-Solomon error correction with approximately 30% of symbol dedicated to error correction. This allows recovery from:

  • Up to 30% data corruption
  • Damaged or obscured modules
  • Poor print quality
  • Scratches or dirt

Best Practices

Symbol Shape Selection:

  • Use FORCE_SQUARE for aesthetic consistency or round labels
  • Use FORCE_RECTANGLE for narrow spaces (circuit boards, cables)
  • Use FORCE_NONE (default) for optimal automatic selection

Size Optimization:

  • Let the encoder select the smallest symbol that fits your data
  • Consider data growth - leave room for future expansion
  • Test readability at actual print size

Encoding Optimization:

  • Use DATA_MATRIX_COMPACT for maximum efficiency
  • Specify CHARACTER_SET when using non-ASCII characters
  • Use GS1_FORMAT for supply chain and product applications

Quality Considerations:

  • Ensure adequate quiet zone (1 module minimum, 2 preferred)
  • Use high contrast (black on white recommended)
  • Print at least 10 pixels per module for reliable scanning
  • Test with actual scanners in production environment

Application-Specific:

  • Electronics: Small square symbols (10x10 to 16x16)
  • Healthcare: GS1 format with expiry dates and lot numbers
  • Logistics: Rectangular symbols (12x26, 16x36) for labels
  • Direct Part Marking: Permanent laser etching or dot peen

See Also

  • Core Reading and Writing - Basic reading and writing operations
  • Configuration Hints - DATA_MATRIX_SHAPE, DATA_MATRIX_COMPACT, GS1_FORMAT hints
  • Barcode Formats - Comparison with other 2D formats
  • Result Handling - Working with Result objects
  • Image Processing - Image preparation for decoding

Install with Tessl CLI

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

docs

index.md

tile.json