Getting Started (4)

Barcodes are widely used in various industries for inventory management, retail, logistics, and more. Reading barcodes efficiently is crucial for automating data entry and improving accuracy. In Java, one of the most reliable libraries for barcode recognition is Spire.Barcode for Java. This article provides a comprehensive guide on how to read barcode in Java using this powerful library.
Table of Contents
- Introduction to Spire.Barcode for Java
- Key Features of Spire.Barcode
- Setting Up Spire.Barcode in Your Java Project
- Reading a Single Barcode from an Image File
- Reading Multiple Barcodes from One Image
- Customizing Barcode Recognition Settings
- Conclusion
- FAQs
Introduction to Spire.Barcode for Java
Spire.Barcode for Java is a robust library designed to generate and read barcodes in Java applications. It supports a wide range of barcode symbologies, including:
- 1D Barcodes : Code 128, Code 39, EAN-13, UPC-A, etc.
- 2D Barcodes : QR Code, DataMatrix, PDF417, etc.
Spire.Barcode provides fast and precise barcode recognition in Java, whether scanning from a dedicated barcode image or a complex image containing additional elements.
Key Features of Spire.Barcode
Before diving into implementation, let’s explore some key features of Spire.Barcode:
- Multi-Format Support : Read barcodes from PNG, JPG, BMP, GIF, and TIFF images.
- Batch Processing : Scan multiple barcodes in a single image.
- High Recognition Accuracy : Advanced algorithms ensure reliable barcode detection.
- Customizable Settings : Adjust scan regions and barcode types for optimized recognition.
- Cross-Platform Compatibility : Works seamlessly on Windows, Linux, and macOS.
These features make Spire.Barcode an excellent choice for enterprise-level barcode processing.
Setting Up Spire.Barcode in Your Java Project
To start reading barcodes in Java, you need to integrate Spire.Barcode into your project. Follow these steps:
Step 1: Install the Library
If you're using Maven, you can easily integrate Spire.Barcode by adding the following dependency to your pom.xml file:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.barcode</artifactId>
<version>5.1.11</version>
</dependency>
For manual setup, download Spire.Barcode for Java from our website and add the downloaded .jar file to your project’s build path.
Step 2: Get a Temporary License
Spire.Barcode requires a license to read certain barcode types. To unlock full barcode recognition capabilities, get a free 30-day trial license. After receiving the license file, apply it using this code:
LicenseProvider.setLicenseKey("your license key");
Now, you're ready to read barcode in Java using Spire.Barcode.
Reading a Single Barcode from an Image File
Reading one barcode from an image is a fundamental scenario, and Spire.Barcode facilitates this with just a few lines of code.
Here’s a step-by-step example:
import com.spire.barcode.BarcodeScanner;
import com.spire.barcode.license.LicenseProvider;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ReadBarcode {
public static void main(String[] args) throws IOException {
// Apply license key to remove restrictions on barcode types
LicenseProvider.setLicenseKey("your license key");
// Load the image file containing the barcode
BufferedImage bufferedImage = ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\barcode.jpg "));
// Scan the barcode from the loaded image
String result = BarcodeScanner.scanOne(bufferedImage);
// Output the scanned barcode result
System.out.print(result);
}
}
Explanation
- ImageIO.read() loads the image file, supporting extensions such as .png, .jpeg, .bmp, or .gif.
- BarcodeScanner.scanOne() detects and decodes the barcode from the image.
- The decoded result is stored in a String .
Note
The scanOne() method and the scan() method, which will be discussed later, can accept not only a BufferedImage as a parameter but also an InputStream and a String representing the image file path. Whether you're processing images from disk, user uploads, or real-time streams, this flexibility simplifies integration into diverse workflows.
Output:

Reading Multiple Barcodes from One Image
Spire.Barcode can detect and decode multiple barcodes in a single image. Here’s how:
import com.spire.barcode.BarcodeScanner;
import com.spire.barcode.license.LicenseProvider;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
public class ReadMultipleBarcodes {
public static void main(String[] args) throws IOException {
// Apply license key to remove restrictions on barcode types
LicenseProvider.setLicenseKey("your license key");
// Load the image file containing the barcode
BufferedImage bufferedImage = ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\barcodes.jpg"));
// Scan the barcode from the loaded image
String[] results = BarcodeScanner.scan(bufferedImage);
// Output the results
System.out.println(Arrays.toString(results));
}
}
Explanation
- BarcodeScanner.scan() identifies and decodes all the barcodes present in the image.
- The results are stored in a String array .
Output:

