How to Create Barcode Using Spire.Barcode for Java

This article presents how to import Spire.Barcode.jar in Java application and create barcode image using the classes involved.

Import Spire.Barcode.jar file

Step 1: Download Spire.Barcode for Java from the following URL. Unzip the package into a directory, you'll get Spire.Barcode.jar and Spire.Common.jar in the lib folder.

Step 2: Create a Java project in Eclipse.

Step 3: Right click the project name, select "New" - "Folder" to create a folder named "Lib".

How to Create Barcode Using Spire.Barcode for Java

Step 4: Copy the Spire.Barcode.jar file and Spire.Common.jar file to this folder.

How to Create Barcode Using Spire.Barcode for Java

Step 5: Select all the jar files, then right click on one of them and select "Build Path" – "Add to Build Path".

How to Create Barcode Using Spire.Barcode for Java

Until now, Spire.Barcode.jar and Spire.Common.jar have been referenced in your Java project. Expand the specific jar file in the “Package Explore” pane, and you’ll be able to view the classes, methods, properties, etc. inside the jar file.

How to Create Barcode Using Spire.Barcode for Java

Using the code

Following code snippet provides an example of creating Code 128 barcode using Spire.Barcode for Java.

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;

public class CODE_128 {

	public static void main(String[] args) throws IOException {
		
		//create an instance of BarcodeSetteings
        BarcodeSettings settings = new BarcodeSettings();
        //set barcode type
        settings.setType(BarCodeType.CODE_128);       
        //set barcode data
        settings.setData("123456789");
        //set the display text
        settings.setData2D("123456789");     
        //show text on bottom
        settings.setShowTextOnBottom(true);
        //set the border invisible
        settings.hasBorder(false);
        //create BarCodeGenerator object based on settings
        BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
        //generate image data and store in BufferedImage instance
        BufferedImage bufferedImage = barCodeGenerator.generateImage();
        //save to .png file format
        ImageIO.write(bufferedImage, "png", new File("CODE128.png"));
        System.out.println("Complete!");
	}
}

Output:

How to Create Barcode Using Spire.Barcode for Java