很多做Java開發的喜歡用ZXing實現掃碼功能,但是ZXing的功能有限。如果要開發企業級的產品,就需要企業級的SDK。Dynamsoft Barcode Reader作爲企業級的掃碼SDK,提供了移動和桌面版本。SDK基於JNI封裝,能夠給Java程序提供最好的性能。
使用Gradle創建Java/Kotlin工程
習慣安卓開發的都很熟悉Gradle。Gradle用於桌面程序開發也很方便。
用Gradle命令創建一個新的工程:
$ 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]
創建的時候,編程語言可以選擇Java或者Kotlin。
打開 app > build.gradle。加入Maven倉庫和依賴:
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:8.1.2'
}
如果要做Android開發,URL和依賴的名字需要修改下:
repositories {
maven {
url "http://download2.dynamsoft.com/maven/dbr/aar"
}
}
dependencies {
implementation 'com.dynamsoft:dynamsoftbarcodereader:latest.release@aar'
}
Java/Kotlin桌面掃碼程序
打開App.java/App.kt文件,創建BarcodeReader
對象:
// Java
import com.dynamsoft.dbr.*;
BarcodeReader br = new BarcodeReader(license);
// Kotlin
import com.dynamsoft.dbr.*
val br = BarcodeReader(license)
設置初始化參數:
// 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)
讀取文件解碼,返回結果:
// 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
)
)
}
現在測試一張模糊的QR二維碼:
在Gradle運行命令中加上文件名和license文件:
./gradlew run --args="../../images/QR-Blurred.jpg ../../license.txt"
如果沒有license,程序也可以運行,但是結果會被混淆。可以申請一個免費的30天試用license來顯示正確的結果。
使用Travis CI測試Windows, Linux和Mac
如果想測試下這個命令行掃碼程序是否可以在Windows, Linux和Mac上正常運行,可以配置.travis.yml
文件:
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"
Travis CI的Windows虛擬機目前不支持Java,可以嘗試手動安裝。在Linux和Mac上需要用chmod +x
修改gradlew
的權限。
源碼
https://github.com/yushulx/gradle-java-kotlin-barcode-sample