Skip to content

Commit 27f4107

Browse files
committed
Added a command-line example
1 parent 730f935 commit 27f4107

6 files changed

Lines changed: 205 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/jni/build
44
classes
55
/target/maven-archiver
6+
target
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM openjdk:11-stretch
2+
COPY images/AllSupportedBarcodeTypes.png AllSupportedBarcodeTypes.png
3+
COPY target/test-1.0-SNAPSHOT-jar-with-dependencies.jar test-1.0-SNAPSHOT-jar-with-dependencies.jar
4+
CMD java -cp test-1.0-SNAPSHOT-jar-with-dependencies.jar com.dynamsoft.App AllSupportedBarcodeTypes.png
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Multiple 1D/2D Barcode Detection in Java
2+
3+
This sample demonstrates how to use the [Dynamsoft Barcode Reader SDK](https://www.dynamsoft.com/barcode-reader/sdk-desktop-server/) to build a simple Java barcode and QR code reader on **Windows**, **Linux (AMD64 and ARM64)**, and **macOS**.
4+
5+
## Usage
6+
1. Obtain a valid [license key](https://www.dynamsoft.com/customer/license/trialLicense) and update the code in `src/main/java/com/dynamsoft/App.java`:
7+
8+
```java
9+
BarcodeReader.initLicense("LICENSE-KEY");
10+
```
11+
12+
2. Run the following commands in your command-line tools:
13+
14+
```bash
15+
mvn clean package
16+
java -cp target/test-1.0-SNAPSHOT-jar-with-dependencies.jar com.dynamsoft.App images/AllSupportedBarcodeTypes.png
17+
```
18+
19+
![Java barcode and QR code reader](https://www.dynamsoft.com/codepool/img/2022/03/arm64-jetson-nano-java-barcode.png)
20+
21+
## Docker Linux Container
22+
Build and Run Docker Container:
23+
24+
```
25+
docker rmi dynamsoft/barcode-reader -f
26+
docker build -t dynamsoft/barcode-reader -f Dockerfile .
27+
docker run -it dynamsoft/barcode-reader
28+
```
29+
30+
![Java barcode reader in Docker](https://www.dynamsoft.com/codepool/img/2020/02/java-barcode-reader-docker.png)
31+
32+
33+
## Blog
34+
[How to Use Dynamsoft Java Barcode Reader to Scan Multiple Barcodes](https://www.dynamsoft.com/codepool/java-barcode-reader-scan-multiple.html)
287 KB
Loading

examples/9.x/command_line/pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.dynamsoft</groupId>
6+
<artifactId>test</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<properties>
9+
<maven.compiler.source>1.8</maven.compiler.source>
10+
<maven.compiler.target>1.8</maven.compiler.target>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
</properties>
13+
<dependencies>
14+
<dependency>
15+
<groupId>junit</groupId>
16+
<artifactId>junit</artifactId>
17+
<version>4.12</version>
18+
<scope>test</scope>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.dynamsoft</groupId>
22+
<artifactId>dbr</artifactId>
23+
<version>9.6.40.1</version>
24+
</dependency>
25+
</dependencies>
26+
<repositories>
27+
<repository>
28+
<id>dbr</id>
29+
<url>https://download2.dynamsoft.com/maven/dbr/jar</url>
30+
</repository>
31+
</repositories>
32+
<build>
33+
<plugins>
34+
<plugin>
35+
<artifactId>maven-assembly-plugin</artifactId>
36+
<configuration>
37+
<descriptorRefs>
38+
<descriptorRef>jar-with-dependencies</descriptorRef>
39+
</descriptorRefs>
40+
<archive>
41+
<manifest>
42+
<mainClass>com.dynamsoft.Test</mainClass>
43+
</manifest>
44+
</archive>
45+
</configuration>
46+
<executions>
47+
<execution>
48+
<id>make-assembly</id>
49+
<phase>package</phase>
50+
<goals>
51+
<goal>single</goal>
52+
</goals>
53+
</execution>
54+
</executions>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
</project>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.dynamsoft;
2+
3+
import com.dynamsoft.dbr.*;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.util.concurrent.Executors;
9+
import java.util.concurrent.ThreadPoolExecutor;
10+
import java.awt.image.*;
11+
import javax.imageio.ImageIO;
12+
13+
public final class App {
14+
private App() {
15+
}
16+
17+
public void decodefile(String filename) {
18+
int iIndex = 0;
19+
String pszImageFile = filename;
20+
BarcodeReader br = null;
21+
try {
22+
// Get a license key from https://www.dynamsoft.com/customer/license/trialLicense?product=dbr
23+
BarcodeReader.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==");
24+
br = new BarcodeReader();
25+
} catch (Exception e) {
26+
System.out.println(e);
27+
return;
28+
}
29+
30+
// BufferedImage img = null;
31+
// try {
32+
// img = ImageIO.read(new File(pszImageFile));
33+
// } catch (IOException e) {
34+
// System.out.println(e);
35+
// }
36+
37+
TextResult[] results = null;
38+
try {
39+
// results = br.decodeBufferedImage(img, "");
40+
results = br.decodeFile(pszImageFile, "");
41+
// results = br.decodeFileInMemory(Files.readAllBytes(new File(pszImageFile).toPath()), "");
42+
} catch (Exception e) {
43+
System.out.println("decode buffered image: " + e);
44+
}
45+
46+
if (results != null && results.length > 0) {
47+
String pszTemp = null;
48+
iIndex = 0;
49+
for (TextResult result : results) {
50+
iIndex++;
51+
pszTemp = String.format(" Barcode %d:", iIndex);
52+
System.out.println(pszTemp);
53+
pszTemp = String.format(" Page: %d", result.localizationResult.pageNumber + 1);
54+
System.out.println(pszTemp);
55+
if (result.barcodeFormat != 0) {
56+
pszTemp = " Type: " + result.barcodeFormatString;
57+
} else {
58+
pszTemp = " Type: " + result.barcodeFormatString_2;
59+
}
60+
System.out.println(pszTemp);
61+
pszTemp = " Value: " + result.barcodeText;
62+
System.out.println(pszTemp);
63+
64+
pszTemp = String.format(" Region points: {(%d,%d),(%d,%d),(%d,%d),(%d,%d)}",
65+
result.localizationResult.resultPoints[0].x, result.localizationResult.resultPoints[0].y,
66+
result.localizationResult.resultPoints[1].x, result.localizationResult.resultPoints[1].y,
67+
result.localizationResult.resultPoints[2].x, result.localizationResult.resultPoints[2].y,
68+
result.localizationResult.resultPoints[3].x, result.localizationResult.resultPoints[3].y);
69+
70+
System.out.println(pszTemp);
71+
System.out.println();
72+
}
73+
}
74+
}
75+
76+
/**
77+
* Says hello to the world.
78+
* @param args The arguments of the program.
79+
*/
80+
public static void main(String[] args) {
81+
if (args.length == 0) {
82+
System.out.println("Please add an image file");
83+
return;
84+
}
85+
86+
final String filename = args[0];
87+
88+
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
89+
90+
for (int i = 1; i <= 1; i++)
91+
{
92+
executor.execute(new Runnable(){
93+
94+
@Override
95+
public void run() {
96+
App test = new App();
97+
test.decodefile(filename);
98+
try {
99+
Thread.sleep(200);
100+
} catch (InterruptedException e) {
101+
System.out.println(e);
102+
}
103+
}
104+
});
105+
}
106+
executor.shutdown();
107+
}
108+
}

0 commit comments

Comments
 (0)