完整的SDK套件
Java条码快速入门
一个dynamsoft-barcodereader-7.5.jar文件位于下Dynamsoft条码阅读器\ lib文件夹中。您可以轻松地将jar文件导入到Java IDE(例如Eclipse)。如果使用的是Maven项目,则要添加依赖项,您需要按以下方式编辑pom.xml文件:
<repositories>
<repository>
<id>dbr </id>
<url>https://download2.dynamsoft.com/maven/dbr/jar </url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.dynamsoft</groupId>
<artifactId>dbr</artifactId>
<version>7.5.0</version>
</dependency>
</dependencies>
这是显示如何解码图像文件中条形码的代码段:
import com.dynamsoft.barcode.*;
public class App {
public static void main(String[] args) {
String filename = "<file path>";
try {
BarcodeReader reader = new BarcodeReader(
"<Your trial license>");
TextResult[] results = reader.decodeFile( filename, "");
for (TextResult result : results) {
System.out.println(result.barcodeText);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
}
}
高级API使用
加载模板文件和更新参数
BarcodeReader br = new BarcodeReader("<LICENSE-KEY>");
br.initRuntimeSettingsWithFile("balanced.json", EnumConflictMode.CM_OVERWRITE); // Load a template file
PublicRuntimeSettings runtimeSettings = br.getRuntimeSettings();
runtimeSettings.barcodeFormatIds = EnumBarcodeFormat.BF_PDF417 | EnumBarcodeFormat.BF_QR_CODE; // Update barcode formats
runtimeSettings.barcodeFormatIds_2 = EnumBarcodeFormat_2.BF2_DOTCODE;
br.updateRuntimeSettings(runtimeSettings);
模板配置很复杂。您可以使用在线条形码工具来自定义算法模板,也可以从GitHub下载现有模板文件。
解码文件流
您可以使用Spring框架来构建Web项目并在服务器端解码上传的图像。假设您有以下代码从多部分表单中获取上传的文件:
@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());
}
然后,您可以直接从文件流中解码条形码:
InputStream inputStream = file.getInputStream();
TextResult[] results = br.decodeFileInMemory(inputStream, "");
inputStream.close();
解码Java OpenCV Mat
通过Maven安装OpenCV Java:
<dependency>
<groupId>org</groupId>
<artifactId>opencv</artifactId>
<version>4.3.0</version>
</dependency>
将帧捕获到OpenCV Mat并从Mat解码条形码:
public static byte[] matToByteArray(Mat original)
{
int width = original.width(), height = original.height(), channels = original.channels();
byte[] sourcePixels = new byte[width * height * channels];
original.get(0, 0, sourcePixels);
return sourcePixels;
}
Mat frame = grabFrame();
TextResult[] results = reader.decodeBuffer(matToByteArray(frame), frame.width(), frame.height(), (int)frame.step1(), EnumImagePixelFormat.IPF_BGR_888, "");
解码BufferedImage
将图像文件读取到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;
}
从BufferedImage读取条形码:
TextResult[] results = null;
try {
results = br.decodeBufferedImage(image, "");
} catch (Exception e) {
System.out.println("decode buffered image: " + e);
}
本文章转载自【慧都科技】欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,尊重他人劳动成果