简体   繁体   English

使用iText从PDF条码图像中检索文本

[英]Retrieve Text from PDF Barcode Images using iText

I have created a PDF by inserting 2 types Bar codes QRCode and Code128 respectively. 我通过分别插入2种条形码QRCode和Code128来创建PDF。

Now my problem is how to extract the Bar Codes Images first and secondly the Text back from those Images, please help me on this, I tried and Google for 2 days but found nothing as suitable way. 现在我的问题是如何首先从这些图像中提取条形码图像,然后再从中提取文本,请对此提供帮助,我尝试了2天,但没有找到合适的方法。

Well I am using itextpdf-5.5.8 release. 我正在使用itextpdf-5.5.8版本。

I'm also working on a similar kind of Project but its in .NET. 我也在研究类似的Project,但它在.NET中。 I'm giving an optional solution for the second issue that is reading the barcode from an image. 我为第二个问题提供了一个可选的解决方案,即从图像中读取条形码。 You can use ZXing for decoding barcode. 您可以使用ZXing解码条形码。 Below is the pseudo-code, you have to pass the image path in the function : 下面是伪代码,您必须在函数中传递图像路径:

static void ScanBarCode(string FileName) {
    System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(FileName);
    try {
        BarcodeReader reader = new BarcodeReader { AutoRotate = true, TryHarder = true };
        Result result = reader.Decode(bitmap);
        string decodedData = result.Text;
        Console.WriteLine(result.ToString());
    } catch {
        throw new Exception("Cannot decode the Barcode code");
    }
}

Well as before I commented about a trick for Code128 Barcode Image was not getting the mouse click select in pdf, as the QRCode. 像以前一样,我评论了Code128 Barcode Image的技巧,它没有使鼠标单击PDF中的选择(作为QRCode)。

So, I store the Code128 Barcode Images as File in a tmp folder and later I inserted those Images from files, by doing this I got the Barcode Image mouse click complete Image select with Help of javaxt.io.Image api. 因此,我将Code128 Barcode图像作为File存储在tmp文件夹中,然后从文件中插入了这些图像,通过这样做,我得到了Barcode Image鼠标,并在javaxt.io.Image api的帮助下单击了完整的图像选择。

Here is the code the time How do I insert the Code128 Barcode Image in PDF - 这是时间代码,我该如何在PDF插入Code128 Barcode Image -

private static void insertBAR(PdfContentByte cb, PdfPTable table, String text, int colspan, com.itextpdf.text.Font font){
    Barcode128 code128 = new Barcode128();
    code128.setBaseline(-1);
    code128.setSize(16f);
    //code128.setBarHeight(16f);
    code128.setCode(text.trim());
    code128.setCodeType(Barcode128.CODE128);
    Image code128Image = code128.createImageWithBarcode(cb, new BaseColor(0, 47, 47), null);

    java.awt.Image awtImage = code128.createAwtImage(new Color(0, 47, 47), Color.WHITE);
    //java.awt.Image awtImage = code128.createAwtImage(Color.BLACK, Color.WHITE);
    //Initialising a 6+ (width and height)size of BufferedImage to put the Barcode Image in center
    BufferedImage bi = new BufferedImage(awtImage.getWidth(null)+6, awtImage.getHeight(null)+6, BufferedImage.TYPE_INT_ARGB);

    Graphics2D gd = bi.createGraphics();
    gd.setColor(Color.WHITE);
    gd.fillRect(0, 0, bi.getWidth(), bi.getHeight());
    //drawing the Barcode Image in center of BufferedImage Rect.
    gd.drawImage(awtImage, 3, 3, null);
    gd.dispose();

    File imgFile = new File(OneMethod.getElementosPath() + "/db/tmp/Img" + (++shortCount) + ".png");

    try {
        javaxt.io.Image img = new javaxt.io.Image(bi);
        img.saveAs(imgFile);
    code128Image = Image.getInstance(imgFile.getAbsolutePath());
    } catch (BadElementException | IOException ex) {
        ex.printStackTrace();
    }

    PdfPCell cell = new PdfPCell();
    cell.setMinimumHeight(35f);
    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setPadding(2f);
    cell.setColspan(colspan);
    cell.addElement(code128Image);
    //cell.setImage(code128Image);
    table.addCell(cell);
}

and now How do I First Extract the Barcode Image using iText and later Retrieve the String values from those Images using zxing api - 现在如何首先使用iText提取Barcode Image然后再使用zxing api从这些图像中检索String值-

private void readAndValidateContents(){
    if(externalLOB.size() == 2){
        try {       
            PdfReader reader = new PdfReader(externalLOB.get(0).getAbsolutePath());     
            PdfReaderContentParser parser = new PdfReaderContentParser(reader);
            //Trying to Extract Images to a tmp folder
            ImageRenderListener listener = new ImageRenderListener(OneMethod.getElementosPath() + "/db/tmp/Img%s.%s");

            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                parser.processContent(i, listener);
            }

            //Retrieving String values from images extracted to tmp folder
            ArrayList<String> ary = OneMethod.decodeBarcodes();

            //Extracting String values from PDF Page using iText API
            String[] contents = (PdfTextExtractor.getTextFromPage(reader, 1)).split("\n");
            externalKey.clear();

            System.out.println("\n\n");

            short line = 0;
            for(String str : contents){
                System.out.println(line++ + str);
            }

            String tempVal = ary.get(0);
            String[] split = tempVal.split("\n");
            tempVal = split[0];

            tempVal = tempVal.substring(0, tempVal.indexOf("."));
            System.out.println("\n\n" + tempVal + "\n\n");

            if(contents[4].equals(tempVal)){ //comparing qr bar code file name with pdf text file name  
                System.out.println("Something XYZ....Here...Done...")
            }else{
                System.err.println("Possibilities for File Interruption, File different of requested...");
            }           
        } catch (StringIndexOutOfBoundsException spiobe){
            System.err.println("File different of requested...");
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (Exception ex){
            ex.printStackTrace();
        } finally{
            OneMethod.clearDirectory(OneMethod.getElementosPath() + "/db/tmp/");
        }
    }
}

class OneMethod {
    static synchronized ArrayList<String> decodeBarcodes(){
        ArrayList<String> ary = new ArrayList<String>();
        short suffix = 1;
        File imageFile = null;

        outer:
        while(true){
            if(++suffix > 5) //Starting to read the Img with Suffix 2 i.e. Img2.jpg
                break outer;
            inner:
            while(true){
                try{
                    InputStream barCodeInputStream;
                    imageFile = new File(OneMethod.getElementosPath() 
                        + "/db/tmp/Img" + suffix + (suffix != 2 ? ".png" : ".jpg"));
                    if(imageFile.exists() && imageFile.isFile()){
                        barCodeInputStream = new FileInputStream(imageFile.getAbsolutePath());
                        BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);

                        //Using zxing-core-3.2.0 with collaborating the old 1.3 version com.google.zxing.client.j2se package inside.
                        //So doing above action you will get the BufferedImageLuminanceSource class which is now not available in new versions.

                        LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
                        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
                        Reader reader = new MultiFormatReader();
                        Result result = reader.decode(bitmap);

                        System.out.println(suffix + " value is : " + result.getText());

                        ary.add(result.getText());
                    }else{
                        System.out.println(imageFile.getAbsolutePath() + " path is not available...");
                    }
                    break inner;
                } catch(FileNotFoundException | NullPointerException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (NotFoundException | ChecksumException | FormatException ex) {
                    ex.printStackTrace();
                }
            }
        }       
        return ary;
    }

    static synchronized void clearDirectory(final String dirPath){
        new Thread(new Runnable(){
            public void run(){
                try{
                    File directory = new File(dirPath);
                    if(directory.isDirectory() == true)
                        try {
                            File[] files = directory.listFiles();
                            for(File file : files){
                                FileUtils.deleteQuietly(file);
                                Thread.sleep(100);
                                if(file.exists())
                                    FileUtils.forceDeleteOnExit(file);
                            }
                        } catch (IOException ioe) {
                            ioe.printStackTrace();
                        }
                }catch(InterruptedException exp){
                    exp.printStackTrace();
                }
            }
        }).start();
    }
}

class ImageRenderListener implements RenderListener { 
    /**
     * Creates a RenderListener that will look for images.
    */
    public ImageRenderListener(String path) {
        this.path = path;
        tempCount = 0;
    }

    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#beginTextBlock()
     */
    public void beginTextBlock() {}

    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#endTextBlock()
     */
    public void endTextBlock() {}

    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#renderImage(
     *     com.itextpdf.text.pdf.parser.ImageRenderInfo)
    */
    public void renderImage(ImageRenderInfo renderInfo) {
    //public void renderImage(InlineImageInfo renderInfo) {
        try {
            String filename;
            FileOutputStream os;
            PdfImageObject image = renderInfo.getImage();
            if (image == null) return;
            filename = String.format(path, ++tempCount, image.getFileType());
            os = new FileOutputStream(filename);
            os.write(image.getImageAsBytes());
            os.flush();
            os.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * @see com.itextpdf.text.pdf.parser.RenderListener#renderText(
     *     com.itextpdf.text.pdf.parser.TextRenderInfo)
    */
    public void renderText(TextRenderInfo renderInfo) {}
}

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

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