How to Build Desktop Barcode Scanner in Java and Kotlin
Dynamsoft offers Java Barcode SDKs for both mobile and desktop development. The mobile edition is available as an Android AAR package, while the desktop edition is provided as a JAR package. This article is designed to assist developers in building desktop or server-side barcode scanner applications using Java or Kotlin.
This article is Part 3 in a 5-Part Series.
- Part 1 - How to Package JNI Shared Library into Jar File
- Part 2 - How to Develop a DotCode Reader in Java Using a Webcam and OpenCV
- Part 3 - How to Build Desktop Barcode Scanner in Java and Kotlin
- Part 4 - How to Read Multiple Barcode and QR Code with Dynamsoft Java Barcode SDK
- Part 5 - Building Java Barcode QR Code Reading Apps: Dynamsoft vs. ZXing
Installing Dynamsoft Barcode Reader via Gradle
To get Dynamsoft Barcode Reader, you can either download the SDK directly or configure the Maven URL for auto-build tools. Using Gradle to create a Java or Kotlin barcode scanner project is recommended.
Steps to Create a New Gradle Project
-
Create a new Gradle project:
$ gradle init Select type of project to generate: 1: basic 2: application 3: library 4: Gradle plugin Enter selection (default: basic) [1..4] Select implementation language: 1: C++ 2: Groovy 3: Java 4: Kotlin 5: Scala 6: Swift Enter selection (default: Java) [1..6] Split functionality across multiple subprojects?: 1: no - only one application project 2: yes - application and library projects Enter selection (default: no - only one application project) [1..2] Select build script DSL: 1: Groovy 2: Kotlin Enter selection (default: Kotlin) [1..2]
-
Add the Maven repository hosted by Dynamsoft and the dependency in
app > build.gradle
:repositories { // Use JCenter for resolving dependencies. jcenter() maven { url "http://download2.dynamsoft.com/maven/dbr/jar" } } dependencies { // Use JUnit test framework. testImplementation 'junit:junit:4.13' // This dependency is used by the application. implementation 'com.google.guava:guava:29.0-jre' // Dynamsoft Barcode Reader SDK implementation 'com.dynamsoft:dbr:9.6.40.1' }
Note: for building Android barcode scanner projects, the URL and the package name are different:
repositories { maven { url "http://download2.dynamsoft.com/maven/dbr/aar" } } dependencies { implementation 'com.dynamsoft:dynamsoftbarcodereader:latest.release@aar' }
Java Barcode Scanner Application for Windows, Linux, and macOS
Dynamsoft Java Barcode SDK is implemented using Java Native Interface (JNI), offering better performance than pure Java SDKs like ZXing. Although the underlying technology is C++, the SDK wraps native libraries for multiple operating systems.
Open App.java
or App.kt
to instantiate the barcode reader object:
// Java
import com.dynamsoft.dbr.*;
BarcodeReader br = new BarcodeReader(license);
// Kotlin
import com.dynamsoft.dbr.*
val br = BarcodeReader(license)
Get a 30-day FREE Trial License here to unlock all API functionalities.
You can generate an appropriate template for parameter initialization using the free barcode online demo:
// Java
br.initRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"Balance\",\"DeblurLevel\":5,\"ExpectedBarcodesCount\":512,\"LocalizationModes\":[{\"Mode\":\"LM_CONNECTED_BLOCKS\"},{\"Mode\":\"LM_STATISTICS\"}]}}", EnumConflictMode.CM_OVERWRITE);
// Kotlin
br.initRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"Balance\",\"DeblurLevel\":5,\"ExpectedBarcodesCount\":512,\"LocalizationModes\":[{\"Mode\":\"LM_CONNECTED_BLOCKS\"},{\"Mode\":\"LM_STATISTICS\"}]}}", EnumConflictMode.CM_OVERWRITE)
The following sample code reads barcodes from an image file and prints out all results:
// Java
results = mBarcodeReader.decodeFile(file, "");
for (TextResult result : results) {
System.out.println(String.format(" Barcode %d:", index++));
if(result.barcodeFormat != 0){
System.out.println(" Type: " + result.barcodeFormatString);
} else {
System.out.println(" Type: " + result.barcodeFormatString_2);
}
System.out.println(" Value: " + result.barcodeText);
System.out.println(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));
}
// Kotlin
val results: Array<TextResult> = br.decodeFile(file, "")
for (result in results) {
println(String.format(" Barcode %d:", index++))
if (result.barcodeFormat != 0) {
System.out.println(" Type: " + result.barcodeFormatString)
} else {
System.out.println(" Type: " + result.barcodeFormatString_2)
}
System.out.println(" Value: " + result.barcodeText)
println(
java.lang.String.format(
" Region points: {(%d,%d),(%d,%d),(%d,%d),(%d,%d)}",
result.localizationResult.resultPoints.get(0).x,
result.localizationResult.resultPoints.get(0).y,
result.localizationResult.resultPoints.get(1).x,
result.localizationResult.resultPoints.get(1).y,
result.localizationResult.resultPoints.get(2).x,
result.localizationResult.resultPoints.get(2).y,
result.localizationResult.resultPoints.get(3).x,
result.localizationResult.resultPoints.get(3).y
)
)
}
With just a few lines of code, a simple command-line barcode scanner is ready.
Here is a blurred QR code image:
You can now run the Gradle command with specified arguments:
./gradlew run --args="../../images/QR-Blurred.jpg ../../license.txt"
Testing Java Applications Using Travis CI
To verify the cross-platform capability of the barcode scanner application, create a .travis.yml file for the GitHub repository:
language: java
jobs:
include:
- name: "Linux"
os: linux
- name: "macOS"
os: osx
- name: "Windows"
os: windows
language: shell
before_install:
- choco install jdk8 -params 'installdir=c:\\jdk' -y;
- export JAVA_HOME=${JAVA_HOME:-/c/jdk};
env: PATH=/c/jdk/bin:$PATH
branches:
only:
- main
script:
- if [[ ${TRAVIS_OS_NAME} != "windows" ]]; then
chmod +x java/gradlew;
chmod +x kotlin/gradlew;
fi
- cd java
- ./gradlew test
- ./gradlew run --args="../../images/AllSupportedBarcodeTypes.png"
- cd ..
- cd kotlin
- ./gradlew test
- ./gradlew run --args="../../images/AllSupportedBarcodeTypes.png"
Add chmod +x
to make the gradlew
script file executable on Linux and macOS.
Source Code
https://github.com/yushulx/java-jni-barcode-qrcode-reader/tree/main/examples/9.x/gradle_kotlin