Dynamsoft Barcode Reader教程:如何使用Dynamsoft Java条形码阅读器扫描多个条形码

Dynamsoft Barcode Reader SDK一款多功能的条码读取控件,只需要几行代码就可以将条码读取功能嵌入到Web或桌面应用程序。这可以节省数月的开发时间和成本。能支持多种图像文件格式以及从摄像机或扫描仪获取的DIB格式。使用Dynamsoft Barcode Reader SDK,你可以创建强大且实用的条形码扫描仪软件,以满足你的业务需求。

Dynamsoft Barcode Reader SDK正式版


使用Maven构建Java条形码阅读器

安装Maven。

为Visual Studio Code安装Maven扩展。

Dynamsoft Barcode Reader教程:如何使用Dynamsoft Java条形码阅读器扫描多个条形码

选择archetype-quickstart-jdk8创建一个“Hello World” Maven项目。

Dynamsoft Barcode Reader教程:如何使用Dynamsoft Java条形码阅读器扫描多个条形码

我们可以按F5键立即运行该应用程序。

Dynamsoft Barcode Reader教程:如何使用Dynamsoft Java条形码阅读器扫描多个条形码

接下来,我们可以将Dynamsoft Barcode Reader添加为pom.xml的依赖项:

<dependencies>    <dependency>        <groupId>com.dynamsoft</groupId>        <artifactId>dbr</artifactId>        <version>7.3</version>    </dependency></dependencies><repositories>    <repository>        <id>dbr</id>        <url>https://download.dynamsoft.com/maven/dbr/jar</url>    </repository></repositories>

在Java源代码中导入依赖项:

import com.dynamsoft.barcode.*;

使用有效的许可证实例化条形码读取器对象:

BarcodeReader br = null;try {    // Get license from https://www.dynamsoft.com/CustomerPortal/Portal/Triallicense.aspx    br = new BarcodeReader("LICENSE-KEY");} catch (Exception e) {    System.out.println(e);    return;}

开发人员可以使用三种可选的解码功能:

1.encodeBufferedImage()

BufferedImage img = null;try {    img = ImageIO.read(new File(pszImageFile));    TextResult[] results = br.decodeBufferedImage(img, "");} catch (IOException e) {    System.out.println(e);}

2.encodeFile()

TextResult[] results = br.decodeFile(pszImageFile, "");

3.encodeFileInMemory

TextResult[] results = br.decodeFileInMemory(Files.readAllBytes(new File(pszImageFile).toPath()), "");

调用解码API后,我们可以获得以下所有条形码结果:

if (results != null &amp;&amp; results.length > 0) {    String pszTemp = null;    iIndex = 0;    for (TextResult result : results) {        iIndex++;        pszTemp = String.format("  Barcode %d:", iIndex);        System.out.println(pszTemp);        pszTemp = String.format("    Page: %d", result.localizationResult.pageNumber + 1);        System.out.println(pszTemp);        if (result.barcodeFormat != 0) {            pszTemp = "    Type: " + result.barcodeFormatString;        } else {            pszTemp = "    Type: " + result.barcodeFormatString_2;        }        System.out.println(pszTemp);        pszTemp = "    Value: " + result.barcodeText;        System.out.println(pszTemp);        pszTemp = String.format("    Region points: {(%d,%d),(%d,%d),(%d,%d),(%d,%d)}",                result.localizationResult.resultPoints[0].x, result.localizationResult.resultPoints[0].y,                result.localizationResult.resultPoints[1].x, result.localizationResult.resultPoints[1].y,                result.localizationResult.resultPoints[2].x, result.localizationResult.resultPoints[2].y,                result.localizationResult.resultPoints[3].x, result.localizationResult.resultPoints[3].y);        System.out.println(pszTemp);        System.out.println();    }}

如果要将条形码读取功能部署到Web服务器以处理大量解码请求,则可以使用线程池来模拟方案:

ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10); for (int i = 1; i <= 1000; i++){    executor.execute(new Runnable(){        @Override        public void run() {            App test = new App();            test.decodefile(filename);            try {                Thread.sleep(200);            } catch (InterruptedException e) {                System.out.println(e);            }        }    });}executor.shutdown();

为了避免内存不足,请不要使用BufferedImage类,因为默认的JVM堆内存是有限的。

要在Visual Studio Code中使用输入图像运行应用程序,请将args添加到.vscode launch.json文件:

{    "version": "0.2.0",    "configurations": [        {            "type": "java",            "name": "Debug (Launch) - Current File",            "request": "launch",            "mainClass": "${file}",            "args": "images/AllSupportedBarcodeTypes.png"        },        {            "type": "java",            "name": "Debug (Launch)-App<test>",            "request": "launch",            "mainClass": "com.dynamsoft.App",            "projectName": "test",            "args": "images/AllSupportedBarcodeTypes.png"        }    ]}

在命令行工具中运行Java条码读取器

为了方便地运行具有依赖性的Java程序,我们可以将maven程序集插件添加到pom.xml中:

<plugin>    <artifactId>maven-assembly-plugin</artifactId>    <configuration>        <descriptorRefs>            <descriptorRef>jar-with-dependencies</descriptorRef>        </descriptorRefs>    </configuration></plugin>

然后将所有内容构建到一个jar文件中:

mvn clean install assembly:assembly

最后,使用以下命令运行Java条码读取器:

java -cp target/test-1.0-SNAPSHOT-jar-with-dependencies.jar com.dynamsoft.App images/AllSupportedBarcodeTypes.png

Dynamsoft Barcode Reader教程:如何使用Dynamsoft Java条形码阅读器扫描多个条形码

一旦Windows没有问题,我们就可以验证该程序是否可以在Linux上运行。

使用Docker创建了一个Linux环境。这是我的Dockerfile

FROM openjdk:11-stretchCOPY images/AllSupportedBarcodeTypes.png AllSupportedBarcodeTypes.pngCOPY target/test-1.0-SNAPSHOT-jar-with-dependencies.jar test-1.0-SNAPSHOT-jar-with-dependencies.jarCMD java -cp test-1.0-SNAPSHOT-jar-with-dependencies.jar com.dynamsoft.App AllSupportedBarcodeTypes.png

现在,构建Docker映像并在Docker Linux容器中运行Java程序:

docker rmi dynamsoft/barcode-reader -fdocker build -t dynamsoft/barcode-reader -f Dockerfile .docker run -it dynamsoft/barcode-reader

Dynamsoft Barcode Reader教程:如何使用Dynamsoft Java条形码阅读器扫描多个条形码

本教程内容到这里就完结了,希望对您有所帮助~您可以关注我们 了解更多产品资讯,或者下载Dynamsoft Barcode Reader SDK试用版免费体验~


想要购买产品正版授权,或了解更多产品信息请点击【咨询在线客服】

Dynamsoft Barcode Reader教程:如何使用Dynamsoft Java条形码阅读器扫描多个条形码


标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年1月1日
下一篇 2020年1月1日

相关推荐

发表回复

登录后才能评论