Barcode Generation and Recognition Tutorial

This tutorial explains how to use Aspose.BarCode for Java to generate a PDF417 barcode image and then read a barcode from this image.

In the task considered in this tutorial, we use Eclipse as an IDE, which is free and could be downloaded here: http://www.eclipse.org/downloads/

Barcode Generation

  1. Start Eclipse and create a new Java project

    Creating a new Java project

todo:image_alt_text

  1. Call the project Demo and click Finish
    We will configure the library later.

    Naming the project

todo:image_alt_text

  1. Right-click on the project we have just created and select required properties

    Setting project properties

todo:image_alt_text

  1. Click on the Java Build Path item and select the Libraries tab

  2. Click Add external Jars

  3. In the opened file dialog, locate Aspose.BarCode.jar and Servlet-api.jar. They are available in the lib directory of the download package of Aspose.BarCode for Java

    Adding references

todo:image_alt_text

  1. Right-click on the Demo project and select New and then Class at Package Explorer.

    Creating a new class

todo:image_alt_text

  1. Let this new class be an Applet and name it “Test”

    Setting properties for the new class

todo:image_alt_text

  1. Add the code to generate a PDF417 barcode:
import java.applet.Applet;
import java.awt.*;
import java.io.IOException;
import com.aspose.barcode.generation.BarcodeGenerator;
public class Test extends Applet {
public void paint(Graphics g) {
// Instantiate a BarCodeBuilder
BarcodeGenerator b = new BarcodeGenerator(com.aspose.barcode.EncodeTypes.PDF_417);
// Small module's width to be 1 millimeter
b.getParameters().getBarcode().getXDimension().setMillimeters(1);
// Text to be encoded
b.setCodeText("This is a test.");
// Save the barcode to disk
try {
// Save as GIF file
b.save(getCodeBase().getPath() + "myPdf417.gif");
System.out.println("Saved barcode image to " + getCodeBase().getPath() + "barcode.gif");
// Save as JPG file
b.save(getCodeBase().getPath() + "myPdf417.jpg");
System.out.println("Saved barcode image to " + getCodeBase().getPath() + "barcode.jpg");
// Save as BMP file
b.save(getCodeBase().getPath() + "myPdf417.bmp");
System.out.println("Saved barcode image to " + getCodeBase().getPath() + "barcode.bmp");
// Save as PNG file
b.save(getCodeBase().getPath() + "myPdf417.png");
System.out.println("Saved barcode image to " + getCodeBase().getPath() + "barcode.png");
} catch (Exception ex) {
}
// Load and Draw the image on Applet
Image img = getImage(getCodeBase(), "barcode.png");
g.drawImage(img, 0, 0, this);
}
}
  1. Right-click on this class in the package explorer and select Run As and Java Applet

todo:image_alt_text

  1. Save the image to a file. Continuing the sample above, add this code to save the generated barcode image to a file:
try {
// Save as GIF file
b.save(getCodeBase().getPath() + "myPdf417.gif");
System.out.println("Saved barcode image to " + getCodeBase().getPath() + "barcode.gif");
// Save as JPG file
b.save(getCodeBase().getPath() + "myPdf417.jpg");
System.out.println("Saved barcode image to " + getCodeBase().getPath() + "barcode.jpg");
// Save as BMP file
b.save(getCodeBase().getPath() + "myPdf417.bmp");
System.out.println("Saved barcode image to " + getCodeBase().getPath() + "barcode.bmp");
// Save as PNG file
b.save(getCodeBase().getPath() + "myPdf417.png");
System.out.println("Saved barcode image to " + getCodeBase().getPath() + "barcode.png");
} catch (Exception ex) {
}

Barcode Recognition

Continuing the sample above, we can add the code to read a barcode from an image:

// The path to the resource directory.
String dataDir = Utils.getDataDir(Barcode_Recognition.class) + "BarcodeReader/basic_features/";
// Initialize barcode reader
BarCodeReader reader = new BarCodeReader(dataDir + "CodeText.jpg");
// read barcode of type Code39Extended
for (BarCodeResult result : reader.readBarCodes()) {
System.out.println("CodeText: " + result.getCodeText());
System.out.println("Symbology type: " + result.getCodeType());
}

The evaluation version of Aspose.BarCode for Java allows decoding only Code 39 barcodes without limitations. To test the reading speed and accuracy for other symbologies, please use the demo JAR provided in the download package.