我正在尝试使用ZXing 2.1库获得一个成功的结果。我在Mac 10.7.5上使用Java 1.6。我可以编码文本,但不能解码任何图像。相反,我得到的只是com.google.zxing.NotFoundException的一行堆栈跟踪。
这看起来很简单,但我不知道我做错了什么。下面是一个简单的重现测试。它将几个条形码编码成图像,然后从内存中解码图像:
public class App {
public static void main(String[] args) {
// Try UPC-A.
try {
testEncodeDecode(BarcodeFormat.UPC_A, "012345678905"); // Valid UPC-A.
} catch (Exception e) {
e.printStackTrace();
}
// Try EAN-13.
try {
testEncodeDecode(BarcodeFormat.EAN_13, "9310779300005"); // Valid EAN-13.
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testEncodeDecode(BarcodeFormat barcodeFormat, String text)
throws WriterException, NotFoundException, ChecksumException, FormatException, IOException {
// Size of buffered image.
int width = 200;
int height = 100;
// Encode to buffered image.
Writer writer = new MultiFormatWriter();
BitMatrix bitMatrix = writer.encode(text, barcodeFormat, width, height);
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
// Write to disk for debugging.
String formatName = "png";
File outputFile = new File(text + "." + formatName);
ImageIO.write(bufferedImage, formatName, outputFile);
// Decode from buffered image.
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);
// Never gets this far!
System.out.println("result=" + result.getText());
}
}输出将简单地为
com.google.zxing.NotFoundException
com.google.zxing.NotFoundException我被难住了!谢谢你的帮助。随函附上输出图像供您参考。
UPC-A

EAN-13

发布于 2013-08-02 13:22:11
我在一开始也面临着类似的问题,但通过提示为我解决了它。您可以先尝试传递TRY_HARDER。应该能行得通。如果没有,请尝试传递POSSIBLE_FORMATS提示,因为您已经知道这些格式。检查并查看这两个提示是否都有效。
发布于 2012-11-19 04:28:20
从简单的角度来看,我认为问题是两边都没有足够的安静区域。规范IIRC要求左右各有9个模块,这个模块大约有2个。
检测器相当宽松,但不是很宽松,以避免假阳性。通常情况下,图像外部的区域被视为一个大的白色平面(实际上,这些区域在白色背景上扫描得很好,就像这个页面),所以它会进行扫描。对于这种格式,我在代码中看到一条注释,它被特别禁用,以避免误报。
您可以尝试禁用此功能或生成更广泛的代码来测试此功能。如果您发现了一个更改,该更改不会增加测试集中的误报,但可以使其通过,那么这可能是值得的。
https://stackoverflow.com/questions/13426239
复制相似问题