gpt4 book ai didi

java - 使用java中的zxing库读取数据矩阵

转载 作者:太空宇宙 更新时间:2023-11-04 06:57:22 25 4
gpt4 key购买 nike

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

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

最佳答案

我遇到了同样的问题,但这对我有用。我认为默认情况下,库需要条形码中的边距,因此如果您没有它们,请使用 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();
}

关于java - 使用java中的zxing库读取数据矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22551919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com