Java Command-Line, GUI and Web Apps for Scanning Barcode and QR Code

In previous article, we discussed how to construct a command-line barcode and QR code scanning application using Java and Dynamsoft Barcode Reader. In this article, we will create more fancy applications, such as a desktop GUI application and a web application. In addition, we will import ZXing SDK to make a comparison with Dynamsoft Barcode Reader.

SDK Version

Maven Configuration

Configure the dependencies in the pom.xml file:


Test Image

The test image contains multiple barcode and QR code.

multiple barcode image

A Variety of Java Applications for Scanning Barcode and QR Code

Let’s see 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 -DartifactId=app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

Import ZXing and Dynamsoft Barcode Reader:

import com.dynamsoft.dbr.*;

Decode the image file with ImageIO and BufferedImage:

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

BufferedImage image = null;
try {
    image = File(filename));
} catch (IOException e) {

To read multiple barcodes encoded with different barcode symbologies in ZXing, we need to use MultiFormatReader and GenericMultipleBarcodeReader. The BufferedImage needs to be converted 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) {
pixels = null;
bitmap = null;

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

BarcodeReader br = null;
try {
    br = new BarcodeReader("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==");
} catch (Exception e) {

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 terminal, we assemble all dependencies into one jar file by using the maven-assembly-plugin:


Build the Maven project and run the application:

mvn clean install assembly:assembly -Dmaven.test.skip=true
java -cp target/command-line-1.0-SNAPSHOT-jar-with-dependencies.jar ..\images\AllSupportedBarcodeTypes.png

java barcode command line

We can see ZXing only returns 8 results, whereas Dynamsoft Barcode Reader returns 18 results.

Desktop GUI Application built with Java Swing

Based on the command-line Java program created above, we can add Swing class to turn the command-line app 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 ZXing and Dynamsoft Barcode Reader.

We initialize the layout with all the widgets:

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

As the button click event is triggered, we can load an image for recognizing barcode and QR code:

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:

mvn clean install assembly:assembly -Dmaven.test.skip=true
java -cp target/gui-1.0-SNAPSHOT-jar-with-dependencies.jar

java barcode gui

You can switch ZXing and Dynamsoft Barcode Reader by drop-down list.

Web Application built with Java Spring Boot

Spring Boot is used to develop web applications. We can follow the official tutorial to get started with a simple web app.

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


Afterwards, we create a controller that contains POST mappings for ZXing and Dynamsoft Barcode Reader:

public class BarcodeController {

    private DynamsoftBarcode mDynamsoftBarcode;
    private ZXingBarcode mZXingBarcode;

    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());

Finally, we build and run the Spring Boot app as follows:

mvn clean install
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 event.

java barcode spring boot

Source Code