使用语言:python,需要用到cv2库。主要(唯一)功能:识别电脑摄像头前面的二维码。能够实现镜像显示图像,同一个二维码在镜头前晃动避免重复识别。
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[0].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_())
|