1

我的测试用例非常简单:我正在生成一个数据矩阵代码,然后我想再次读取它。两者都使用 xzing vs3.0.0。我使用 qr-code 和 pdf417 以同样的方式执行此操作 - 它运行良好。

这是我的代码:

   @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();
}

如果我运行上面的测试,就会在 out 中生成一个数据矩阵图像。此文件可由 xzing 在线阅读器读取。但它不适用于我自己的代码:

com.google.zxing.NotFoundException

有任何想法吗?提前致谢。

4

1 回答 1

1

我有同样的问题,但这对我有用。我认为默认情况下,库需要条形码中的边距,所以如果你没有它们,请使用 PURE_BARCODE 提示。

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();
}
于 2015-08-19T05:35:29.653 回答