通过python扫描二维码/条形码并打印数据
作者:NZ卑微学python 时间:2022-03-12 11:32:32
需提前安装好pyzbar和opencv-python库(博主的电脑安装opencv-python库比较麻烦,但大部分都不会出现该问题)
安装方法:打开命令框输入 pip install pyzbar/opencv- python
接下来介绍代码
#首先导入本次所需要的库,最后一个csv是Python自带的csv表格操作库,这里我们需要把我们扫到的二维码信息都存入csv表格里。
import cv2
from pyzbar import pyzbar
import csv
#然后我们设置一个变量,来存放我们扫到的码的信息,我们每次扫描一遍都会要检测扫描到的码是不是之前扫描到的,
# 如果没有就存放到这里。接着我们调用opencv的方法来实例化一个摄像头,
# 最后我们设置一些我们存放码信息的表格的路径。
found = set()
capture = cv2.VideoCapture(0)
#存放数据的表格
PATH = "test.csv"
#不停的用摄像头来采集条码,
while(1):
# 首先我们要用刚才实例化的摄像头来采集实时的照片,
ret,frame = capture.read()
#找到图像中的条形码并进行解码
test = pyzbar.decode(frame)
# 循环检测到的条形码
for tests in test:
# 先将它转换成字符串
testdate = tests.data.decode('utf-8')
testtype = tests.type
# 绘出图像上条形码的数据和条形码类型
printout = "{} ({})".format(testdate, testtype)
if testdate not in found:
# 向终端打印条形码数据和条形码类型
print("[INFO] Found {} barcode: {}".format(testtype, testdate))
print(printout)
#存放扫描数据
if testdate not in found:
with open(PATH,'x+') as f:
#x+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。
csv_write = csv.writer(f)
date = [testdate]
csv_write.writerow(date)
found.add(testdate)
cv2.imshow('Test',frame)
if cv2.waitKey(1) == ord('q'):
break
如果遇到在命令符中无法安装opencv-python库的话,手动在该网址https://www.lfd.uci.edu/~gohlke/pythonlibs/下载对应的whl包,手动安装。
来源:https://blog.csdn.net/qq_40402988/article/details/103030258
标签:python,二维码,条形码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
文案是网站的性格
2009-09-02 13:05:00
![](https://img.aspxhome.com/file/UploadPic/20099/2/01-84s.jpg)
Python中itertools库的四个函数介绍
2021-05-04 14:34:05
python 初始化一个定长的数组实例
2022-12-27 18:59:36
python模块内置属性概念及实例
2023-11-23 15:34:27
OpenCV中VideoCapture类的使用详解
2022-10-26 18:38:15
Python 实现某个功能每隔一段时间被执行一次的功能方法
2021-11-14 09:57:25
Python Flask-Login模块使用案例详解
2021-10-14 21:01:17
![](https://img.aspxhome.com/file/2023/0/90590_0s.png)
numpy使用fromstring创建矩阵的实例
2022-02-23 09:29:49
Python使用ClickHouse的实践与踩坑记录
2023-06-12 21:45:44
![](https://img.aspxhome.com/file/2023/3/59643_0s.jpg)
Python基础数据类型tuple元组的概念与用法
2022-11-25 03:59:16
很无聊的一个找碴游戏
2008-07-02 13:10:00
python openpyxl打开文件关闭问题
2023-02-19 17:27:42
![](https://img.aspxhome.com/file/2023/8/92108_0s.png)
详解Python中映射类型的内建函数和工厂函数
2022-10-15 06:24:55
asp我对后台安全的一些做法
2011-09-01 19:22:09
对python csv模块配置分隔符和引用符详解
2023-04-14 13:52:20
详解配置Django的Celery异步之路踩坑
2022-11-25 22:06:45
python爬虫入门教程--HTML文本的解析库BeautifulSoup(四)
2023-08-12 23:45:10
解析SQL server与asp 互操作的时间处理
2009-02-05 16:13:00
Python第三方库h5py_读取mat文件并显示值的方法
2022-06-24 15:48:57
浅析Windows 嵌入python解释器的过程
2023-01-26 11:43:28