【问题标题】:reading datamatrix with xzing lib in java在java中使用zxing lib读取数据矩阵
【发布时间】:2014-03-21 06:38:44
【问题描述】:

我的测试用例非常简单:我正在生成一个数据矩阵代码,然后我想再次读取它。两者都使用 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

有什么想法吗?提前致谢。

【问题讨论】:

    标签: java zxing datamatrix


    【解决方案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();
    }
    

    【讨论】:

    • 这正是我所缺少的。现在它起作用了。请选择此作为接受的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    相关资源
    最近更新 更多