Customizing Barcode Recognition Settings
For improved accuracy, Spire.Barcode allows you to customize scan settings, such as defining a scan region or specifying a particular barcode type. This enhanced approach ensures you have the flexibility and control needed for effective barcode scanning in Java.
Here is an example:
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeScanner;
import com.spire.barcode.license.LicenseProvider;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class CustomScanSettings {
public static void main(String[] args) throws IOException {
// Apply license key to remove restrictions on barcode types
LicenseProvider.setLicenseKey("your license key");
// Load the image file containing the barcode
BufferedImage bufferedImage = ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\barcodes.jpg"));
// Define a rectangle area for barcode recognition
Rectangle rectangle = new Rectangle(0,0,380,270);
// Scan the barcode from the loaded image
String[] results = BarcodeScanner.scan(bufferedImage, rectangle, BarCodeType.Code_93);
// Output the first result
System.out.print(results[0]);
}
}
Explanation
- Rectangle() defines a specific area within the image for barcode recognition.
- BarCodeType enumeration allows you to specify the barcode type for more accurate detection.
Output:

Conclusion
In this article, we've explored the essential steps to set up Spire.Barcode in your Java project, read barcodes from images , handle multiple barcodes , and customize recognition settings for optimal accuracy. By leveraging these capabilities, developers can seamlessly integrate barcode scanning into their applications, improving data entry automation and reducing errors.
With Spire.Barcode, you have a reliable tool at your disposal to meet your barcode reading needs, paving the way for more efficient business operations.
FAQs
Q1. What types of barcodes can Spire.Barcode read?
Spire.Barcode supports 38+ barcode types, including both 1D barcodes like Code 128, Code 39, EAN-13, and UPC-A, as well as 2D barcodes such as QR Code, DataMatrix, and PDF417.
Q2. Can I customize the barcode scanning region?
Yes, Spire.Barcode allows you to define a specific scan area within the image by using a Rectangle object. This feature helps improve accuracy by focusing on a designated section of the image.
Q3. Can Spire.Barcode read multiple barcodes from a single image?
Yes! Using BarcodeScanner.scan(), you can detect and decode multiple barcodes in one image efficiently.
Q4. Is a license required to use Spire.Barcode for barcode recognition?
A commercial license is needed for full functionality, but you can get a free 30-day trial license to test all features before purchasing.
Q5. Can I use Spire.Barcode for Java to create barcodes?
Yes, Spire.Barcode supports generating over 38 commonly used 1D and 2D barcodes. For more information, check out: How to Create Barcode in Java
Invisiblize Textual Data When Creating Barcode&QR code in Java
2021-07-02 02:27:06 Written by KoohjiIn this tutorial, we are going to introduce how to invisiblize the textual data in the middle of creating a Barcode&QR code using Spire.Barcode for Java and its settings.setShowText(false); method.
Invisiblize the Textual Data When Creating a Barcode
import com.spire.barcode.BarCodeGenerator;
import com.spire.barcode.BarCodeType;
import com.spire.barcode.BarcodeSettings;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class CreateBarcode {
public static void main(String[] args) throws IOException {
BarcodeSettings settings = new BarcodeSettings();//Instantiate BarcodeSettings object
settings.setType(BarCodeType.Codabar);//Set the Barcode type
settings.setData("123456789");//Set the Barcode data
settings.setShowText(false);//Make the textual data invisible
settings.setShowTextOnBottom(true);//Make the textual data visible at the bottom of the Barcode
settings.setX(0.8f);//Set width for the black and white stripes
settings.setImageHeight(50);//Set height for the created Barcode image
settings.setImageWidth(70);//Set width for the created Barcode image
settings.hasBorder(true);//Make the border visible
settings.setBorderColor(new Color(135,206,250));//Set the border color
settings.setBorderWidth(1);//Set the border width
settings.setBackColor(new Color(240,255,255));//Set background color for the Barcode
BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);//Instantiage BarCodeGenerator object
BufferedImage bufferedImage = barCodeGenerator.generateImage();//Generate image data based on settings and save it to BufferedImage instance
ImageIO.write(bufferedImage, "png", new File("Codabar.png"));//Save the Barcode as PNG
}
}
Output

