简体   繁体   English

扫描的条形码很少,但很少使用Zxing

[英]few barcodes are scanned but few are not using Zxing

I am using zxing to scan barcode and split that in pdf. 我正在使用zxing扫描条形码并将其拆分为pdf。 But most barcodes are scanned and few aren't. 但是大多数条形码都会被扫描,而很少被扫描。 Although all barcodes are properly visible and i can scan them using barcode android app. 虽然所有条码都正确可见,我可以使用条码android应用程序对其进行扫描。 my code is 我的代码是

    Boolean flag = Boolean.FALSE;
    PDDocument pdfDoc = null;
    Result prevResult = null;
    try{
        pdfDoc = PDDocument.load(new File(pathToReadPdf));
        log.debug("Total pdf pages : "+pdfDoc.getNumberOfPages());

        Reader reader = new MultiFormatReader();
        List<PDPage> pages = pdfDoc.getDocumentCatalog().getAllPages();
        for(PDPage page : pages) {
            PDResources resources = page.getResources();
            // Identify images from pdf
            Map images = resources.getImages();
            if( images != null ){
                Iterator imageIter = images.keySet().iterator();
                while( imageIter.hasNext()){
                    String key = (String)imageIter.next();
                    PDXObjectImage image = (PDXObjectImage)images.get( key );
                    if (image.getRGBImage()!=null){
                    Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>(3);
                    Vector<BarcodeFormat> barcodeFormats = new Vector<BarcodeFormat>();
                    barcodeFormats.add(BarcodeFormat.CODE_128);
                    decodeHints.put(DecodeHintType.POSSIBLE_FORMATS, barcodeFormats);
                    decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
                    decodeHints.put(DecodeHintType.PURE_BARCODE, true);
                    //decodeHints.put(DecodeHintType.CHARACTER_SET, "ISO-8859-1");
                    LuminanceSource source = new BufferedImageLuminanceSource(image.getRGBImage(), 0, 0, image.getWidth(), image.getHeight());
                    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
                    Result result = null;

                    try{
                        result = reader.decode(bitmap, decodeHints);
                        splitPdf(page, result, loanApplicationId);
                        prevResult= result;
                        flag = Boolean.TRUE;
                    }catch(NotFoundException nfe){
                        if(prevResult!=null){
                            mergePDF(page, prevResult, loanApplicationId);
                        }
                        continue;
                    }
                    log.debug("Barcode text is " + result.getText());
                    }
                }
            }
        }
    }catch(Exception e){
        e.printStackTrace();
        log.error("Error while splitting PDF : " + e.getMessage(), e);
    }
    finally {
        try{
            if(pdfDoc != null){
                pdfDoc.close();
            }
        }catch (IOException ioe){
            ioe.printStackTrace();
            log.error("Error while closing PDF : " + ioe.getMessage(), ioe);
        }
    }
    return flag;` 

I think error might be in bitmap conversion. 我认为错误可能出在位图转换中。 I am getting error at com.google.zxing.NotFoundException at result = reader.decode(bitmap, decodeHints); 我在com.google.zxing.NotFoundException时报错, result = reader.decode(bitmap, decodeHints);

barcode creation logic: 条形码创建逻辑:

 public byte[] createBarCode128(String fileName) {

    byte[] imageInByte = new byte[1024];
    try {
        Code128Bean bean = new Code128Bean();
        final int dpi = 300;

        //Configure the barcode generator
        bean.setModuleWidth(UnitConv.in2mm(6.0f / dpi));
        bean.doQuietZone(false);

        BitmapCanvasProvider canvas = new BitmapCanvasProvider(null, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);

        //Generate the barcode
        bean.generateBarcode(canvas, fileName);

        //Signal end of generation
        canvas.finish();

        BufferedImage originalImage = canvas.getBufferedImage();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(originalImage, "jpg", baos);
        baos.flush();
        imageInByte = baos.toByteArray();
        log.debug(imageInByte.toString());
        baos.close();
        log.debug(" Bar Code is generated successfully ");
    }
    catch (IOException ex) {
        ex.printStackTrace();
        log.error(ex.getMessage(),ex);
    }
    return imageInByte;
}

I am using below dependencies: 我正在使用以下依赖项:

 <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>core</artifactId>
        <version>2.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>fontbox</artifactId>
        <version>1.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>1.8.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.zxing</groupId>
        <artifactId>javase</artifactId>
        <version>2.2</version>
    </dependency>

my java version is 6 so zxing version 3 is not supported. 我的Java版本是6,所以不支持zxing版本3。

Please suggest any solution. 请提出任何解决方案。

Having looked at the sample file provided by the OP I can see no real differences between the barcodes zxing can scan and those it cannot scan. 查看了OP提供的示例文件后,我看不到zxing可以扫描的条形码和不能扫描的条形码之间没有真正的区别。 They all seem to be scanned at 300 dpi and embedded in the same fashion. 它们似乎都以300 dpi扫描并以相同的方式嵌入。

Zooming into the image one can see, though, that the scanning quality is quite poor for the purpose of barcode recognition: 但是,放大图像可以看到,出于条形码识别的目的,扫描质量相当差:

在此处输入图片说明

The scanned bar code outlines are not sharply cut and have some saw tooth pattern. 扫描的条形码轮廓没有被清晰地切割,并且具有一些锯齿形图案。 This makes the bars appear to be of differing width on differing scan lines. 这使得条在不同的扫描线上看起来具有不同的宽度。

I assume you simply are lucky with the codes you can recognize. 我认为您只是可以识别代码而感到幸运。

I'd propose changing the scan properties, maybe b&w instead of grayscale, maybe a different resolution... 我建议更改扫描属性,也许是黑白而不是灰度,也许是不同的分辨率...

As you said, some barcode are scanning and some are not. 如您所说,有些条形码正在扫描,有些则没有。 Please check whether that format is in Zxing list supported format. 请检查该格式是否为Zxing列表支持的格式。

  1. QR_CODE 二维码
  2. DATA_MATRIX DATA_MATRIX
  3. UPC_E UPC_E
  4. UPC_A UPC_A
  5. EAN_8 EAN_8
  6. EAN_13 EAN_13
  7. UPC_EAN_EXTENSION UPC_EAN_EXTENSION
  8. CODE_128 CODE_128
  9. CODE_39 CODE_39

You can give the code and test in this. 您可以在此给出代码并进行测试。 http://www.barcode-generator.org/ http://www.barcode-generator.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM