How to Easily Read Barcodes in Java: Complete Guide

Java Examples of Reading Barcodes from Image

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

Spire.Barcode for Java is a robust library designed to generate and recognize 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:

Read a single barcode from an image and print out the result.

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:

Read multiple barcodes from one image and print out the results.

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:

Read barcodes from the specified area of an image.

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