How to Integrate Java Barcode SDK to Command-Line, GUI and Web Apps

This article shows how to integrate Java barcode SDKs - ZXing and Dynamsoft Barcode Reader - to different Java applications.

Java Barcode Apps

Here I have an image containing multiple barcodes.

multiple barcode image

Configure the Maven dependencies for ZXing and Dynamsoft Barcode Reader:


To make Dynamsoft Barcode Reader work, apply for a trial license from the online portal.

Command Line

Create a new Maven project:

mvn archetype:generate -DartifactId=app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Import ZXing and Dynamsoft Barcode Reader:

import com.dynamsoft.barcode.*;

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) {

The testing image contains multiple barcodes. To read multiple barcodes encoded with different barcode symbologies, we need to use MultiFormatReader and GenericMultipleBarcodeReader. The inconvenient part is we have to convert BufferedImage to BinaryBitmap first:

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;

Invoking Dynamsoft Barcode Reader method is much simpler, for it supports BufferedImage as the input parameter:

        BarcodeReader br = null;
        try {
            br = new BarcodeReader("LICENSE-KEY");
        } 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, we can assemble all dependencies into one jar file. Add the maven-assembly-plugin to pom.xml:


Build the Maven project:

mvn clean install assembly:assembly -Dmaven.test.skip=true

Run the Java barcode program as follows:

java -cp target/command-line-1.0-SNAPSHOT-jar-with-dependencies.jar ..\images\AllSupportedBarcodeTypes.png

Check the SDK performance. We can see ZXing only returns 8 results, whereas Dynamsoft Barcode Reader returns 18 results.

java barcode command line


Based on the command-line Java barcode program created above, we can add Swing class to modify the command-line app to a GUI app.

Here are 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.

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

Select an image and decode barcodes by clicking the button:

    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 GUI app:

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

java barcode gui


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

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


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

Build and run the Java barcode web app:

mvn clean install
java -jar target/web-1.0-SNAPSHOT.jar

Visit http://localhost:8080/swagger-ui.html to test ZXing and Dynamsoft Barcode Reader via POST event.

java barcode spring boot

What About Android Development?

When developing mobile apps with Java barcode SDKs, we use Gradle instead of Maven to add dependencies.

allprojects {
    repositories {
        maven {
            url ""
implementation ''
implementation 'com.dynamsoft:dynamsoftbarcodereader:7.3.0'

Source Code

Search Blog Posts