简单的条形码识别 一段可爱的缝合怪程序

2021/6/27 17:20:59

本文主要是介绍简单的条形码识别 一段可爱的缝合怪程序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

import cv2     #调用cv2库
from pyzbar import pyzbar  #调用pyzbar条形码识别库
fa=set() #建立不重复集合fa
flag=0
def scan_qrcode(qrcode):    #解码用子函数
    datas = pyzbar.decode(qrcode)
    for tests in datas:
     testdate = tests.data.decode('utf-8')   #对条形码信息解码
     testtype = tests.type      #读取条形码信息
     printout = "{} ({})".format(testdate, testtype)
    if testdate not in fa:     #判断新识别到的信息是否已经存在
        print(printout)        #打印到对话框
    if testdate not in fa:     #判断新识别到的信息是否已经存在
      with open("E:/chengxu/01.txt", "a+") as f:    #文档地址
	        f.write(testdate+'\n')   #将信息写入文档
      f.close()      #关闭文档
      fa.add(testdate)  #将信息写入不重复集合fa

#主程序                         
cap = cv2.VideoCapture(0)
#打开摄像头
while True:
    
    ret, frame = cap.read()  #显示当前帧
    cv2.imshow('video', frame)
    # 解析二维码
    text = None  
    try:
        text = scan_qrcode(frame)  #将截取的图片送入子函数解码
    except Exception :
        pass
    if text:
        print(text)
        break

    key = cv2.waitKey(10)  #设置显示框停留时间
    if key == ord('q'):    #键盘Q为关键点,关闭摄像头
        break
    c= cv2.waitKey(30) & 0xff 
    if c==27:              #键盘esc为关键点,退出程序识别
      cap.release()
      break
cv2.destroyAllWindows()   #删除所有窗口

因为写了很久,忘了都有缝合谁的了,基本每一句都有解释,就不需要多余的文字叙述了。



这篇关于简单的条形码识别 一段可爱的缝合怪程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程