目录
1、显示逻辑
2、业务逻辑
二维码的产生历史可以追溯到20世纪80年代。当时,日本Denso Wave公司为了追踪汽车零部件的运输和销售情况,开发了一种能够存储大量信息的条形码,这就是二维码的前身。到了20世纪90年代,随着手机和照相机的普及,人们开始研究如何将二维码应用到手机和相机上,以实现更快速、更便捷的数据传输和识别。
QR码(也就是我们常说的二维码)可存储 7000 多个字符,由相机等设备读取,并从像素图像中解析出包含的信息,读取速度比其他条码快得多。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'qr.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(703, 409)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setMarkdown("")
self.textEdit.setObjectName("textEdit")
self.horizontalLayout.addWidget(self.textEdit)
self.verticalLayout.addLayout(self.horizontalLayout)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setObjectName("pushButton_2")
self.verticalLayout.addWidget(self.pushButton_2)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setObjectName("label_3")
self.horizontalLayout_2.addWidget(self.label_3)
self.textEdit_2 = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit_2.setObjectName("textEdit_2")
self.horizontalLayout_2.addWidget(self.textEdit_2)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3.addLayout(self.verticalLayout)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label_2.setMidLineWidth(0)
self.label_2.setObjectName("label_2")
self.horizontalLayout_3.addWidget(self.label_2)
self.horizontalLayout_3.setStretch(0, 1)
self.horizontalLayout_3.setStretch(1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 703, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "写入数据"))
self.textEdit.setHtml(_translate("MainWindow", "\n"
"\n"
"
"))
self.pushButton.setText(_translate("MainWindow", "生成二维码"))
self.pushButton_2.setText(_translate("MainWindow", "读取二维码"))
self.label_3.setText(_translate("MainWindow", "读取数据"))
self.label_2.setText(_translate("MainWindow", "QR"))
import sys
import qrcode
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from PyQt5.QtGui import QPixmap, QImage
from qr import Ui_MainWindow
import cv2
import numpy as np
class Ui_logo_ui(QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setWindowTitle("二维码生成与读取")
self.img = []
self.data = ""
self.ui.pushButton.clicked.connect(self.generateqrcode)
self.ui.pushButton_2.clicked.connect(self.readqrcode)
def generateqrcode(self):
self.data = self.ui.textEdit.toPlainText()
img= qrcode.make(data=self.data)
img.save('qrcode.png')
self.img=cv2.imread('qrcode.png')
self.show_cv_img(self.img, self.ui.label_2)
def readqrcode(self):
det = cv2.QRCodeDetector()
val, pts, st_code = det.detectAndDecode(self.img)
self.ui.textEdit_2.setPlainText(val)
def show_cv_img(self, img, label):
shrink = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
QtImg = QImage(shrink.data,
shrink.shape[1],
shrink.shape[0],
shrink.shape[1] * 3,
QImage.Format_RGB888)
jpg_out = QPixmap(QtImg).scaled(
label.width(), label.height())
label.setPixmap(jpg_out)
# 添加一个退出的提示事件
def closeEvent(self, event):
"""我们创建了一个消息框,上面有俩按钮:Yes和No.第一个字符串显示在消息框的标题栏,第二个字符串显示在对话框,
第三个参数是消息框的俩按钮,最后一个参数是默认按钮,这个按钮是默认选中的。返回值在变量reply里。"""
reply = QMessageBox.question(self, 'Message', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No,
QMessageBox.No)
# 判断返回值,如果点击的是Yes按钮,我们就关闭组件和应用,否则就忽略关闭事件
if reply == QMessageBox.Yes:
cv2.destroyAllWindows()
event.accept()
else:
event.ignore()
if __name__ == "__main__":
app = QApplication(sys.argv)
mainWindow = Ui_logo_ui()
mainWindow.show()
sys.exit(app.exec_())