标签:条形码 localizationResult Java .. Kotlin resultPoints result
很多做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
标签:条形码,localizationResult,Java,..,Kotlin,resultPoints,result 来源: https://blog.csdn.net/yushulx/article/details/114638606
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。