Building Java Barcode QR Code Reading Apps: Dynamsoft vs. ZXing

In a previous article, we explored how to create a command-line barcode and QR code scanning application using Java and the Dynamsoft Barcode Reader. This article will delve into building more sophisticated applications, such as a desktop GUI application and a web application. Additionally, we will integrate the ZXing SDK to compare it with the Dynamsoft Barcode Reader.

Prerequisites

Maven Configuration

Set up the dependencies in your pom.xml file:

<repositories>
        <repository>
            <id>dbr</id>
            <url>https://download2.dynamsoft.com/maven/dbr/jar</url>
        </repository>
</repositories>
<dependencies>
        <dependency>
            <groupId>com.dynamsoft</groupId>
            <artifactId>dbr</artifactId>
            <version>9.6.40.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.4.0</version>
          </dependency>
</dependencies>

Test Image

The test image contains multiple barcode and QR code.

multiple barcode image

A Variety of Java Applications for Scanning Barcodes and QR Codes

Let’s explore how ZXing and Dynamsoft Barcode Reader perform in command-line, GUI, and web applications.

Command-Line Application

Create a new Maven project:

mvn archetype:generate -DgroupId=com.java.barcode -DartifactId=app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

Import ZXing and Dynamsoft Barcode Reader:

import com.dynamsoft.dbr.*;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.*;

Decode the image file with ImageIO and BufferedImage:

import java.awt.image.*;
import javax.imageio.ImageIO;

BufferedImage image = null;
try {
    image = ImageIO.read(new File(filename));
} catch (IOException e) {
    System.out.println(e);
    return;
}

To read multiple barcodes encoded with different barcode symbologies in ZXing, use MultiFormatReader and GenericMultipleBarcodeReader. Convert the BufferedImage to BinaryBitmap:

BinaryBitmap bitmap = null;
int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
RGBLuminanceSource source = new RGBLuminanceSource(image.getWidth(), image.getHeight(), pixels);
bitmap = new BinaryBitmap(new HybridBinarizer(source));
            
MultiFormatReader reader = new MultiFormatReader();  
GenericMultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
try {
    Result[] zxingResults = multiReader.decodeMultiple(bitmap);
} catch (NotFoundException e) {
    e.printStackTrace();
}
pixels = null;
bitmap = null;

In contrast, using Dynamsoft Barcode Reader API is simpler, as it supports BufferedImage as the input parameter:

BarcodeReader br = null;
try {
    BarcodeReader.initLicense("LICENSE-KEY");
    br = new BarcodeReader();
} catch (Exception e) {
    System.out.println(e);
    return;
}

TextResult[] results = null;
try {
    results = br.decodeBufferedImage(image, "");
} catch (Exception e) {
    System.out.println("decode buffered image: " + e);
}

To run the Java program conveniently in the terminal, assemble all dependencies into one jar file using the maven-assembly-plugin:

<build>
<plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>com.java.barcode.App</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
</build>

Build the Maven project and run the application:

mvn clean package
java -jar target/test-1.0-SNAPSHOT-jar-with-dependencies.jar <image-file>

java barcode command line

ZXing returns 8 results, whereas Dynamsoft Barcode Reader returns 18 results.

Desktop GUI Application Built with Java Swing

Based on the command-line Java program, we can add a Swing class to convert it into a GUI app.

Import the relevant classes:

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

The widgets we need include JTextArea, JButton, JFileChooser and JComboBox:

  • JTextArea: Display the results.
  • JButton: Trigger the click event.
  • JFileChooser: Select an image file from the disk drive.
  • JComboBox: Toggle between ZXing and Dynamsoft Barcode Reader.

Initialize the layout with all the widgets:

public App() {
    super(new BorderLayout());
    
    mFileChooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
            ".png", "png");
    mFileChooser.setFileFilter(filter);
    mLoad = new JButton("Load File");
    mLoad.addActionListener(this);
    
    mSourceList = new JComboBox(new String[]{"ZXing", "Dynamsoft"});
    mSourceList.setSelectedIndex(0);
    
    JPanel buttonPanel = new JPanel(); 
    buttonPanel.add(mSourceList);
    buttonPanel.add(mLoad);
    add(buttonPanel, BorderLayout.PAGE_START);
    
    mTextArea = new JTextArea();
    mTextArea.setSize(480, 480);
    JScrollPane sp = new JScrollPane(mTextArea); 
    add(sp, BorderLayout.CENTER);
}

Handle the button click event to load an image for barcode and QR code recognition:

public void actionPerformed(ActionEvent e) {

    int returnVal = mFileChooser.showOpenDialog(App.this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = mFileChooser.getSelectedFile();     
        String filename = file.toPath().toString();          
        if (mSourceList.getSelectedItem().toString().equals("Dynamsoft")) {
            TextResult[] results = decodefileDynamsoft(filename);
        }
        else {
            Result[] results = decodefileZXing(filename);
        }
    } 
}

Build and run the Java GUI application:

mvc clean package
java -jar target/test-1.0-SNAPSHOT-jar-with-dependencies.jar

java barcode gui

You can switch between ZXing and Dynamsoft Barcode Reader using the drop-down list.

Web Application Built with Java Spring Boot

We use Spring Boot to develop a web application.

To quickly test the server-side Java barcode API, integrate swagger-ui into the Spring Boot app by adding the following dependencies:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-core</artifactId>
    <version>1.1.45</version>
    <exclusions>
        <exclusion>
            <groupId>io.github.classgraph</groupId>
            <artifactId>classgraph</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.1.45</version>
</dependency>

Create a controller with POST mappings for ZXing and Dynamsoft Barcode Reader:

@RestController
public class BarcodeController {

    private DynamsoftBarcode mDynamsoftBarcode;
    private ZXingBarcode mZXingBarcode;

    @Autowired
    public BarcodeController(DynamsoftBarcode dynamsoft, ZXingBarcode zxing) 
    {
        mDynamsoftBarcode = dynamsoft;
        mZXingBarcode = zxing;
    }

    @PostMapping(value = "/api/dynamsoft"
            , consumes = MediaType.MULTIPART_FORM_DATA_VALUE
            , produces = MediaType.APPLICATION_JSON_VALUE)
    public BarcodeResponse getDynamsoft(@RequestPart MultipartFile file) throws Exception {
        return mDynamsoftBarcode.decode(file.getOriginalFilename(), file.getInputStream());
    }

    @PostMapping(value = "/api/zxing"
            , consumes = MediaType.MULTIPART_FORM_DATA_VALUE
            , produces = MediaType.APPLICATION_JSON_VALUE)
    public BarcodeResponse getZXing(@RequestPart MultipartFile file) throws Exception {
        return mZXingBarcode.decode(file.getOriginalFilename(), file.getInputStream());
    }
}

Build and run the Spring Boot app:

mvc clean package
java -jar target/web-1.0-SNAPSHOT.jar

Now, visit http://localhost:8080/swagger-ui.html to test ZXing and Dynamsoft Barcode Reader via POST requests.

java barcode spring boot

Source Code

https://github.com/yushulx/java-jni-barcode-qrcode-reader/tree/main/examples/9.x/zxing_dynamsoft