How to Scan Barcodes and QR Codes in Linux with Java Using ZBar

How to Read Barcode and QR Code in Linux Using ZBar with Java

Scanning barcodes and QR codes is essential in various applications, from inventory management to web link sharing. This guide demonstrates how to read barcodes and QR codes on Linux using ZBar, a widely-used open-source tool, combined with Java for automated processing.

Why Choose ZBar for Barcode and QR Code Scanning?

ZBar supports a wide range of barcode formats, such as QR codes, EAN, UPC, and Code 128, and can process static images and real-time video streams. In this article, we’ll install ZBar, use its command-line tool, and integrate it with Java to automate the scanning process.

Step 1: Install ZBar on Linux

Install ZBar on your Linux system by following these simple steps:

For Debian/Ubuntu-based Systems:

sudo apt update
sudo apt install zbar-tools

For Fedora/RHEL-based Systems:

sudo dnf install zbar

Step 2: Verify the Installation

After installation, you can verify that ZBar is installed correctly by running:

zbarimg --version

If ZBar is installed successfully, it will display the version information.

Step 3: Scanning a Barcode or QR Code Image with ZBar

To test ZBar, use the following command to scan a barcode or QR code from an image. Let’s say you have a file named sample-qr.png that contains a QR code.

zbarimg -q --raw sample-qr.png

This command outputs the data encoded in the QR code.

Step 4: Integrate ZBar with Java to Read Barcodes and QR Codes

Integrating ZBar with Java enables automated barcode and QR code reading within a Java application. We’ll use Apache Commons Exec to execute the zbarimg command from Java and capture the output. Ensure zbarimg is in your system’s PATH, so Java can access it.

Java Code for Barcode and QR Code Scanning

Here’s a Java method that reads barcode and QR code data from an image using ZBar:


import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.Executor;
import org.apache.commons.exec.PumpStreamHandler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class BarcodeReader {

    public static String[] readBarCode(String src) {
        // Initialize command and output stream
        CommandLine cmdLine = new CommandLine("zbarimg");
        ByteArrayOutputStream stdout = new ByteArrayOutputStream();
        
        // Set up the command arguments
        cmdLine.addArgument("-q");  // Quiet mode
        cmdLine.addArgument("--raw");  // Output raw data
        cmdLine.addArgument(src);  // Source file path

        // Set up executor and stream handler
        Executor executor = new DefaultExecutor();
        executor.setStreamHandler(new PumpStreamHandler(stdout));
        executor.setWatchdog(new ExecuteWatchdog(60 * 1000));  // 1-minute timeout

        // Execute the command
        try {
            executor.execute(cmdLine);
        } catch (ExecuteException e) {
            System.err.println("Command execution failed: " + e.getMessage());
        } catch (IOException e) {
            System.err.println("I/O error during command execution: " + e.getMessage());
            return new String[0];
        }

        // Split and return the output as lines
        return stdout.toString().split("\n");
    }

    public static void main(String[] args) {
        String src = "sample-qr.png"; 

        // Run the barcode reader and print the result
        String[] result = readBarCode(src);
        if (result.length > 0) {
            for (String line : result) {
                System.out.println("Barcode/QR Data: " + line);
            }
        } else {
            System.out.println("No barcode or QR code detected.");
        }
    }
}

This Java program sets up a command-line execution environment, runs zbarimg on the specified image, and captures the output. The results are printed, showing the barcode or QR code data detected in the image.

Conclusion

With ZBar, you can easily read barcodes and QR codes in Linux. By combining ZBar with Java, as shown above, you can build applications that process barcodes and QR codes automatically. Try this setup, and enhance your Java applications with reliable barcode reading capabilities!

No comments:

Post a Comment