qt如何给label赋值_如何用Qt Python创建简单的桌面条形码应用

本文介绍了如何结合Qt for Python和Dynamsoft Barcode Reader SDK创建一个简单的Windows桌面条形码读取应用。首先讲解了Qt for Python和SDK的安装,接着展示如何设计GUI界面,加载图片并调用接口识别条形码,最后提供了源码链接。

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代码。

运行Python37Libsite-packagesPySide2designer.exe

94770e0c0bf469b7e3882ad06e746245.png

设计好界面之后保存到.ui文件。

Python37Scriptspyside2-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)DynamsoftBarcode Reader 6.4.1Images', "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()
 
        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.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(index) + "n"
            out += "Barcode format: " + result[0] + 'n'
            out += "Barcode value: " + result[1] + 'n'
            out += '-----------------------------------n'
            index += 1
 
        self.results.setText(out)

运行程序:

python barcode-reader.py

62c36242ebe4111103382254574f872b.png

源码

https://github.com/dynamsoft-dbr/python/tree/master/examples/qt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值