当我使用CPython通过Dynamsoft Barcode Reader 创建Python条码扩展时,我不得不考虑Python版本的兼容性,而且又不想在操作系统中安装所有Python版本,所以选择了Docker容器作为解决方案。
Dynamsoft Barcode Reader SDK一款多功能的条码读取控件,只需要几行代码就可以将条码读取功能嵌入到Web或桌面应用程序。这可以节省数月的开发时间和成本。能支持多种图像文件格式以及从摄像机或扫描仪获取的DIB格式。使用Dynamsoft Barcode Reader SDK,你可以创建强大且实用的条形码扫描仪软件,以满足你的业务需求。
Dynamsoft Barcode Reader正式版
当我使用CPython通过Dynamsoft Barcode Reader 创建Python条码扩展时,我不得不考虑Python版本的兼容性。但是,我不愿意在操作系统中安装所有Python版本。为了测试我的Python条形码应用程序在Windows中的兼容性,我选择了Docker容器作为解决方案。
使用Python Barcode SDK创建Docker映像
为Windows安装Docker桌面。
我没有安装Python 3.8,所以我从Docker集线器中提取了一个包含Python 3.8的Docker映像,并立即运行它:
docker run -it --rm python:3.8 bash
如果不存在目标Docker映像,则以上命令将自动触发下载。
要构建一个包含Dynamsoft Barcode Reader SDK的新Docker映像,请创建一个简单的Dockerfile:
FROM python:3.8RUN pip install dbr
构建Docker映像:
docker build --rm -t yushulx/dynamsoft-barcode-reader:python-3.8 .
运行一个容器以测试适用于Python的Dynamsoft条码阅读器是否可以工作:
docker run -it --rm yushulx/dynamsoft-barcode-reader:python-3.8

如果没有问题,请将Docker映像发布到Docker中心:
docker logindocker push yushulx/dynamsoft-barcode-reader:python-3.8
将本地文件夹挂载到Docker容器
我将Windows文件夹安装到Linux容器,并使用Python 3.8运行Python脚本。
打开Docker桌面的设置,并选择共享驱动器:
运行以下命令将Windows文件夹安装到Linux容器:
docker run -it --rm -v d:/code/docker:/dynamsoft yushulx/dynamsoft-barcode-reader:python-3.8 bash
使用Visual Studio Code远程容器调试Python代码
我喜欢使用Visual Studio Code编写和调试Python代码。
为VSCode安装Docker扩展。
按F1键运行“附加到运行容器”选项。
打开项目文件夹。
然后,您可以按F5键以远程调试代码。
这是完整的Python条形码检测代码:
import osfrom dbr import DynamsoftBarcodeReaderdbr = DynamsoftBarcodeReader()def InitLicense(license): dbr.InitLicense(license)def DecodeFile(fileName): try: results = dbr.DecodeFile(fileName) textResults = results["TextResults"] resultsLength = len(textResults) print("count: " + str(resultsLength)) if resultsLength != 0: for textResult in textResults: print(textResult["BarcodeFormatString"]) print(textResult["BarcodeText"]) localizationResult = textResult["LocalizationResult"] x1 = localizationResult["X1"] y1 = localizationResult["Y1"] x2 = localizationResult["X2"] y2 = localizationResult["Y2"] x3 = localizationResult["X3"] y3 = localizationResult["Y3"] x4 = localizationResult["X4"] y4 = localizationResult["Y4"] localizationPoints = [(x1,y1),(x2,y2),(x3,y3),(x4,y4)] print(localizationPoints) else : print("No barcode detected") except Exception as err: print(err)if __name__ == "__main__": # Get the license from https://www.dynamsoft.com/CustomerPortal/Portal/Triallicense.aspx licenseKey = "LICENSE-KEY" fileName = r"test.jpg" InitLicense(licenseKey) dir_path = os.path.dirname(os.path.realpath(__file__)) DecodeFile(os.path.join(dir_path, fileName))
本教程内容到这里就完结了,希望对您有所帮助哦~感兴趣的朋友可以继续关注我们 站,了解更多产品资讯!也可以下载Dynamsoft Barcode Reader试用版免费体验~
想要购买产品正版授权,或了解更多产品信息请点击【咨询在线客服】
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!