File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Blatant Advertising and the fly likes Generate & Recognize Barcodes from PDF Documents in Java Applications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Blatant Advertising
Bookmark "Generate & Recognize Barcodes from PDF Documents in Java Applications" Watch "Generate & Recognize Barcodes from PDF Documents in Java Applications" New topic
Author

Generate & Recognize Barcodes from PDF Documents in Java Applications

sherazam khan
Ranch Hand

Joined: Mar 10, 2010
Posts: 287

This technical tip shows how we can use Aspose.Pdf for Java to recognize barcodes from PDF documents.  First, we will use Aspose.BarCode for Java and generate a barcode image. We will save this image to a file on disk. Then, we will use Aspose.Pdf for Java to create Adobe PDF document and insert this barcode in that document. For the recognition part, we will first extract images from the PDF document using Aspose.Pdf.Kit for Java. Once images are extracted, we will pass these images to Aspose.BarCode for Java for barcode recognition. Below is the complete java program to generate and recognize barcodes from Adobe PDF documents:


[Java]


import com.aspose.barcode.*;

import com.aspose.barcoderecognition.BarCodeReadType;

import com.aspose.barcoderecognition.BarCodeReader;

import com.aspose.pdf.elements.Pdf;

import com.aspose.pdf.elements.Section;

import com.aspose.pdf.kit.License;

import com.aspose.pdf.kit.PdfExtractor;

import java.awt.Toolkit;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;


public class RecognitionFromPdf

{

    public static void main(String[] args)

    {

        try

        {

            // set license

            License licPdfKit = new License();

            FileInputStream stream = new FileInputStream("AsposeLicense.lic");

            licPdfKit.setLicense(stream);


            com.aspose.barcode.License licBarCode = new com.aspose.barcode.License();

            licBarCode.setLicense("AsposeLicense.lic");


            String strPdfDoc = " output1.pdf";

            String strBarCodeImage = "";


            // generate barcode and add to pdf file

            BarCodeBuilder builder = new BarCodeBuilder();

            builder.setSymbologyType(Symbology.Code39Standard);

            builder.setCodeText("test-123");

            String strBarCodeImageSave = " img.jpg";

            builder.save(strBarCodeImageSave);

            //Instantiate a Pdf object by calling its empty constructor

            Pdf pdf1 = new Pdf();

            //Create a section in the Pdf object

            Section sec1 = pdf1.getSections().add();

            //Create an image object in the section

            com.aspose.pdf.elements.Image img1 = new com.aspose.pdf.elements.Image(sec1);

            //Add image object into the Paragraphs collection of the section

            sec1.getParagraphs().add(img1);

            //Set the path of image file

            img1.getImageInfo().setFile(strBarCodeImageSave);

            img1.getImageInfo().setTitle("JPEG image");

            //Save the Pdf

            FileOutputStream out = new FileOutputStream(new File(strPdfDoc));

            pdf1.save(out);


            //Instantiate PdfExtractor object

            PdfExtractor extractor = new PdfExtractor();


            //Bind the input PDF document to extractor

            extractor.bindPdf(strPdfDoc);


            //Extract images from the input PDF document

            extractor.extractImage();

            String suffix = ".jpg";

            int imageCount = 1;

            while (extractor.hasNextImage()) {

                System.out.println("Extracting image " + imageCount);

                strBarCodeImage = "tmpbarcode" + imageCount + suffix;

                extractor.getNextImage(strBarCodeImage);


                // recognize barcode from image

                BarCodeReader reader = new BarCodeReader(strBarCodeImage,BarCodeReadType.getCode39Standard());

                while (reader.read())

                {

                    System.out.println("codetext: " + reader.getCodeText());

                }

                imageCount++;

                reader.close();

            }

        }

        catch(Exception ex)

        {

            System.out.println(ex.getMessage());

        }

    }

}



More about Aspose.BarCode for Java

Contact Information


Aspose Pty Ltd

Suite 163, 79 Longueville Road

Lane Cove, NSW, 2066

Australia

Aspose - Your File Format Experts

sales@aspose.com


Phone: 888.277.6734

Fax: 866.810.9465


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generate & Recognize Barcodes from PDF Documents in Java Applications