Aspose.BarCode for Java is the equivalent component of Aspose.BarCode for .NET, which provides functionalities for generation and recognition of more than 20 types of barcode.
In this tutorial, we will demonstrate how to use Aspose.BarCode for Java to generate a PDF417 barcode image, then read the barcode out of that image.
Barcode Generation
In this tutorial, we are using Eclipse as the IDE, which is free and could be downloaded at:
http://www.eclipse.org/downloads/
And we should download the latest version of Aspose.BarCode for Java at:
http://www.aspose.com/Downloads/Aspose.BarCode/Default.aspx
Step 1
Start Eclipse and create a new Java project:
Figure: New project
Step 2
Input project name as Demo and click finish, we will configure the library later:
Figure: Project name
Step 3
Right click the project we just created and choose properties:
Figure: Project properties
Step 4
Click on Java Build Path item and choose Libraries tab, Click on Add external Jars. In the open file dialog, locate Aspose.BarCode.jar and Servlet-api.jar. They are available at the lib directory of the download package of Aspose.BarCode for Java.
Figure: Add class libraries
Note: You could use your own Servlet-api.jar if it's available.
Step 5
Right click on the Demo project and choose New-> Class on Package Explorer:
Figure: Create a class
Step 6
Let this new class be an Applet and name it Test:
Figure: Class properties
Step 7
Coding, generate a PDF417 barcode:
[Java]
import java.applet.Applet;
import java.awt.*;
import com.aspose.barcode.*;
public class Test extends Applet
{
public void paint(Graphics g)
{
//Instantiate a BarCodeBuilder
BarCodeBuilder b = new BarCodeBuilder();
//Choose Symbology to be PDF417
b.setSymbology(Symbology.PDF417);
//Small module's width to be 1 millimeter
b.setXDimension(1);
//Small module's height to be 4 millimeter
b.setYDimension(4);
//Text to be encoded
b.setCodeText("This is a test.");
//Set up the size of marginal areas
b.setMargins(new Margins(10, 10, 10, 10));
//Draw the barcode image to Graphics g
b.render(g);
}
}
Step 8
Right click this class in package explorer and choose Run As->Java Applet:
Figure: Class properties
Save Image to File
Continuing the sample above, we can add code to save the barcode image to file:
[Java]
try
{
//save as GIF file
b.save("c:\\myPDF417.gif");
//save as JPG file
b.save("c:\\myPDF417.jpg");
//save as BMP file
b.save("c:\\myPDF417.bmp");
}
catch(Exception ex){}
Barcode Recognition
Continuing the sample above, we can add code to scan a barcode from an image:
[Java]
try
{
//Get the image we created, or any other image available
Image img = this.getToolkit().getImage("c:\\myPDF.gif");
//Instantiate a BarCodeReader
BarCodeReader r = new BarCodeReader(img);
//Set up target barcode's symbology
//Use -1 for unknown symbologies
r.setSymbology(Symbology.PDF417);
//Set up target barcode's orientation
//Use -1 for unknown orientations
r.setOrientation(Orientation.East);
//Scan the image for target barcode(s)
BarCodeInfo[] results = r.read();
if(results.length > 0)
{
//barcode found.
//results[0].getCodeText();
}
else
{
//barcode not found.
}
}
catch(Exception ex){}
Note: Evaluation of Aspose.BarCode for Java library only allow Code39 types of barcodes. To test the speed and accuracy of other barcode symbologies, please use our Demo Jar provided in the download package.
Note: If the instantiation of BarCodeReader throws an exception, that’s probably because the image format is not supported, please download the free JAI library to load the image from www.java.net.