翻譯|使用教程|編輯:楊鵬連|2021-03-04 16:14:06.970|閱讀 420 次
概述:Qt Designer是用于使用Qt小部件構建跨平臺圖形用戶界面(GUI)的Qt工具。在本文中,我將使用Qt設計器通過適用于Windows,Linux,macOS和Raspberry Pi OS的Dynamsoft Python條形碼SDK創建高級桌面條形碼讀取器應用程序。該應用程序包含Dynamsoft條碼閱讀器的所有強大功能。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Dynamsoft Barcode Reader SDK一款多功能的條碼讀取控件,只需要幾行代碼就可以將條碼讀取功能嵌入到Web或桌面應用程序。這可以節省數月的開發時間和成本。能支持多種圖像文件格式以及從攝像機或掃描儀獲取的DIB格式。使用Dynamsoft Barcode Reader SDK,你可以創建強大且實用的條形碼掃描儀軟件,以滿足你的業務需求。
點擊下載Dynamsoft Barcode Reader最新版
Qt Designer是用于使用Qt小部件構建跨平臺圖形用戶界面(GUI)的Qt工具。在本文中,我將使用Qt設計器通過適用于Windows,Linux,macOS和Raspberry Pi OS的Dynamsoft Python條形碼SDK創建高級桌面條形碼讀取器應用程序。該應用程序包含Dynamsoft條碼閱讀器的所有強大功能。
使用Qt Designer構造UI
所需的Qt小部件包括:
菜單欄
pyside2-uic design.ui -o design.py用Python構建跨平臺條形碼閱讀器
讓我們逐步構建條形碼閱讀器應用程序。
要求
安裝相關的Python軟件包。對于Raspberry Pi OS,我們可以通過以下apt-get命令安裝PySide2 :
python3 -m pip install opencv-python python3 -m pip install dbr # Windows, Linux and macOS python3 -m pip install PySide2 # Raspberry Pi OS sudo apt-get install python3-pyside2.qt3dcore python3-pyside2.qt3dinput python3-pyside2.qt3dlogic python3-pyside2.qt3drender python3-pyside2.qtcharts python3-pyside2.qtconcurrent python3-pyside2.qtcore python3-pyside2.qtgui python3-pyside2.qthelp python3-pyside2.qtlocation python3-pyside2.qtmultimedia python3-pyside2.qtmultimediawidgets python3-pyside2.qtnetwork python3-pyside2.qtopengl python3-pyside2.qtpositioning python3-pyside2.qtprintsupport python3-pyside2.qtqml python3-pyside2.qtquick python3-pyside2.qtquickwidgets python3-pyside2.qtscript python3-pyside2.qtscripttools python3-pyside2.qtsensors python3-pyside2.qtsql python3-pyside2.qtsvg python3-pyside2.qttest python3-pyside2.qttexttospeech python3-pyside2.qtuitools python3-pyside2.qtwebchannel python3-pyside2.qtwebsockets python3-pyside2.qtwidgets python3-pyside2.qtx11extras python3-pyside2.qtxml python3-pyside2.qtxmlpatterns python3-pyside2uic加載UI文件
因為UI代碼已經準備好,所以我們可以按以下方式加載它:
''' Usage: app.py <license.txt> ''' import sys from PySide2.QtGui import QPixmap, QImage from PySide2.QtWidgets import QApplication, QMainWindow, QInputDialog from PySide2.QtCore import QFile, QTimer from PySide2.QtWidgets import * from design import Ui_MainWindow from barcode_manager import * import os import cv2 from dbr import EnumBarcodeFormat, EnumBarcodeFormat_2 class MainWindow(QMainWindow): def __init__(self, license): super(MainWindow, self).__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) def main(): try: with open(sys.argv[1]) as f: license = f.read() except: license = "" app = QApplication(sys.argv) window = MainWindow(license) window.show() sys.exit(app.exec_()) if __name__ == '__main__': print(__doc__) main()我們可以使用有效的條形碼許可證密鑰啟動應用程序,也可以稍后通過菜單欄激活SDK。
下一步是創建一些全局變量并將小部件信號綁定到插槽:
# Initialization self._all_data = {} self._results = None # Dynamsoft Barcode Reader self._barcodeManager = BarcodeManager(license) # Create a timer. self.timer = None # Open camera self._cap = None # self.openCamera() # Resolution list self.ui.comboBox.currentTextChanged.connect(self.onComboBoxChanged) # The current path. self._path = os.path.dirname(os.path.realpath(__file__)) # Camera button self.ui.pushButton_open.clicked.connect(self.openCamera) self.ui.pushButton_stop.clicked.connect(self.stopCamera) # Load file self.ui.actionOpen_File.triggered.connect(self.openFile) # Load directory self.ui.actionOpen_Folder.triggered.connect(self.openFolder) # Export template self.ui.actionExport_template.triggered.connect(self.exportTemplate) # About self.ui.actionAbout.triggered.connect(self.about) # Set license self.ui.actionEnter_License_Key.triggered.connect(self.setLicense) ## List widget self.ui.listWidget.currentItemChanged.connect(self.currentItemChanged) ## Template load button self.ui.pushButton_template.clicked.connect(self.loadTemplate) ## Template export button self.ui.pushButton_export_template.clicked.connect(self.exportTemplate)用于打開和保存文件的文件對話框
基本的文件操作包括加載圖像文件,從文件夾加載一批圖像文件,加載和保存模板文件。
def openFile(self): filename = QFileDialog.getOpenFileName(self, 'Open File', self._path, "Barcode images (*)") if filename is None or filename[0] == '': return filename = filename[0]
def openFolder(self): dir = QFileDialog.getExistingDirectory(self, 'Open Folder', self._path, QFileDialog.ShowDirsOnly) if dir is '': return files = [os.path.join(dir, f) for f in os.listdir(dir) if os.path.isfile(os.path.join(dir, f))] if len(files) == 0: return for filename in files: self.appendFile(filename)
def exportTemplate(self): filename = QFileDialog.getSaveFileName(self, 'Save File', self._path, "Barcode Template (*.json)") if filename is None or filename[0] == '': return filename = filename[0]解碼JPEG,PNG,TIFF,PDF和GIF文件中的條形碼
如果您更喜歡OpenCV API,則可以像下面這樣解碼條形碼:
frame = cv2.imread(filename) results = self._reader.decode_buffer(frame)但是,OpenCV僅支持JPEG,PNG和TIFF。Dynamsoft Python條形碼SDK提供的圖像編解碼器還支持PDF和GIF文件。要涵蓋更多圖像格式,我們可以致電decode_file():
results = self._reader.decode_file(filename)別忘了我們需要在標簽窗口小部件中顯示圖像。因此,還有一個步驟是從中間結果中獲取圖像數據,然后將其轉換為NumPy數組:
intermediate_results = self._reader.get_all_intermediate_results() imageData = intermediate_results[0].results[0] buffer = imageData.bytes width = imageData.width height = imageData.height stride = imageData.stride format = imageData.image_pixel_format channel = 3 if format == EnumImagePixelFormat.IPF_RGB_888: channel = 3 elif format == EnumImagePixelFormat.IPF_BINARY or format == EnumImagePixelFormat.IPF_GRAYSCALED or format == EnumImagePixelFormat.IPF_BINARYINVERTED: channel = 1 if format == EnumImagePixelFormat.IPF_BINARY or format == EnumImagePixelFormat.IPF_BINARYINVERTED: whiteValue = 1 if format == EnumImagePixelFormat.IPF_BINARYINVERTED: whiteValue = 0 binData = bytearray(len(buffer) << 3) count = 0 for pos in range(len(buffer)): for bit in range(7, -1, -1): if (buffer[pos] >> bit) & 0x01 == whiteValue: binData[count] = 255 else: binData[count] = 0 count += 1 frame = np.ndarray((height, width, channel), np.uint8, binData, 0, (stride << 3, channel, 1)) else: frame = np.ndarray((height, width, channel), np.uint8, buffer, 0, (stride, channel, 1))顯示網絡攝像頭流
我們觸發一個計時器來顯示網絡攝像頭流:
def openCamera(self): width = 640; height = 480 self._cap = cv2.VideoCapture(0) self._cap.set(cv2.CAP_PROP_FRAME_WIDTH, width) self._cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height) if not self._cap.isOpened(): self.showMessageBox('Error', "Failed to open camera.") return self.timer = QTimer() self.timer.timeout.connect(self.nextFrameUpdate) self.timer.start(1000./24) def nextFrameUpdate(self): ret, frame = self._cap.read() frame, self._results = self._barcodeManager.decode_frame(frame) self.showResults(frame, self._results)創建用于實時條形碼掃描的Python流程
解碼條形碼是一項占用大量CPU的任務。為了避免實時阻塞UI,我們創建了一個Python進程來運行代碼:
def create_barcode_process(self): size = 1 self.frameQueue = Queue(size) self.resultQueue = Queue(size) self.barcodeScanning = Process(target=process_barcode_frame, args=(self._license, self.frameQueue, self.resultQueue, self._template, self._types, self._types2)) self.barcodeScanning.start()運行GUI條形碼閱讀器
python3 app_advanced.py license.txt視窗
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: