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

时间:2016-03-15 08:54:57

标签: java image pdf itext barcode

我通过分别插入2种条形码QRCode和Code128来创建PDF。

现在我的问题是如何首先提取条形码图像,然后从这些图像中提取文本, 请帮我这个,我尝试了谷歌2天,但没有找到合适的方式。

我正在使用itextpdf-5.5.8版本。

2 个答案:

答案 0 :(得分:1)

我也在研究一种类似的项目,但它在.NET中。我正在为第二个问题提供一个可选的解决方案,即从图像中读取条形码。您可以使用 ZXing 来解码条形码。下面是伪代码,你必须在函数中传递图像路径:

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");
    }
}

答案 1 :(得分:0)

以前我评论Code128 Barcode Image的一个技巧是没有让鼠标点击选择pdf,作为QRCode。

所以,我将Code128 Barcode图像存储为File在tmp文件夹中,之后我从文件中插入了这些图像,通过这样做,我得到Barcode Image鼠标点击完成图像选择javaxt.io.Image api。

的帮助

以下是代码时间如何在Code128 Barcode Image中插入PDF -

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);
}

现在如何首先使用Barcode Image提取iText以及稍后使用String api从这些图片中检索zxing

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) {}
}