Qt for Python可以快速跨平台的GUI应用。这篇文章分享下如何结合Dynamsoft Barcode Reader SDK来创建一个简单的读码应用。
安装Qt for Python
官方站点可以下载对应的wheel文件,或者通过命令行安装:
pip install pyside2
我之前用Python 3.6无法成功加载Qt:
Error:
Traceback (most recent call last):
File "test2.py", line 3, in <module>
from PySide2.QtWidgets import (QApplication, QLabel, QPushButton, QVBoxLayout, QWidget)
ImportError: DLL load failed: The specified procedure could not be found.
换成Python 3.7之后就好了。
安装Dynamsoft Barcode Reader
下载barcode sdk。
申请一个免费试用的license。
获取extension源码,然后编译安装:
python setup.py build install
简单的Windows桌面扫码应用
Qt安装之后可以用Qt designer来设计界面并生成Python代码。
运行Python37\Lib\site-packages\PySide2\designer.exe。
设计好界面之后保存到.ui文件。
用Python37\Scripts\pyside2-uic.exe把.ui文件转换成Python文件:
pyside2-uic -x *.ui -o ui.py
之后可以直接运行这个Python文件。
应用界面很简单。Button用于加载图片,Label用于显示图片,Text用于显示结果。
import sys
from PySide2.QtGui import QPixmap
from PySide2.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QWidget, QFileDialog, QTextEdit, QSizePolicy
from PySide2.QtCore import Slot, Qt, QStringListModel, QSize
import dbr
import os
class UI_Window(QWidget):
def __init__(self):
QWidget.__init__(self)
# The default barcode image.
dir_path = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(dir_path, 'image.tif')
# Create a layout.
layout = QVBoxLayout()
# Add a button
self.btn = QPushButton("Load an image")
self.btn.clicked.connect(self.pickFile)
layout.addWidget(self.btn)
# Add a label
self.label = QLabel()
self.label.setFixedSize(640, 640)
pixmap = self.resizeImage(filename)
self.label.setPixmap(pixmap)
layout.addWidget(self.label)
# Add a text area
self.results = QTextEdit()
self.readBarcode(filename)
layout.addWidget(self.results)
# Set the layout
self.setLayout(layout)
self.setWindowTitle("Dynamsoft Barcode Reader")
self.setFixedSize(800, 800)
点击按钮之后调用系统对话框来加载文件:
def pickFile(self):
# Load an image file.
filename = QFileDialog.getOpenFileName(self, 'Open file',
'E:\\Program Files (x86)\\Dynamsoft\\Barcode Reader 6.4.1\\Images', "Barcode images (*)")
# Show barcode images
pixmap = self.resizeImage(filename[0])
self.label.setPixmap(pixmap)
# Read barcodes
self.readBarcode(filename[0])
调节图片尺寸,让图片显示在固定区域内:
def resizeImage(self, filename):
pixmap = QPixmap(filename)
lwidth = self.label.maximumWidth()
pwidth = pixmap.width()
lheight = self.label.maximumHeight()
pheight = pixmap.height(www.meiwanyule.cn/ )
wratio = pwidth * 1.0 / lwidth
hratio = pheight * 1.0 / lheight
if pwidth > lwidth or pheight > lheight:
if wratio > hratio:
lheight = pheight / wratio
else:
lwidth = pwidth / hratio
scaled_pixmap = pixmap.www.tiaotiaoylzc.com/ scaled(lwidth, lheight)
return scaled_pixmap
else:
return pixmap
调用barcode接口识别条形码:
def readBarcode(self, filename):
dbr.initLicense("<Your License>")
results = dbr.decodeFile(filename, 0x3FF | 0x2000000 | 0x4000000 | 0x8000000 | 0x10000000)
out = ''
index = 0
for result in results:
out += "Index: " + str(www.hengda157.com index) + "\n"
out += "Barcode format:www.lezongyule.com " + result[0] + '\n'
out += "Barcode value: " + result[1] + '\n'
out += '-----------------------------------\n'
index += 1
self.results.setText(out)
运行程序:
python barcode-reader.py
如何用Qt Python创建简单的桌面条形码应用
最新推荐文章于 2019-10-15 23:48:48 发布