Invisiblize the Textual Data When Creating a QR code
import com.spire.barcode.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class CreateQR {
public static void main(String[] args) throws IOException {
BarcodeSettings settings = new BarcodeSettings();//Instantiate BarcodeSettings object
settings.setType(BarCodeType.QR_Code);//Set Barcode type as QR code
settings.setData("Hello 123456789");//Set the data
settings.setShowText(false);// Make the textual data invisible
settings.setQRCodeDataMode(QRCodeDataMode.Alpha_Number);//Set data mode
settings.setImageWidth(50);// Set widthfor the created QR code image
settings.setImageHeight(50);// Set height for the created QR code image
settings.setX(3.0f);//Set width for QR code model
settings.setQRCodeECL(QRCodeECL.H);//Set QR code ECL
settings.hasBorder(false);//make the border invisible
BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);//Instantiage BarCodeGenerator object
BufferedImage bufferedImage = barCodeGenerator.generateImage();//Generate image data based on settings and save it to BufferedImage instance
ImageIO.write(bufferedImage, "png", new File("QRCode.png"));//Save the QR code as PNG
}
}
Output


QR codes have become an integral part of modern digital interactions, enabling seamless data sharing through scannable patterns. If you’re looking to generate QR codes in Java, the Spire.Barcode for Java library offers a robust, user-friendly solution.
This comprehensive guide will walk you through every aspect of creating QR code in Java, including setup, basic QR code generation, logo integration, advanced customization, and industry best practices.
- Java Barcode Generator Library
- How to Generate a QR Code in Java
- Generate a QR Code with a Logo in Java
- Customize QR Code Options
- Best Practices for QR Code Generation
- Conclusion
Java Barcode Generator Library
Spire.Barcode for Java is a lightweight, high-performance library designed to generate and read 1D and 2D barcodes, including QR codes, Data Matrix, Code 128, and more. It stands out for its:
- Ease of use: Minimal code required to generate barcodes.
- Customization options: Adjust size, color, error correction, and add logos.
- Compatibility: Supports Java SE 6.0 and above, and integrates with popular IDEs like IntelliJ and Eclipse.
- Output flexibility: Export QR codes to common image formats (PNG, JPG, BMP, etc.).
Spire.Barcode simplifies QR code generation with pre-built classes for configuration and rendering. Download it to manually import or add it to your project using Maven:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.barcode</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
Tip: Request a free trial license here to remove evaluation message and watermarks.
You can also use the Free Version to process barcodes:
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.barcode.free</artifactId>
<version>5.2.1</version>
</dependency>
How to Generate a QR Code in Java
Creating a standard QR code with Spire.Barcode for Java involves just a few steps. This example generates a QR code that encodes a URL and saves it as a PNG image.
Key Components:
- BarcodeSettings: Configures all properties of the QR code.
- setType(): Specifies the QR code type (other types like Code 128 are also supported).
- setData(): Defines the content to encode (URL, text, numbers, etc.).
- setX(): Sets the width of each "module" (the tiny squares in the QR code).
- setQRCodeECL(): Defines error correction level.
- generateImage(): Uses the settings to render the QR code as a BufferedImage.
- ImageIO.write(): Saves the image to a file (PNG, JPG, or other formats).
Java code to create a QR code:
import com.spire.barcode.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class GenerateQRcode {
public static void main(String[] args) throws IOException {
// Create a BarcodeSettings object
BarcodeSettings settings = new BarcodeSettings();
// Set the barcode type to QR Code
settings.setType(BarCodeType.QR_Code);
// Set the QR code data
settings.setData("https://www.e-iceblue.com/");
// Set to not display the encoded text (the URL)
settings.setShowText(false);
// Set width of the barcode module
settings.setX(2);
// Set the error correction level
settings.setQRCodeECL(QRCodeECL.M);
// Set the data mode
settings.setQRCodeDataMode(QRCodeDataMode.Auto);
// Create BarCodeGenerator object based on settings
BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
// Generates QR code as a BufferedImage (in-memory image object)
BufferedImage bufferedImage = barCodeGenerator.generateImage();
// Save the generated QR code image in PNG format
ImageIO.write(bufferedImage,"png",new File("QR_Code.png"));
}
}
Output: A basic QR code image in PNG format. When scanned, the user will be directed to the specified URL address.

