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.
Select archetype-quickstart-jdk8 to create a “Hello World” Maven project.
We can press F5 to run the app instantly.
Next, we can add Dynamsoft Barcode Reader as the dependency to pom.xml:
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.aspxbr = 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 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:
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:
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:
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
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-stretchCOPY images/AllSupportedBarcodeTypes.png AllSupportedBarcodeTypes.pngCOPY target/test-1.0-SNAPSHOT-jar-with-dependencies.jar test-1.0-SNAPSHOT-jar-with-dependencies.jarCMD 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 -fdocker build -t dynamsoft/barcode-reader -f Dockerfile .docker run -it dynamsoft/barcode-reader
Source Code
Originally published at https://www.codepool.biz on February 26, 2020.