How to Use Dynamsoft Java Barcode Reader to Scan Multiple Barcodes

There are many open-source and commercial barcode SDKs, but only a few of them can recognize multiple barcodes by scanning once. When you search Google for barcode SDK or Java barcode SDK, you will find Dynamsoft Barcode Reader SDK is always ranked in the top 5 of the search results. In this article, I will share how to read muti-barcodes by using Dynamsoft Java barcode reader.

Building Java Barcode Reader with Maven

Install Maven.

Install Maven extension for Visual Studio Code.

Maven extension for visual studio code

Select archetype-quickstart-jdk8 to create a “Hello World” Maven project.

vscode Maven project

We can press F5 to run the app instantly.

run Java in vscode

Next, we can add Dynamsoft Barcode Reader as the dependency to pom.xml:

    <dependencies>
        <dependency>
            <groupId>com.dynamsoft</groupId>
            <artifactId>dbr</artifactId>
            <version>7.3</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>dbr</id>
            <url>https://download.dynamsoft.com/maven/dbr/jar</url>
        </repository>
    </repositories>

Import the dependency in the Java source code:

import com.dynamsoft.barcode.*;

Instantiate the barcode reader object with a valid license:

        BarcodeReader br = null;
        try {
            // Get license from https://www.dynamsoft.com/CustomerPortal/Portal/Triallicense.aspx
            br = new BarcodeReader("LICENSE-KEY");
        } catch (Exception e) {
            System.out.println(e);
            return;
        }

There are three optional decoding functions available for developers:

  • decodeBufferedImage()
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File(pszImageFile));
            TextResult[] results = br.decodeBufferedImage(img, "");

        } catch (IOException e) {
            System.out.println(e);
        }
  • decodeFile()
TextResult[] results = br.decodeFile(pszImageFile, "");
  • decodeFileInMemory
TextResult[] results = br.decodeFileInMemory(Files.readAllBytes(new File(pszImageFile).toPath()), "");

After invoking decoding API, we can get all barcode results as follows:

        if (results != null &amp;&amp; results.length > 0) {
            String pszTemp = null;
            iIndex = 0;
            for (TextResult result : results) {
                iIndex++;
                pszTemp = String.format("  Barcode %d:", iIndex);
                System.out.println(pszTemp);
                pszTemp = String.format("    Page: %d", result.localizationResult.pageNumber + 1);
                System.out.println(pszTemp);
                if (result.barcodeFormat != 0) {
                    pszTemp = "    Type: " + result.barcodeFormatString;
                } else {
                    pszTemp = "    Type: " + result.barcodeFormatString_2;
                }
                System.out.println(pszTemp);
                pszTemp = "    Value: " + result.barcodeText;
                System.out.println(pszTemp);

                pszTemp = String.format("    Region points: {(%d,%d),(%d,%d),(%d,%d),(%d,%d)}",
                        result.localizationResult.resultPoints[0].x, result.localizationResult.resultPoints[0].y,
                        result.localizationResult.resultPoints[1].x, result.localizationResult.resultPoints[1].y,
                        result.localizationResult.resultPoints[2].x, result.localizationResult.resultPoints[2].y,
                        result.localizationResult.resultPoints[3].x, result.localizationResult.resultPoints[3].y);

                System.out.println(pszTemp);
                System.out.println();
            }
        }

If you want to deploy the barcode reading function to a web server for processing amounts of decoding request, you can use a thread pool to simulate the scenario:

        ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
         
        for (int i = 1; i <= 1000; i++) 
        {
            executor.execute(new Runnable(){
            
                @Override
                public void run() {
                    App test = new App();
                    test.decodefile(filename);
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        System.out.println(e);
                    }
                }
            });
        }
        executor.shutdown();

To avoid running out of memory, don’t use the BufferedImage class, because the default JVM heap memory is limited.

To run the app with an input image in Visual Studio Code, add args to the .vscode\launch.json file:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Debug (Launch) - Current File",
            "request": "launch",
            "mainClass": "${file}",
            "args": "images/AllSupportedBarcodeTypes.png"
        },
        {
            "type": "java",
            "name": "Debug (Launch)-App<test>",
            "request": "launch",
            "mainClass": "com.dynamsoft.App",
            "projectName": "test",
            "args": "images/AllSupportedBarcodeTypes.png"
        }
    ]
}

Running Java Barcode Reader in Command Line Tools

To conveniently run the Java program with dependencies, we can add maven assembly plugin to pom.xml:

                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>

Then build everything into a jar file:

mvn clean install assembly:assembly

Finally, run the Java barcode reader by using the following command:

java -cp target/test-1.0-SNAPSHOT-jar-with-dependencies.jar com.dynamsoft.App images/AllSupportedBarcodeTypes.png
read multiple barcodes by using Java barcode reader

Once there is no issue in Windows, we can verify whether the program can work in Linux.

I created a Linux environment with Docker. Here is my Dockerfile:

FROM openjdk:11-stretch
COPY images/AllSupportedBarcodeTypes.png AllSupportedBarcodeTypes.png
COPY target/test-1.0-SNAPSHOT-jar-with-dependencies.jar test-1.0-SNAPSHOT-jar-with-dependencies.jar
CMD java -cp test-1.0-SNAPSHOT-jar-with-dependencies.jar com.dynamsoft.App AllSupportedBarcodeTypes.png

Now, build the Docker image and run the Java program in a Docker Linux container:

docker rmi dynamsoft/barcode-reader -f
docker build -t dynamsoft/barcode-reader -f Dockerfile .
docker run -it dynamsoft/barcode-reader
Java barcode reader in Docker Linux container

Source Code

https://github.com/yushulx/java-barcode-reader