用少量代码即可实现一个二维码识别器
使用语言:python,需要用到cv2库。主要(唯一)功能:识别电脑摄像头前面的二维码。能够实现镜像显示图像,同一个二维码在镜头前晃动避免重复识别。``` python
import cv2
from PIL import Image
from pyzbar.pyzbar import decode
import random
import os
import sys
from PyQt5.QtWidgets import QApplication, QWidget,QSystemTrayIcon,QAction,QMenu,qApp,QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QCoreApplication
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit,
QTextEdit, QGridLayout, QApplication)
x=[]
class emp():
data = b'XXX'
global isExit
isExit = False
global lastResult
lastResult = ''
class MyQRRec(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# self.resize(850, 650)
# self.move(300, 300)
self.setGeometry(300, 200, 850, 650)
self.setWindowTitle('二维码识别')
grid = QGridLayout()
self.setLayout(grid)
grid.setSpacing(10)
titleEdit = QTextEdit()
titlePrompt = QLabel('把二维码放在摄像头前面:')
grid.addWidget(titlePrompt,1,1)
grid.addWidget(titleEdit, 2, 1)
self.show()
x = []
cap = cv2.VideoCapture(0)
#-----------------------------
cv2.namedWindow('Camera Feed')
while not(isExit):
ref,frame = cap.read()
#镜像显示
frame = cv2.flip(frame, 1)
cv2.imshow('Camera Feed', frame)
x=decode(frame)
app.processEvents()
if x!=[]:
y=x.data
y=y.decode()
global lastResult
if y!=lastResult:
titleEdit.setPlainText ( titleEdit.toPlainText()+y )
lastResult = y
cap.release()
cv2.destroyAllWindows()
x=['fdfdfd']
sys.exit()
QCoreApplication.instance().quit
def closeEvent(self, event):
global isExit
isExit =True
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyQRRec()
sys.exit(app.exec_())
``` :lol python这样用代码还是好多 果真是高手如云,学到了很多知识 不错,可以学习一下了。哈哈哈 这个有意思,可以用摄像头代替扫码枪么? 感谢分享不错不错学习啦 这个厉害,之前还遇到过这个需求,不过最后因为数量少还是用手机做了 这个厉害,学习一下QT5和CV2的运用,感谢分享 学习了,后面自己测试看看
页:
[1]
2