【问题标题】:ZXing 2.1: com.google.zxing.NotFoundExceptionZXing 2.1:com.google.zxing.NotFoundException
【发布时间】:2012-11-17 00:07:39
【问题描述】:

我正在尝试使用ZXing 2.1 库获得成功的结果。我在 Mac OS X 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

【问题讨论】:

  • 保存并分享您正在生成的图像——那里可能出现问题。
  • 当然可以。图片已附加,代码现在反映了此步骤。

标签: java exception barcode zxing


【解决方案1】:

一开始我也遇到了类似的问题,但通过提示为我解决了这个问题。 您可以先尝试传递TRY_HARDER。它应该工作。如果没有,请尝试传递POSSIBLE_FORMATS 提示,因为您已经知道格式。检查两个提示是否有效。

【讨论】:

    【解决方案2】:

    从简单的角度来看,我认为问题在于两边都没有足够的安静区。规范 IIRC 需要左右 9 个模块,而这大约有 2 个。

    检测器相当宽松,但不是那么多,以避免误报。通常,图像外部的区域被视为一个大的白色平面(实际上,这些扫描很好,在本页这样的白色背景上)所以它会扫描。对于这种格式,我在代码中看到了一条注释,说明这是为了避免误报而专门禁用的。

    您可以尝试禁用此功能或生成更广泛的代码来测试它。如果您发现更改不会增加测试集中的误报但通过了测试,那么这可能值得提交。

    【讨论】:

    • 你能重现我的问题吗?如何修改代码以完成您的建议?我在类文档中找不到任何内容。谢谢!
    • 没有记录,这是黑客行为。以 EAN13Reader 为例。我认为更好的解决方案是生成具有足够安静区域的代码。如果您没有修改代码的经验,请不要担心尝试修改它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    相关资源
    最近更新 更多