Skip to content

Commit 73e4c44

Browse files
committed
Added a Gradle example
1 parent 27f4107 commit 73e4c44

25 files changed

Lines changed: 937 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Gradle Project Example: Java & Kotlin Barcode Reader
2+
This sample demonstrates how to use Gradle to build and run a simple command-line barcode reader written in Java or Kotlin.
3+
4+
## License Key
5+
Get a [30-day FREE trial license](https://www.dynamsoft.com/customer/license/trialLicense/).
6+
7+
## Java Barcode SDK Documentation
8+
9+
For comprehensive documentation, visit the [Java Barcode SDK](https://www.dynamsoft.com/barcode-reader/docs/server/programming/java/) page.
10+
11+
## Usage
12+
To build and run the barcode reader, use the following command:
13+
14+
```bash
15+
./gradlew run --args="<image file> <license file>"
16+
```
160 KB
Loading
179 KB
Loading
47.8 KB
Loading
2.49 MB
Loading
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# These are explicitly windows files and should use crlf
5+
*.bat text eol=crlf
6+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Java application project to get you started.
5+
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
6+
* User Manual available at https://docs.gradle.org/6.8.3/userguide/building_java_projects.html
7+
*/
8+
9+
plugins {
10+
// Apply the application plugin to add support for building a CLI application in Java.
11+
id 'application'
12+
}
13+
14+
repositories {
15+
// Use JCenter for resolving dependencies.
16+
jcenter()
17+
maven {
18+
url "http://download2.dynamsoft.com/maven/dbr/jar"
19+
}
20+
}
21+
22+
dependencies {
23+
// Use JUnit test framework.
24+
testImplementation 'junit:junit:4.13'
25+
26+
// This dependency is used by the application.
27+
implementation 'com.google.guava:guava:29.0-jre'
28+
29+
// Dynamsoft Barcode Reader SDK
30+
implementation 'com.dynamsoft:dbr:9.6.40.1'
31+
}
32+
33+
application {
34+
// Define the main class for the application.
35+
mainClass = 'dynamsoft.barcode.reader.App'
36+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* This Java source file was generated by the Gradle 'init' task.
3+
*/
4+
package dynamsoft.barcode.reader;
5+
6+
import com.dynamsoft.dbr.*;
7+
8+
import java.nio.file.*;
9+
10+
public class App {
11+
12+
private BarcodeReader mBarcodeReader;
13+
14+
public App(String license) {
15+
try {
16+
mBarcodeReader = new BarcodeReader(license);
17+
} catch (Exception e) {
18+
//TODO: handle exception
19+
e.printStackTrace();
20+
}
21+
22+
}
23+
24+
public BarcodeReader getBarcodeReader() {
25+
return mBarcodeReader;
26+
}
27+
28+
public String getGreeting() {
29+
return "Hello Dynamsoft Barcode Reader!";
30+
}
31+
32+
public TextResult[] decodeFile(String file) {
33+
TextResult[] results = null;
34+
// Read barcode
35+
try {
36+
//Best coverage settings
37+
// br.initRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"BestCoverage\",\"DeblurLevel\":9,\"ExpectedBarcodesCount\":512,\"ScaleDownThreshold\":100000,\"LocalizationModes\":[{\"Mode\":\"LM_CONNECTED_BLOCKS\"},{\"Mode\":\"LM_SCAN_DIRECTLY\"},{\"Mode\":\"LM_STATISTICS\"},{\"Mode\":\"LM_LINES\"},{\"Mode\":\"LM_STATISTICS_MARKS\"}],\"GrayscaleTransformationModes\":[{\"Mode\":\"GTM_ORIGINAL\"},{\"Mode\":\"GTM_INVERTED\"}]}}", EnumConflictMode.CM_OVERWRITE);
38+
//Best speed settings
39+
//br.initRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"BestSpeed\",\"DeblurLevel\":3,\"ExpectedBarcodesCount\":512,\"LocalizationModes\":[{\"Mode\":\"LM_SCAN_DIRECTLY\"}],\"TextFilterModes\":[{\"MinImageDimension\":262144,\"Mode\":\"TFM_GENERAL_CONTOUR\"}]}}",EnumConflictMode.CM_OVERWRITE);
40+
//Balance settings
41+
mBarcodeReader.initRuntimeSettingsWithString("{\"ImageParameter\":{\"Name\":\"Balance\",\"DeblurLevel\":5,\"ExpectedBarcodesCount\":512,\"LocalizationModes\":[{\"Mode\":\"LM_CONNECTED_BLOCKS\"},{\"Mode\":\"LM_STATISTICS\"}]}}",EnumConflictMode.CM_OVERWRITE);
42+
long start = System.currentTimeMillis();
43+
results = mBarcodeReader.decodeFile(file, "");
44+
long end = System.currentTimeMillis();
45+
46+
if (results == null || results.length == 0) {
47+
System.out.println(String.format("No barcode found. Total time cost: %d ms.", (end - start)));
48+
} else {
49+
System.out.println(String.format("Total barcode(s) found: %d. Total time cost: %d ms.", results.length, (end -start)));
50+
int index = 0;
51+
for (TextResult result : results) {
52+
System.out.println(String.format(" Barcode %d:", index++));
53+
if(result.barcodeFormat != 0){
54+
System.out.println(" Type: " + result.barcodeFormatString);
55+
} else {
56+
System.out.println(" Type: " + result.barcodeFormatString_2);
57+
}
58+
59+
System.out.println(" Value: " + result.barcodeText);
60+
61+
System.out.println(String.format(" Region points: {(%d,%d),(%d,%d),(%d,%d),(%d,%d)}",
62+
result.localizationResult.resultPoints[0].x, result.localizationResult.resultPoints[0].y,
63+
result.localizationResult.resultPoints[1].x,result.localizationResult.resultPoints[1].y,
64+
result.localizationResult.resultPoints[2].x,result.localizationResult.resultPoints[2].y,
65+
result.localizationResult.resultPoints[3].x,result.localizationResult.resultPoints[3].y));
66+
}
67+
}
68+
} catch (BarcodeReaderException e) {
69+
e.printStackTrace();
70+
}
71+
72+
return results;
73+
}
74+
75+
public static void main(String[] args) throws Exception {
76+
77+
System.out.println("Working Directory = " + System.getProperty("user.dir"));
78+
79+
if (args.length == 0) {
80+
String newLine = System.getProperty("line.separator");
81+
String s = new StringBuilder()
82+
.append("Usage:")
83+
.append(newLine)
84+
.append(" ./gradlew run --args=\"<image file> <license file>\"").append(newLine)
85+
.toString();
86+
87+
System.out.println(s);
88+
return;
89+
}
90+
91+
String file = args[0];
92+
String license = "DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==";
93+
94+
if (args.length == 2) {
95+
96+
license = new String(Files.readAllBytes(Paths.get(args[1])));
97+
}
98+
App app = new App(license);
99+
System.out.println(app.getGreeting());
100+
app.decodeFile(file);
101+
102+
}
103+
}
104+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* This Java source file was generated by the Gradle 'init' task.
3+
*/
4+
package dynamsoft.barcode.reader;
5+
6+
import org.junit.Test;
7+
import static org.junit.Assert.*;
8+
9+
public class AppTest {
10+
@Test public void testAppHasAGreeting() {
11+
App classUnderTest = new App("");
12+
assertNotNull("app should have a greeting", classUnderTest.getGreeting());
13+
}
14+
15+
@Test public void testAppHasABarcodeReader() {
16+
App classUnderTest = new App("");
17+
assertNotNull("app should have a barcode reader instance", classUnderTest.getBarcodeReader());
18+
}
19+
}

0 commit comments

Comments
 (0)