Core barcode encoding/decoding library supporting 17 formats including QR Code, Data Matrix, Aztec, PDF 417, and various 1D barcodes
—
Core interfaces and classes for barcode decoding and encoding across all supported formats. The Reader and Writer interfaces define the primary API, while MultiFormatReader and MultiFormatWriter provide convenient multi-format implementations.
Main interface for decoding barcodes from binary bitmap images. Implementations attempt to locate and decode a barcode, throwing specific exceptions when detection or decoding fails.
/**
* Main interface for decoding barcodes from images.
*/
interface Reader {
/**
* Locates and decodes a barcode in the provided binary bitmap.
*
* @param image Binary bitmap representation of the image
* @return Result containing decoded text and metadata
* @throws NotFoundException When no barcode is found in the image
* @throws ChecksumException When barcode checksum validation fails
* @throws FormatException When barcode format rules are violated
*/
Result decode(BinaryBitmap image) throws NotFoundException, ChecksumException, FormatException;
/**
* Locates and decodes a 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 When no barcode is found in the image
* @throws ChecksumException When barcode checksum validation fails
* @throws FormatException When barcode format rules are violated
*/
Result decode(BinaryBitmap image, Map<DecodeHintType,?> hints) throws NotFoundException, ChecksumException, FormatException;
/**
* Resets any internal state. Should be called between decoding attempts.
*/
void reset();
}Main interface for encoding barcodes. Implementations generate a BitMatrix representation of the barcode that can be rendered to an image.
/**
* Main interface for encoding barcodes.
*/
interface Writer {
/**
* Encodes contents into a barcode BitMatrix.
*
* @param contents String content to encode
* @param format Barcode format to generate
* @param width Preferred width in pixels
* @param height Preferred height in pixels
* @return BitMatrix representation of the barcode
* @throws WriterException When encoding fails
*/
BitMatrix encode(String contents, BarcodeFormat format, int width, int height) throws WriterException;
/**
* Encodes contents into a barcode BitMatrix with configuration hints.
*
* @param contents String content to encode
* @param format Barcode format to generate
* @param width Preferred width in pixels
* @param height Preferred height in pixels
* @param hints Map of EncodeHintType to configuration values
* @return BitMatrix representation of the barcode
* @throws WriterException When encoding fails
*/
BitMatrix encode(String contents, BarcodeFormat format, int width, int height, Map<EncodeHintType,?> hints) throws WriterException;
}Multi-format barcode reader that attempts to decode using all supported formats or a subset specified via hints. Automatically dispatches to format-specific readers based on detected or configured formats.
/**
* Multi-format barcode reader implementation.
* Attempts to decode using all supported formats or formats specified in hints.
*/
class MultiFormatReader implements Reader {
/**
* Creates a MultiFormatReader with default settings.
*/
public MultiFormatReader();
/**
* Sets configuration hints for this reader.
*
* @param hints Map of DecodeHintType to configuration values
*/
public void setHints(Map<DecodeHintType,?> hints);
/**
* Decodes a barcode using the current state without resetting.
* Useful for continuous scanning scenarios where you want to maintain
* state across multiple decode attempts.
*
* @param image Binary bitmap representation of the image
* @return Result containing decoded text and metadata
* @throws NotFoundException When no barcode is found in the image
*/
public Result decodeWithState(BinaryBitmap image) throws NotFoundException;
// Implements Reader interface methods
}Usage Example:
import com.google.zxing.*;
import com.google.zxing.common.*;
import java.util.*;
// Create reader
Reader reader = new MultiFormatReader();
// Decode with default settings
Result result = reader.decode(binaryBitmap);
// Decode with hints
Map<DecodeHintType, Object> hints = new EnumMap<>(DecodeHintType.class);
hints.put(DecodeHintType.POSSIBLE_FORMATS,
Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.DATA_MATRIX));
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result2 = reader.decode(binaryBitmap, hints);
// Access result data
String text = result2.getText();
BarcodeFormat format = result2.getBarcodeFormat();
ResultPoint[] points = result2.getResultPoints();Multi-format barcode writer that routes encoding requests to the appropriate format-specific writer based on the requested BarcodeFormat.
/**
* Multi-format barcode writer implementation.
* Routes encoding requests to format-specific writers.
*/
class MultiFormatWriter implements Writer {
/**
* Creates a MultiFormatWriter.
*/
public MultiFormatWriter();
// Implements Writer interface methods
}Usage Example:
import com.google.zxing.*;
import com.google.zxing.common.*;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.util.*;
// Create writer
Writer writer = new MultiFormatWriter();
// Encode with default settings
BitMatrix matrix = writer.encode(
"Hello, World!",
BarcodeFormat.QR_CODE,
300,
300
);
// Encode with hints
Map<EncodeHintType, Object> hints = new EnumMap<>(EncodeHintType.class);
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix matrix2 = writer.encode(
"Hello, World!",
BarcodeFormat.QR_CODE,
300,
300,
hints
);
// Convert BitMatrix to image pixels
int width = matrix2.getWidth();
int height = matrix2.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] = matrix2.get(x, y) ? 0xFF000000 : 0xFFFFFFFF;
}
}Binary representation of an image for barcode decoding. Created from a Binarizer which converts grayscale luminance to 1-bit black/white.
/**
* Binary representation of an image for barcode decoding.
*/
class BinaryBitmap {
/**
* Creates a BinaryBitmap from a Binarizer.
*
* @param binarizer Binarizer containing the image data
*/
public BinaryBitmap(Binarizer binarizer);
/**
* Gets the width of the bitmap.
*
* @return Width in pixels
*/
public int getWidth();
/**
* Gets the height of the bitmap.
*
* @return Height in pixels
*/
public int getHeight();
/**
* Gets a single row of black/white pixels.
*
* @param y Row index (0-based)
* @param row Optional BitArray to reuse (may be null)
* @return BitArray containing the row data
* @throws NotFoundException If the row cannot be binarized
*/
public BitArray getBlackRow(int y, BitArray row) throws NotFoundException;
/**
* Gets the entire black/white matrix.
*
* @return BitMatrix containing the entire image
* @throws NotFoundException If the matrix cannot be binarized
*/
public BitMatrix getBlackMatrix() throws NotFoundException;
/**
* Checks if cropping is supported.
*
* @return true if cropping is supported
*/
public boolean isCropSupported();
/**
* Crops the bitmap to a rectangular region.
*
* @param left Left coordinate
* @param top Top coordinate
* @param width Width of the region
* @param height Height of the region
* @return New cropped BinaryBitmap
*/
public BinaryBitmap crop(int left, int top, int width, int height);
/**
* Checks if rotation is supported.
*
* @return true if rotation is supported
*/
public boolean isRotateSupported();
/**
* Rotates the bitmap 90 degrees counter-clockwise.
*
* @return New rotated BinaryBitmap
*/
public BinaryBitmap rotateCounterClockwise();
/**
* Rotates the bitmap 45 degrees counter-clockwise.
*
* @return New rotated BinaryBitmap
*/
public BinaryBitmap rotateCounterClockwise45();
}Usage Example:
import com.google.zxing.*;
import com.google.zxing.common.*;
// From RGB pixels
int[] pixels = ...; // RGB int array
int width = ...;
int height = ...;
LuminanceSource source = new RGBLuminanceSource(width, height, pixels);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
// Use bitmap for decoding
Reader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);Commonly used DecodeHintType values for configuring readers:
POSSIBLE_FORMATS (Collection<BarcodeFormat>): Limits detection to specific formatsTRY_HARDER (Boolean): Spend more time for better accuracyCHARACTER_SET (String): Expected character encoding (e.g., "UTF-8")PURE_BARCODE (Boolean): Image is pure barcode without border/quiet zoneALSO_INVERTED (Boolean): Also try decoding inverted (black/white swapped) imageALLOWED_LENGTHS (int[]): Valid barcode data lengthsASSUME_GS1 (Boolean): Assume GS1 standard encodingNEED_RESULT_POINT_CALLBACK (ResultPointCallback): Callback for result points during detectionCommonly used EncodeHintType values for configuring writers:
ERROR_CORRECTION (Object): Error correction level (format-specific)CHARACTER_SET (String): Character encoding (e.g., "UTF-8")MARGIN (Integer): Quiet zone margin in pixelsQR_VERSION (Integer): Exact QR Code version (1-40)DATA_MATRIX_SHAPE (SymbolShapeHint): Data Matrix shape preferencePDF417_COMPACTION (Compaction): PDF417 compaction modeAZTEC_LAYERS (Integer): Aztec Code layer specification