Integrate with the Spire.PDF for Java library to add the QR code image to a PDF document.
Generate a QR Code with a Logo in Java
Adding a logo to a QR code enhances brand visibility while maintaining scannability. Spire.Barcode for Java simplifies this by allowing you to overlay an image onto the QR code via the setQRCodeLogoImage() method.
Key Considerations:
- Use a small logo (10-20% of QR code size) to avoid disrupting data patterns.
- Ensure high contrast between the logo and QR code background.
Java code to generate a QR code with embedded logo:
import com.spire.barcode.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class QRcodeWithLogo {
public static void main(String[] args) throws IOException {
// Create a BarcodeSettings object
BarcodeSettings settings = new BarcodeSettings();
// Set the barcode type to QR Code
settings.setType(BarCodeType.QR_Code);
// Set the QR code data (URL, text, numbers, etc.)
settings.setData("https://www.e-iceblue.com/");
// Set to not display the encoded text (the URL)
settings.setShowText(false);
// Set width of the barcode module
settings.setX(3);
// Set the error correction level
settings.setQRCodeECL(QRCodeECL.H);
// Set the data mode
settings.setQRCodeDataMode(QRCodeDataMode.Auto);
// Load an image and set it as the logo image for the QR code
BufferedImage image = ImageIO.read(new File("logo.png"));
settings.setQRCodeLogoImage(image);
// Create BarCodeGenerator object based on settings
BarCodeGenerator barCodeGenerator = new BarCodeGenerator(settings);
// Generates QR code as a BufferedImage (in-memory image object)
BufferedImage bufferedImage = barCodeGenerator.generateImage();
// Save the generated QR code image in PNG format
ImageIO.write(bufferedImage,"png",new File("QrCodeWithLogo.png"));
}
}
Output: A PNG image file containing a scannable QR code with a centered logo.

Need to scan the QR code in Java? Refer to: How to Read Barcodes in Java Using Spire.Bacode
Customize QR Code Options
Spire.Barcode for Java lets you tailor QR codes to match your design requirements. Below are key customization options:
1. Add Display Text (Top/Bottom Labels)
Add descriptive text above or below the QR code and set its formatting (font, color, alignment).
// Enable top and bottom text
settings.setShowTopText(true);
settings.isShowBottomText(true);
// Set text content
settings.setTopText("Scan to Visit Our Site");
settings.setBottomText("Example Corp © 2025");
// Customize text appearance
settings.setTopTextFont("Arial", 14.0f);
settings.setTopTextColor(Color.BLUE);
settings.setTopTextMargin(1.0f);
settings.setBottomTextFont("Calibri", 12.0f, FontStyle.Italic);
settings.setBottomTextColor(Color.GRAY);
// Align text (Near, Center, Far)
settings.setTopTextAligment(StringAlignment.Center);
settings.setBottomTextAlignment(StringAlignment.Far);

2. Customize Borders & Quiet Zones
The "quiet zone" is a blank (usually white) area surrounding the QR code (and its border). It’s critical for scanners to recognize where the QR code starts and ends.
Spire.Barcode for Java allows you to set both the visible border and quiet zone:
// Enable border
settings.hasBorder(true);
// Set border properties
settings.setBorderColor(Color.BLUE); // Border color
settings.setBorderWidth(1); // Border thickness
settings.setBorderDashStyle(3); // Dashed border (options: Solid, Dash, Dot, etc.)
// Set border margins
// These margins create the "quiet zone" around the QR code
settings.setLeftMargin(8);
settings.setRightMargin(8);
settings.setTopMargin(8);
settings.setBottomMargin(8);

3. Change Colors
Modify the QR code’s foreground (data) and background colors to match your brand:
// Set foreground color (black by default)
settings.setForeColor(Color.GRAY);
// Set background color (white by default)
settings.setBackColor(new Color(250, 243, 202));

Best Practices for QR Code Generation
Follow these guidelines for professional QR implementations:
- Error Correction Level: Choose wisely:
- L (Low): 7% data recovery
- M (Medium): 15% recovery
- Q (Quartile): 25% recovery
- H (High): 30% recovery (best for logos)
- Optimal Size: Minimum 22 cm (0.8x0.8 in) for print, 200200 px for digital
- Contrast: Ensure high contrast between code and background
- Test Scannability: Always verify your QR codes with multiple devices/ scanner apps
Conclusion
Generating QR codes in Java is straightforward with libraries like Spire.Barcode. Whether you need a basic QR code encoding text/URL, a branded code with a logo, or a fully customized design, this library provides the tools to achieve it efficiently. By following the examples and best practices outlined in this guide, you’ll be able to integrate professional-grade QR codes into your Java applications, enhancing user experience and enabling seamless data sharing.
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".

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

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

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.

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:
