Skip to content

Commit 0fce106

Browse files
committed
Added a ZXing example
1 parent 6138924 commit 0fce106

17 files changed

Lines changed: 899 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# ZXing & Dynamsoft Java Barcode Reader: Command Line, GUI and Web
2+
3+
This repository contains three samples (command line, GUI, and web) demonstrating how to implement a Java barcode and QR code reader using [ZXing](https://github.com/zxing/zxing) and [Dynamsoft Barcode Reader](https://www.dynamsoft.com/barcode-reader/sdk-desktop-server/).
4+
5+
## Install ZXing and Dynamsoft Barcode Reader in Maven Project
6+
To set up your Maven project, configure the `pom.xml` file to include the necessary repositories and dependencies:
7+
8+
```xml
9+
<repositories>
10+
<repository>
11+
<id>dbr</id>
12+
<url>https://download2.dynamsoft.com/maven/dbr/jar</url>
13+
</repository>
14+
</repositories>
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.dynamsoft</groupId>
18+
<artifactId>dbr</artifactId>
19+
<version>9.6.40.1</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.google.zxing</groupId>
23+
<artifactId>core</artifactId>
24+
<version>3.4.0</version>
25+
</dependency>
26+
```
27+
28+
## License Key
29+
Get a valid [license key](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr) for Dynamsoft Barcode Reader and update your Java code as follows:
30+
31+
```java
32+
BarcodeReader.initLicense("LICENSE-KEY");
33+
BarcodeReader br = new BarcodeReader();
34+
```
35+
36+
## Usage
37+
- [Command Line](command-line/README.md)
38+
- [GUI](gui/README.md)
39+
- [Web](web/README.md)
40+
41+
## Blog
42+
[How to Integrate Java Barcode SDK to Command-Line, GUI and Web Apps](https://www.dynamsoft.com/codepool/java-barcode-command-line-gui-web.html)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ZXing vs Dynamsoft Barcode Reader in Java
2+
3+
## Usage
4+
1. Obtain a valid [license key](https://www.dynamsoft.com/customer/license/trialLicense) and update the code in `src/main/java/com/java/barcode/App.java`:
5+
6+
```java
7+
BarcodeReader.initLicense("LICENSE-KEY");
8+
```
9+
10+
2. Run the following commands in your command-line tools:
11+
12+
```bash
13+
mvn clean package
14+
java -jar target/test-1.0-SNAPSHOT-jar-with-dependencies.jar ../../../../images/AllSupportedBarcodeTypes.png
15+
```
16+
17+
![Java barcode command line](http://www.dynamsoft.com/codepool/img/2020/03/java-barcode-command-line.png)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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>com.google.zxing</groupId>
16+
<artifactId>core</artifactId>
17+
<version>3.4.0</version>
18+
</dependency>
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>4.12</version>
23+
<scope>test</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.dynamsoft</groupId>
27+
<artifactId>dbr</artifactId>
28+
<version>9.6.40.1</version>
29+
</dependency>
30+
</dependencies>
31+
<repositories>
32+
<repository>
33+
<id>dbr</id>
34+
<url>https://download2.dynamsoft.com/maven/dbr/jar</url>
35+
</repository>
36+
</repositories>
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<artifactId>maven-assembly-plugin</artifactId>
41+
<configuration>
42+
<descriptorRefs>
43+
<descriptorRef>jar-with-dependencies</descriptorRef>
44+
</descriptorRefs>
45+
<archive>
46+
<manifest>
47+
<mainClass>com.java.barcode.App</mainClass>
48+
</manifest>
49+
</archive>
50+
</configuration>
51+
<executions>
52+
<execution>
53+
<id>make-assembly</id>
54+
<phase>package</phase>
55+
<goals>
56+
<goal>single</goal>
57+
</goals>
58+
</execution>
59+
</executions>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
</project>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.java.barcode;
2+
3+
import com.dynamsoft.dbr.*;
4+
import com.google.zxing.BinaryBitmap;
5+
import com.google.zxing.MultiFormatReader;
6+
import com.google.zxing.NotFoundException;
7+
import com.google.zxing.RGBLuminanceSource;
8+
import com.google.zxing.Result;
9+
import com.google.zxing.common.HybridBinarizer;
10+
import com.google.zxing.multi.*;
11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.awt.image.*;
14+
import javax.imageio.ImageIO;
15+
16+
public class App
17+
{
18+
public void decodefile(String filename) {
19+
// Read an image to BufferedImage
20+
BufferedImage image = null;
21+
try {
22+
image = ImageIO.read(new File(filename));
23+
} catch (IOException e) {
24+
System.out.println(e);
25+
return;
26+
}
27+
28+
// ZXing
29+
BinaryBitmap bitmap = null;
30+
int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
31+
RGBLuminanceSource source = new RGBLuminanceSource(image.getWidth(), image.getHeight(), pixels);
32+
bitmap = new BinaryBitmap(new HybridBinarizer(source));
33+
34+
MultiFormatReader reader = new MultiFormatReader();
35+
GenericMultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
36+
try {
37+
Result[] zxingResults = multiReader.decodeMultiple(bitmap);
38+
System.out.println("ZXing result count: " + zxingResults.length);
39+
if (zxingResults != null) {
40+
for (Result zxingResult : zxingResults) {
41+
System.out.println("Format: " + zxingResult.getBarcodeFormat());
42+
System.out.println("Text: " + zxingResult.getText());
43+
System.out.println();
44+
}
45+
}
46+
} catch (NotFoundException e) {
47+
e.printStackTrace();
48+
}
49+
pixels = null;
50+
bitmap = null;
51+
52+
System.out.println("------------------------------------------------------");
53+
54+
55+
// Dynamsoft
56+
BarcodeReader br = null;
57+
try {
58+
// Get a license key from https://www.dynamsoft.com/customer/license/trialLicense?product=dbr
59+
BarcodeReader.initLicense("DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==");
60+
br = new BarcodeReader();
61+
} catch (Exception e) {
62+
System.out.println(e);
63+
return;
64+
}
65+
66+
TextResult[] results = null;
67+
try {
68+
results = br.decodeBufferedImage(image, "");
69+
} catch (Exception e) {
70+
System.out.println("decode buffered image: " + e);
71+
}
72+
73+
if (results != null && results.length > 0) {
74+
System.out.println("DBR result count: " + results.length);
75+
String pszTemp = null;
76+
for (TextResult result : results) {
77+
if (result.barcodeFormat != 0) {
78+
pszTemp = "Format: " + result.barcodeFormatString;
79+
} else {
80+
pszTemp = "Format: " + result.barcodeFormatString_2;
81+
}
82+
System.out.println(pszTemp);
83+
System.out.println("Text: " + result.barcodeText);
84+
System.out.println();
85+
}
86+
}
87+
88+
if (image != null) {
89+
image.flush();
90+
image = null;
91+
}
92+
}
93+
94+
public static void main( String[] args )
95+
{
96+
if (args.length == 0) {
97+
System.out.println("Please add an image file");
98+
return;
99+
}
100+
101+
final String filename = args[0];
102+
App test = new App();
103+
test.decodefile(filename);
104+
}
105+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.java.barcode;
2+
3+
import junit.framework.Test;
4+
import junit.framework.TestCase;
5+
import junit.framework.TestSuite;
6+
7+
/**
8+
* Unit test for simple App.
9+
*/
10+
public class AppTest
11+
extends TestCase
12+
{
13+
/**
14+
* Create the test case
15+
*
16+
* @param testName name of the test case
17+
*/
18+
public AppTest( String testName )
19+
{
20+
super( testName );
21+
}
22+
23+
/**
24+
* @return the suite of tests being tested
25+
*/
26+
public static Test suite()
27+
{
28+
return new TestSuite( AppTest.class );
29+
}
30+
31+
/**
32+
* Rigourous Test :-)
33+
*/
34+
public void testApp()
35+
{
36+
assertTrue( true );
37+
}
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# ZXing vs Dynamsoft Barcode Reader in Java
2+
3+
## Usage
4+
1. Obtain a valid [license key](https://www.dynamsoft.com/customer/license/trialLicense) and update the code in `src/main/java/com/java/barcode/App.java`:
5+
6+
```java
7+
BarcodeReader.initLicense("LICENSE-KEY");
8+
```
9+
10+
2. Run the following commands in your command-line tools:
11+
12+
```
13+
mvc clean package
14+
java -jar target/test-1.0-SNAPSHOT-jar-with-dependencies.jar
15+
```
16+
17+
![Java barcode GUI](http://www.dynamsoft.com/codepool/img/2020/03/java-barcode-gui.png)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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>com.google.zxing</groupId>
16+
<artifactId>core</artifactId>
17+
<version>3.4.0</version>
18+
</dependency>
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>4.12</version>
23+
<scope>test</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.dynamsoft</groupId>
27+
<artifactId>dbr</artifactId>
28+
<version>9.6.40.1</version>
29+
</dependency>
30+
</dependencies>
31+
<repositories>
32+
<repository>
33+
<id>dbr</id>
34+
<url>https://download2.dynamsoft.com/maven/dbr/jar</url>
35+
</repository>
36+
</repositories>
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<artifactId>maven-assembly-plugin</artifactId>
41+
<configuration>
42+
<descriptorRefs>
43+
<descriptorRef>jar-with-dependencies</descriptorRef>
44+
</descriptorRefs>
45+
<archive>
46+
<manifest>
47+
<mainClass>com.java.barcode.App</mainClass>
48+
</manifest>
49+
</archive>
50+
</configuration>
51+
<executions>
52+
<execution>
53+
<id>make-assembly</id>
54+
<phase>package</phase>
55+
<goals>
56+
<goal>single</goal>
57+
</goals>
58+
</execution>
59+
</executions>
60+
</plugin>
61+
</plugins>
62+
</build>
63+
</project>

0 commit comments

Comments
 (0)