Pyqt+QRcode 生成 识别 二维码
1.生成二维码
python生成二维码是件很简单的事,使用第三方库Python QRCode就可生成二维码,我用Pyqt给QRcode打个壳
一.python-qrcode介绍
python-qrcode是个用来生成二维码图片的第三方模块,依赖于 PIL 模块和 qrcode 库。
PIL下载地址:
https://pypi.python.org/pypi/PIL/1.1.6 或
http://www.pythonware.com/products/pil/
qrcode下载地址:
https://pypi.python.org/pypi/qrcode
安装:
1 python setup.py install
简单用法
1 import qrcode 2 img = qrcode.make('hello, qrcode') 3 img.save('test.png')
高级用法
1 import qrcode 2 qr = qrcode.QRCode( 3 version=1, 4 error_correction=qrcode.constants.ERROR_CORRECT_L, 5 box_size=10, 6 border=4, 7 ) 8 qr.add_data('hello, qrcode') 9 qr.make(fit=True) 10 img = qr.make_image() 11 img.save('test.png')
参数说明:
version:值为1~40的整数,控制二维码的大小(最小值是1,是个12×12的矩阵)。 如果想让程序自动确定,将值设置为 None 并使用 fit 参数即可。 error_correction:控制二维码的错误纠正功能。可取值下列4个常量。 box_size:控制二维码中每个小格子包含的像素数。 border:控制边框(二维码与图片边界的距离)包含的格子数(默认为4,是相关标准规定的最小值) |
二. 打壳
做一个Ui转换为Py文件, import qrcode 后就可以生成二维码了
完整代码如下:
1 # -*- coding: utf-8 -*- 2 3 # Form implementation generated from reading ui file 'Qrcode.ui' 4 # 5 # Created: Thu Jan 22 09:32:41 2015 6 # by: PyQt4 UI code generator 4.10.3 7 # 8 # WARNING! All changes made in this file will be lost! 9 10 from PyQt4 import QtCore, QtGui 11 12 try: 13 _fromUtf8 = QtCore.QString.fromUtf8 14 except AttributeError: 15 def _fromUtf8(s): 16 return s 17 18 try: 19 _encoding = QtGui.QApplication.UnicodeUTF8 20 def _translate(context, text, disambig): 21 return QtGui.QApplication.translate(context, text, disambig, _encoding) 22 except AttributeError: 23 def _translate(context, text, disambig): 24 return QtGui.QApplication.translate(context, text, disambig) 25 26 class Ui_qrcode(object): 27 def setupUi(self, qrcode): 28 qrcode.setObjectName(_fromUtf8("qrcode")) 29 qrcode.setEnabled(True) 30 qrcode.resize(515, 424) 31 self.groupBoxshow = QtGui.QGroupBox(qrcode) 32 self.groupBoxshow.setGeometry(QtCore.QRect(50, 120, 431, 281)) 33 sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred) 34 sizePolicy.setHorizontalStretch(0) 35 sizePolicy.setVerticalStretch(0) 36 sizePolicy.setHeightForWidth(self.groupBoxshow.sizePolicy().hasHeightForWidth()) 37 self.groupBoxshow.setSizePolicy(sizePolicy) 38 self.groupBoxshow.setMinimumSize(QtCore.QSize(0, 0)) 39 self.groupBoxshow.setAutoFillBackground(True) 40 self.groupBoxshow.setCheckable(False) 41 self.groupBoxshow.setObjectName(_fromUtf8("groupBoxshow")) 42 self.label = QtGui.QLabel(self.groupBoxshow) 43 self.label.setGeometry(QtCore.QRect(10, 20, 401, 241)) 44 self.label.setText(_fromUtf8("")) 45 self.label.setObjectName(_fromUtf8("label")) 46 self.groupBox = QtGui.QGroupBox(qrcode) 47 self.groupBox.setGeometry(QtCore.QRect(50, 20, 431, 91)) 48 self.groupBox.setObjectName(_fromUtf8("groupBox")) 49 self.horizontalLayoutWidget = QtGui.QWidget(self.groupBox) 50 self.horizontalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 401, 80)) 51 self.horizontalLayoutWidget.setObjectName(_fromUtf8("horizontalLayoutWidget")) 52 self.horizontalLayout = QtGui.QHBoxLayout(self.horizontalLayoutWidget) 53 self.horizontalLayout.setMargin(0) 54 self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) 55 self.label_url = QtGui.QLabel(self.horizontalLayoutWidget) 56 self.label_url.setObjectName(_fromUtf8("label_url")) 57 self.horizontalLayout.addWidget(self.label_url) 58 self.lineEditInput = QtGui.QLineEdit(self.horizontalLayoutWidget) 59 self.lineEditInput.setObjectName(_fromUtf8("lineEditInput")) 60 self.horizontalLayout.addWidget(self.lineEditInput) 61 self.pushButtonOk = QtGui.QPushButton(self.horizontalLayoutWidget) 62 self.pushButtonOk.setObjectName(_fromUtf8("pushButtonOk")) 63 self.horizontalLayout.addWidget(self.pushButtonOk) 64 65 self.retranslateUi(qrcode) 66 QtCore.QMetaObject.connectSlotsByName(qrcode) 67 68 def retranslateUi(self, qrcode): 69 qrcode.setWindowTitle(_translate("qrcode", "Pyqt Qrcode", None)) 70 self.groupBoxshow.setTitle(_translate("qrcode", "二维码显示", None)) 71 self.groupBox.setTitle(_translate("qrcode", "填写二维码内容", None)) 72 self.label_url.setText(_translate("qrcode", "URL地址:", None)) 73 self.pushButtonOk.setText(_translate("qrcode", "生成二维码", None)) 74 75 76 import qrcode 77 class showQr(QtGui.QWidget): 78 def __init__(self): 79 super(showQr, self).__init__() 80 self.ui_qr = Ui_qrcode() 81 self.ui_qr.setupUi(self) 82 # PyQT禁止窗口最大化按钮: 83 self.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint) 84 # PyQT禁止调整窗口大小: 85 self.setFixedSize(self.width(), self.height()) 86 87 # 触发信号槽 88 QtGui.QWidget.connect(self.ui_qr.pushButtonOk, QtCore.SIGNAL('clicked()'), self.generateQr) 89 # self.ui_qr.pushButtonOk.clicked.connect(self.generateQr) 90 91 def generateQr(self): 92 textcontent=self.ui_qr.lineEditInput.text() 93 if textcontent=='': 94 QtGui.QMessageBox.information(self, (u'提示'),(u'请填写URL地址'),QtGui.QMessageBox.Yes) 95 else: 96 filename='Qrcode.png' 97 qr = qrcode.QRCode( 98 version=None, 99 error_correction=qrcode.constants.ERROR_CORRECT_L, 100 box_size=5, 101 border=4, 102 ) 103 qr.add_data(textcontent) 104 qr.make(fit=True) 105 img = qr.make_image() 106 img.save(filename) 107 # img = qrcode.make(textcontent) 108 # img.save(filename) 109 self.ui_qr.label.setPixmap(QtGui.QPixmap(filename)) 110 111 112 113 # 重载keyPressEvent , 当按下Esc退出 114 def keyPressEvent(self, event): 115 if event.key() ==QtCore.Qt.Key_Escape: 116 self.close() 117 118 119 if __name__ == "__main__": 120 import sys 121 app = QtGui.QApplication(sys.argv) 122 qr = showQr() 123 qr.show() 124 sys.exit(app.exec_())
运行效果:
2.识别二维码
解析二维码用到zbar模块
扯蛋的zbar 在Pypi中只有py25 和py26 版本,更新时间还是2009年, 下载后py27还用不了!
最后在github 中找到支持Py27的zbar库
地址: https://github.com/jacobvalenta/zbar-py27-msi
one_qr_code.png
代码:
1 from PIL import Image 2 import zbar 3 4 def get_QR(): 5 scanner = zbar.ImageScanner() 6 scanner.parse_config("enable") 7 pil = Image.open("one_qr_code.png").convert('L') 8 width, height = pil.size 9 raw = pil.tostring() 10 image = zbar.Image(width, height, 'Y800', raw) 11 scanner.scan(image) 12 data = '' 13 for symbol in image: 14 data+=symbol.data 15 del(image) 16 return data 17 18 qrdata= get_QR() 19 print(qrdata)
返回解析结果:
http://weixin.qq.com/r/n0zE3NfEaooVrZO-9xmN