Recognizing Barcodes from PDF in Java

Introduction

Welcome to our step-by-step guide on recognizing barcodes from PDFs using Aspose.BarCode for Java. Barcodes play a crucial role in data management and organization, and with Aspose.BarCode, the process becomes seamless. In this tutorial, we will walk you through the entire process, from setting up the necessary prerequisites to implementing the code for barcode recognition in a PDF file.

Prerequisites

Before diving into the tutorial, make sure you have the following prerequisites in place:

  1. Aspose.BarCode for Java License: Obtain and set up a valid license for Aspose.BarCode for Java. You can acquire a license from Aspose Purchase.

  2. Aspose.PDF License: Additionally, set up a license for Aspose.PDF, which is required for working with PDF files. You can apply for a license here.

  3. Download Aspose.BarCode for Java: Download the Aspose.BarCode library from here.

Now that you have the necessary prerequisites, let’s proceed to import the required packages and begin our tutorial.

Import Packages

In your Java project, include the Aspose.BarCode and Aspose.PDF packages. Here’s a sample code snippet to get you started:

import com.aspose.barcode.*;
import com.aspose.barcode.License;
import com.aspose.barcode.barcoderecognition.BarCodeReader;
import com.aspose.barcode.barcoderecognition.BarCodeResult;
import com.aspose.barcode.barcoderecognition.BaseDecodeType;
import com.aspose.barcode.barcoderecognition.DecodeType;
import com.aspose.pdf.*;
import com.aspose.pdf.facades.PdfExtractor;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

Step 1: Generate Barcode and Add to PDF

BarcodeGenerator builder = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.CODE_39_STANDARD);
builder.setCodeText("test123");
String strBarCodeImageSave = dataDir + "input_image1.jpg";
builder.save(strBarCodeImageSave);

Step 2: Create PDF and Add Barcode Image

Document pdf1 = new Document();
Page page = pdf1.getPages().add();
BufferedImage originalImage = ImageIO.read(new File(strBarCodeImageSave));
page.getResources().getImages().add(originalImage);
pdf1.save(strPdfDoc);

Step 3: Extract Images from PDF

PdfExtractor extractor = new PdfExtractor();
extractor.bindPdf(strPdfDoc);
extractor.extractImage();

Step 4: Recognize Barcode from Extracted Images

String suffix = ".jpg";
int imageCount = 1;

while (extractor.hasNextImage()) {
    System.out.println("Extracting image " + imageCount);
    strBarCodeImage = "tmpbarcode" + imageCount + suffix;
    extractor.getNextImage(strBarCodeImage);

    BarCodeReader reader = new BarCodeReader(strBarCodeImage, DecodeType.CODE_39_EXTENDED);

    for (BarCodeResult result : reader.readBarCodes()) {
        System.out.println("CodeText: " + result.getCodeText());
        System.out.println("Symbology type: " + result.getCodeType());
    }

    imageCount++;
}

Repeat these steps as needed, adjusting filenames and paths accordingly.

Conclusion

Congratulations! You have successfully learned how to recognize barcodes from PDFs using Aspose.BarCode for Java. This tutorial aimed to provide a comprehensive guide while maintaining simplicity and clarity. Feel free to explore more features and functionalities offered by Aspose.BarCode in the documentation.

Frequently Asked Questions (FAQs)

Q: Can I use Aspose.BarCode for Java without a license?

While Aspose.BarCode can be used without a license, it is recommended to obtain one for full functionality and to comply with licensing terms.

Q: How can I get a temporary license for Aspose.BarCode for Java?

You can obtain a temporary license here.

Q: Are there any limitations on the barcode types supported by Aspose.BarCode?

Aspose.BarCode supports a wide range of barcode types. Refer to the documentation for a complete list.

Q: Is there a trial version available for Aspose.BarCode for Java?

Yes, you can download the trial version from here.

Q: Where can I seek support or ask questions about Aspose.BarCode for Java?

Visit the Aspose.BarCode forum for support and discussions.