Java 相同的表格,相同的条形码(128),Zxing只读取一个条形码(来自2个文档)

Java 相同的表格,相同的条形码(128),Zxing只读取一个条形码(来自2个文档),java,pdf,barcode,zxing,Java,Pdf,Barcode,Zxing,我正在写一个简单的程序,从图像中提取条形码 我试过zxing。它工作得很好。直到我发现一个奇怪的东西 我所在的组织发布表单。相同的表格(我从世界上的两个地方得到的) org)使用相同的条形码,我在同一台机器(同样的结果)中扫描它们以 pdf格式 zxing在第一张图像上做得很好,并且返回了条形码。运气不好 第二个图像。。我在尝试提取条形码时遇到com.google.zxing.NotFoundException 从第二个图像。问题发生在组织的更多形式上) 这是Zxing无法识别的图像 这是他确

我正在写一个简单的程序,从图像中提取条形码

我试过zxing。它工作得很好。直到我发现一个奇怪的东西

我所在的组织发布表单。相同的表格(我从世界上的两个地方得到的)

org)使用相同的条形码,我在同一台机器(同样的结果)中扫描它们以

pdf格式

zxing在第一张图像上做得很好,并且返回了条形码。运气不好

第二个图像。。我在尝试提取条形码时遇到com.google.zxing.NotFoundException

从第二个图像。问题发生在组织的更多形式上)

这是Zxing无法识别的图像

这是他确实认识的人

这是我的代码:

private String handlePdf(File pdfFile) throws Exception { 


    StringBuilder sb = new StringBuilder();  


    PDDocument pdDoc = PDDocument.load(pdfFile);  


    int size = pdDoc.getDocumentCatalog().getAllPages().size();  


    for (int i = 0; i < size; i++) {    


        PDPage page = (PDPage) pdDoc.getDocumentCatalog().getAllPages().get(i); 


        PDResources resources = page.getResources();  


        Map images = resources.getImages();  


           if (images != null) {  


            Iterator<String> imageIter = images.keySet().iterator();  


                   while (imageIter.hasNext()) {  


                  String key = (String) imageIter.next();  


                  PDXObjectImage image = (PDXObjectImage) images.get(key);  


                  String barcode = null ;  


                  barcode = extraceBarcodeFromImage(image.getRGBImage());  


                if(barcode!= null){  


                   sb.append(barcode);  


                   sb.append(",");  


                }  


               }  


         }  

    }  


     return sb.capacity() > 0 ? sb.toString().substring(0, sb.length()-1) : 
                                  "no barcode was found";


}    



private String extraceBarcodeFromImage(BufferedImage image)  


            throws NotFoundException {  


    String finalResult;  


    if (image == null)  


        throw new IllegalArgumentException("Could not decode image.");  


    Map<DecodeHintType, Object> HINTS;  


    HINTS = new EnumMap(DecodeHintType.class);  


    HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);  


    HINTS.put(DecodeHintType.POSSIBLE_FORMATS,  


            EnumSet.allOf(BarcodeFormat.class));  


    Map<DecodeHintType, Object> HINTS_PURE;  


    HINTS_PURE = new EnumMap<DecodeHintType, Object>(HINTS);  


    HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);  


    LuminanceSource source = new BufferedImageLuminanceSource(image);  


    BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(  


            source));  


    MultiFormatReader barcodeReader = new MultiFormatReader();   


    Result result;  


    result = barcodeReader.decode(bitmap, HINTS_PURE);   


    finalResult = String.valueOf(result.getText());  


    return finalResult;  


}  
私有字符串handlePdf(文件pdfFile)引发异常{
StringBuilder sb=新的StringBuilder();
PDDocument pdDoc=PDDocument.load(pdfFile);
int size=pdDoc.getDocumentCatalog().getAllPages().size();
对于(int i=0;i0?sb.toString()子字符串(0,sb.length()-1):
“未找到条形码”;
}    
私有字符串extraceBarcodeFromImage(BuffereImage图像)
抛出NotFoundException{
字符串最终结果;
if(image==null)
抛出新的IllegalArgumentException(“无法解码图像”);
地图提示;
提示=新的EnumMap(DecodeHintType.class);
put(DecodeHintType.TRY_,Boolean.TRUE);
提示.put(DecodeHintType.mables_格式,
allOf(BarcodeFormat.class));
地图提示(u PURE),;
提示\u PURE=新的枚举映射(提示);
提示\u PURE.put(DecodeHintType.PURE\u条形码,布尔值.TRUE);
亮度源=新的缓冲区图像亮度源(图像);
BinaryBitmap=新的BinaryBitmap(新的GlobalHistorogrambinarizer(
资料来源);
MultiFormatReader条形码阅读器=新的MultiFormatReader();
结果;
结果=条形码阅读器.decode(位图,提示\u PURE);
finalResult=String.valueOf(result.getText());
返回最终结果;
}  

感谢您在这种情况下提供的任何帮助

问题只是图像质量相当低

以一定的放大倍数查看工作条形码:

质量不好,但已经足够好了

现在将其与不起作用的条形码进行比较:

这是有点旋转(可能是由于旋转),酒吧是非常边缘。条纹太多,zxing无法识别条形码

我用一个调试器处理了这个问题,甚至让它查看图像的每一行(在不进行操作的情况下,它只查看256行等间距的行),它也没有找到任何可以成功读取为条形码的行


因此,您要么必须提高扫描质量(更高的分辨率,确保文档不旋转,…),要么必须寻找条码读取器库,它不仅单独查看图像列或行,而是尝试识别更大的结构。我对这些东西还不够深入,无法推荐任何东西。

你能提供实际的PDF进行分析吗?我提供了两个PDF链接。它们坏了吗?好的。当我试着从我的手机下载时,一些html被存储,没有PDF。现在在我的桌面上下载了实际的PDF。谢谢你,如果能得到你的意见,我将不胜感激。