Core barcode encoding/decoding library supporting 17 formats including QR Code, Data Matrix, Aztec, PDF 417, and various 1D barcodes
—
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.
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);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;
}
}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 codes support various symbol sizes with different capacities:
| Size | Numeric | Alphanumeric | Binary | Use Case |
|---|---|---|---|---|
| 10x10 | 6 | 3 | 1 | Minimal data |
| 12x12 | 10 | 6 | 3 | Small labels |
| 14x14 | 16 | 10 | 6 | Product marking |
| 16x16 | 24 | 16 | 10 | Standard use |
| 18x18 | 36 | 25 | 16 | Medium data |
| 20x20 | 48 | 32 | 22 | Common size |
| 22x22 | 62 | 43 | 28 | Extended data |
| 24x24 | 76 | 52 | 34 | Large capacity |
| 26x26 | 98 | 67 | 44 | Very large |
| 32x32 | 152 | 104 | 69 | Maximum square |
| 36x36 | 192 | 132 | 88 | Extended square |
| 40x40 | 235 | 161 | 107 | Large square |
| 44x44 | 288 | 198 | 132 | Very large square |
| 48x48 | 348 | 239 | 159 | Huge square |
| 52x52 | 408 | 280 | 186 | Maximum square |
| 64x64 | 560 | 385 | 256 | Extended capacity |
| 72x72 | 736 | 505 | 336 | Very high capacity |
| 80x80 | 912 | 626 | 416 | Extremely large |
| 88x88 | 1152 | 792 | 527 | Maximum data |
| 96x96 | 1392 | 956 | 636 | Huge data |
| 104x104 | 1632 | 1120 | 744 | Very huge data |
| 120x120 | 2100 | 1443 | 960 | Maximum square |
| 132x132 | 2608 | 1792 | 1191 | Extremely huge |
| 144x144 | 3116 | 2138 | 1423 | Maximum capacity |
| Size | Numeric | Alphanumeric | Binary | Use Case |
|---|---|---|---|---|
| 8x18 | 10 | 6 | 3 | Narrow labels |
| 8x32 | 20 | 13 | 8 | Long narrow |
| 12x26 | 32 | 22 | 14 | Medium narrow |
| 12x36 | 44 | 30 | 20 | Extended narrow |
| 16x36 | 64 | 43 | 28 | Wide medium |
| 16x48 | 98 | 67 | 44 | Very wide |
Data Matrix supports multiple encoding modes for optimal data compression:
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
);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
);Data Matrix uses Reed-Solomon error correction with approximately 30% of symbol dedicated to error correction. This allows recovery from:
Symbol Shape Selection:
Size Optimization:
Encoding Optimization:
Quality Considerations:
Application-Specific: