个人博客导航页(点击右侧链接即可打开个人博客):大牛带你入门技术栈
Java Barcode应用
我的测试图片包含了各种类型的条形码。 在Maven的配置文件中添加ZXing和Dynamsoft Barcode Reader:
<repositories>
<repository>
<id>dbr</id>
<url>https://download.dynamsoft.com/maven/dbr/jar</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.dynamsoft</groupId>
<artifactId>dbr</artifactId>
<version>7.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
命令行
新建一个Maven工程:
mvn archetype:generate -DgroupId=com.java.barcode -DartifactId=app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
导入需要用的条形码库:
import com.dynamsoft.barcode.*;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.*;
使用ImageIO和BufferedImage读入图片:
import java.awt.image.*;
import javax.imageio.ImageIO;
BufferedImage image = null;
try {
image = ImageIO.read(new File(filename));
} catch (IOException e) {
System.out.println(e);
return;
}
ZXing读取多个条形码和单个条形码用到的类是不同的。以下是读取多个条形码的方法:
BinaryBitmap bitmap = null;
int[] pixels = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
RGBLuminanceSource source = new RGBLuminanceSource(image.getWidth(), image.getHeight(), pixels);
bitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader();
GenericMultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);
try {
Result[] zxingResults = multiReader.decodeMultiple(bitmap);
} catch (NotFoundException e) {
e.printStackTrace();
}
pixels = null;
bitmap = null;
使用Dynamsoft Barcode Reader比较简单,接口默认就可以识别多个条形码:
BarcodeReader br = null;
try {
br = new BarcodeReader("LICENSE-KEY");
} catch (Exception e) {
System.out.println(e);
return;
}
TextResult[] results = null;
try {
results = br.decodeBufferedImage(image, "");
} catch (Exception e) {
System.out.println("decode buffered image: " + e);
}
为了方便运行,用Maven插件打包所有依赖:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
编译运行工程:
mvn clean install assembly:assembly -Dmaven.test.skip=true
java -cp target/command-line-1.0-SNAPSHOT-jar-with-dependencies.jar com.java.barcode.App ..\images\AllSupportedBarcodeTypes.png
运行结果:
界面
基于命令行的代码,用Swing实现界面。 导入相关的类:
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
添加组件JTextArea, JButton, JFileChooser,JComboBox:
public App() {
super(new BorderLayout());
mFileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
".png", "png");
mFileChooser.setFileFilter(filter);
mLoad = new JButton("Load File");
mLoad.addActionListener(this);
mSourceList = new JComboBox(new String[]{"ZXing", "Dynamsoft"});
mSourceList.setSelectedIndex(0);
JPanel buttonPanel = new JPanel();
buttonPanel.add(mSourceList);
buttonPanel.add(mLoad);
add(buttonPanel, BorderLayout.PAGE_START);
mTextArea = new JTextArea();
mTextArea.setSize(480, 480);
JScrollPane sp = new JScrollPane(mTextArea);
add(sp, BorderLayout.CENTER);
}
添加按钮响应:
public void actionPerformed(ActionEvent e) {
int returnVal = mFileChooser.showOpenDialog(App.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = mFileChooser.getSelectedFile();
String filename = file.toPath().toString();
if (mSourceList.getSelectedItem().toString().equals("Dynamsoft")) {
TextResult[] results = decodefileDynamsoft(filename);
}
else {
Result[] results = decodefileZXing(filename);
}
}
}
编译运行:
mvn clean install assembly:assembly -Dmaven.test.skip=true
java -cp target/gui-1.0-SNAPSHOT-jar-with-dependencies.jar com.java.barcode.App
网络
根据Spring Boot的入门指南来创建一个简单的web应用。 在pom.xml中添加:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-core</artifactId>
<version>1.1.45</version>
<exclusions>
<exclusion>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.1.45</version>
</dependency>
这样就可以通过swagger-ui来测试server端的接口。 创建server端的代码:
@RestController
public class BarcodeController {
private DynamsoftBarcode mDynamsoftBarcode;
private ZXingBarcode mZXingBarcode;
@Autowired
public BarcodeController(DynamsoftBarcode dynamsoft, ZXingBarcode zxing)
{
mDynamsoftBarcode = dynamsoft;
mZXingBarcode = zxing;
}
@PostMapping(value = "/api/dynamsoft"
, consumes = MediaType.MULTIPART_FORM_DATA_VALUE
, produces = MediaType.APPLICATION_JSON_VALUE)
public BarcodeResponse getDynamsoft(@RequestPart MultipartFile file) throws Exception {
return mDynamsoftBarcode.decode(file.getOriginalFilename(), file.getInputStream());
}
@PostMapping(value = "/api/zxing"
, consumes = MediaType.MULTIPART_FORM_DATA_VALUE
, produces = MediaType.APPLICATION_JSON_VALUE)
public BarcodeResponse getZXing(@RequestPart MultipartFile file) throws Exception {
return mZXingBarcode.decode(file.getOriginalFilename(), file.getInputStream());
}
}
编译运行:
mvn clean install
java -jar target/web-1.0-SNAPSHOT.jar
打开http://localhost:8080/swagger-ui.html 测试接口。
安卓开发
如果要给安卓开发,在gradle中添加:
allprojects {
repositories {
google()
jcenter()
maven {
url "http://download.dynamsoft.com/maven/dbr/aar"
}
}
}
implementation 'com.google.zxing:core:3.4.0'
implementation 'com.dynamsoft:dynamsoftbarcodereader:7.3.0'
源码
https://github.com/yushulx/java-barcode-command-gui-web
附Java/C/C++/机器学习/算法与数据结构/前端/安卓/Python/程序员必读/书籍书单大全:
(点击右侧 即可打开个人博客内有干货):技术干货小栈
=====>>①【Java大牛带你入门到进阶之路】<<====
=====>>②【算法数据结构+acm大牛带你入门到进阶之路】<<===
=====>>③【数据库大牛带你入门到进阶之路】<<=====
=====>>④【Web前端大牛带你入门到进阶之路】<<====
=====>>⑤【机器学习和python大牛带你入门到进阶之路】<<====
=====>>⑥【架构师大牛带你入门到进阶之路】<<=====
=====>>⑦【C++大牛带你入门到进阶之路】<<====
=====>>⑧【ios大牛带你入门到进阶之路】<<====
=====>>⑨【Web安全大牛带你入门到进阶之路】<<=====
=====>>⑩【Linux和操作系统大牛带你入门到进阶之路】<<=====天下没有不劳而获的果实,望各位年轻的朋友,想学技术的朋友,在决心扎入技术道路的路上披荆斩棘,把书弄懂了,再去敲代码,把原理弄懂了,再去实践,将会带给你的人生,你的工作,你的未来一个美梦。