1

My test case is very simple: I'm generating a data matrix code and then I want to read it again. Both with xzing vs3.0.0. I'm doing this the same way with qr-code and pdf417 - and it works perfectly.

This is my code:

   @Test
public void testDataMatrix() throws Exception {
    writeDataMatrix();
    String result = readDataMatrix("out/data_matrix.png", "UTF-8", new EnumMap<DecodeHintType, Object>(DecodeHintType.class));
    assertEquals("my message", result);
}

public static void writeDataMatrix() throws IOException {
    DataMatrixWriter writer = new DataMatrixWriter();
    BitMatrix matrix = writer.encode("my message", BarcodeFormat.DATA_MATRIX, 100, 100);

    MatrixToImageWriter.writeToPath(matrix, "PNG", Paths.get("out/data_matrix.png"));
}

public static String readDataMatrix(String filePath, String charset, Map hintMap)
        throws FileNotFoundException, IOException, NotFoundException {
    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
            new BufferedImageLuminanceSource(
                    ImageIO.read(new FileInputStream(filePath)))));
    Result qrCodeResult = new MultiFormatReader().decode(binaryBitmap,
            hintMap);
    return qrCodeResult.getText();
}

If I run the test above, a data matrix image will be generated in out. This file is readable by the xzing online reader. But it works not in my own code:

com.google.zxing.NotFoundException

Any ideas? Thanks in advance.

xdoo
  • 11
  • 3

1 Answers1

1

I had the same problem but this worked for me. I think by default the library expects margins in the barcode so if you don't have them use the PURE_BARCODE hint.

public static String readDataMatrix(String filePath, String charset)
   throws FileNotFoundException, IOException, NotFoundException
{
   HashMap<DecodeHintType, Object> decodeHintMap = new HashMap<DecodeHintType, Object>();
   decodeHintMap.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);

   BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(ImageIO.read(new FileInputStream(filePath)))));

   Result codeResult = new DataMatrixReader().decode(binaryBitmap, decodeHintMap);

   return codeResult.getText();
}
vik
  • 323
  • 3
  • 9