我的测试用例非常简单:我生成了一个数据矩阵代码,然后我想再次阅读它。两者都与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();
}
如果我运行上面的测试,将生成一个数据矩阵图像。 xzing在线阅读器可以读取此文件。但它不适用于我自己的代码:
com.google.zxing.NotFoundException
有什么想法吗?提前谢谢。
答案 0 :(得分: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();
}