How to Read Multiple Barcode and QR Code with Dynamsoft Java Barcode SDK
Tags: java, qrcode, barcode, arm64
Published: 2022-03-17 02:04:56

There are many open-source and commercial barcode SDKs, but only a few of them can recognize multiple barcode and QR code simultaneously. When you search Google for barcode SDK or Java barcode SDK, Dynamsoft Barcode Reader SDK always appears in the top 5 of the search results. This article guides you through the process of creating a Java barcode and QR code scanning application for Windows (**x86 and x64**), Linux (**AMD64 and ARM64**), and macOS. 

## SDK Version
[v9.0](https://www.dynamsoft.com/barcode-reader/downloads/)

## Supported Platforms
- Windows (x86 and x64)
- Linux (x64 and ARM64): PC, Raspberry Pi, Jetson Nano
- macOS (x64)

## Steps to Configure Java Barcode SDK with Maven
1. Install [Maven](https://maven.apache.org/download.cgi).
2. Create a new Maven project via the command line:
    
    ```bash
    mvn archetype:generate -DgroupId=com.dynamsoft -DartifactId=test -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
    ```
3. Open the **pom.xml** file in the new project folder to add the dependency:
    
    ```xml
    <dependencies>
        <dependency>
            <groupId>com.dynamsoft</groupId>
            <artifactId>dbr</artifactId>
            <version>9.0.0</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>dbr</id>
            <url>https://download2.dynamsoft.com/maven/dbr/jar</url>
        </repository>
    </repositories>
    ```

4. Build the project:  
    
    ```bash
    mvn package
    ```  

5. Run the Java application:
    
    ```bash
    java -cp target/test-1.0-SNAPSHOT.jar com.dynamsoft.App
    ```
    
### Maven Configuration in Visual Studio Code
If you are using **Visual Studio Code**, the steps can be simplified as follows:
1. Install the Maven extension:

    ![Maven extension for visual studio code](https://www.dynamsoft.com/codepool/img/2020/02/vscode-maven-extension.png)

2. Select **archetype-quickstart-jdk8** to create a new Maven project.

    ![vscode Maven project](https://www.dynamsoft.com/codepool/img/2020/02/vscode-maven-project.png)

3. Add the dependency to `pom.xml`.
4. Press **F5** to run the app instantly.
    
    ![run Java in vscode](https://www.dynamsoft.com/codepool/img/2020/02/vscode-maven-run.png)

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

```json
{
    "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"
        }
    ]
}
```


## Implementing Multiple Barcode and QR Code Scanning in Java

First, we import the Java barcode SDK:

```java
import com.dynamsoft.dbr.*;
```

Before instantiating the barcode reader object, you must get a valid [license key](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr) and set it globally:

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

There are three optional decoding functions available for developers. We can call one of them to scan multiple barcode and QR code:

- [decodeBufferedImage](https://www.dynamsoft.com/barcode-reader/programming/java/api-reference/BarcodeReader/decode.html?ver=latest#decodebufferedimage)

    ```java
    BufferedImage img = null;
    try {
        img = ImageIO.read(new File(pszImageFile));
        TextResult[] results = br.decodeBufferedImage(img, "");
    
    } catch (IOException e) {
        System.out.println(e);
    }
    ```

- [decodeFile](https://www.dynamsoft.com/barcode-reader/programming/java/api-reference/BarcodeReader/decode.html?ver=latest#decodefile)

    ```java
    TextResult[] results = br.decodeFile(pszImageFile, "");
    ```

- [decodeFileInMemory](https://www.dynamsoft.com/barcode-reader/programming/java/api-reference/BarcodeReader/decode.html?ver=latest#decodefileinmemory)

    ```java
    TextResult[] results = br.decodeFileInMemory(Files.readAllBytes(new File(pszImageFile).toPath()), "");
    ```

Finally, we use the following code to extract the information from the recognition results:

```java
if (results != null && 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 scan amounts of barcode and QR code images concurrently, you can use a thread pool to improve the performance:

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

Note: due to the limitation of JVM heap memory, you may suffer from the out-of-memory error when reading image files to `BufferedImage` in Java. Therefore, using `BufferedImage` in multiple threads is not recommended.

## How to Build and Run Java Barcode and QR Code Reader 
To easily run the Java program with dependencies, we add a maven assembly plugin to `pom.xml`:

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

The plugin will build everything into a jar file:

```plain
mvn clean install assembly:assembly
```

Finally, we run the Java application by using the following command:

```plain
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](https://www.dynamsoft.com/codepool/img/2020/02/AllSupportedBarcodeTypes-1000x652.png)

![Java barcode and QR code reader](https://www.dynamsoft.com/codepool/img/2022/03/arm64-jetson-nano-java-barcode.png)


The Java program can work on Windows, Linux, macOS, Raspberry Pi and Jetson Nano.


## How to Deploy and Run Java Application Using Docker
1. Create a **Dockerfile**:

    ```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
    ```

2. Build the Docker image and run the Java program in a Docker Linux container:

    ```plain
    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](https://www.dynamsoft.com/codepool/img/2020/02/java-barcode-reader-docker.png)

## Source Code

[https://github.com/yushulx/java-barcode-reader](https://github.com/yushulx/java-barcode-reader)