python初学者,在医院看到抽血时,在收集血管的瓶子上扫描一下二维码后,就可以录入信息,于是想着参考编写一个二维码扫描后打卡小程序,程序可以读取二维码信息作为一个输入姓名和id号,二维码解析从网上参考的,程序如下,后面会在此基础上加入多线程继续完善
from pyzbar.pyzbar import decode
import cv2
import numpy as np
import threading
import time
# param name is str
# param work_id is str
class Person:
def __init__(self, name, work_id):
self.name = name
self.work_id = work_id
self.work_on_time = 0
self.work_off_time = 0
def time_record(self, time):
if self.work_on_time == 0:
self.work_on_time = time
else:
self.work_off_time = time
# 对象列表
person_list = []
# 画框
def paint_line(info, img):
pts = np.array([info.polygon], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img, [pts], True, (255, 0, 255), 5)
pts2 = info.rect
cv2.putText(img, info.data.decode('utf-8'), (pts2[0], pts2[1]), cv2.FONT_HERSHEY_COMPLEX, 0.9, (255, 0, 255))
def decode_img_info(img):
# print('开始识别信息:')
for info in decode(img):
info_decode = info.data.decode('utf-8')
print(info)
print(info_decode)
# 画框
paint_line(info, img)
print((info_decode))
my_info = info_decode.split()
name_str = my_info[0][5:]
id_str = my_info[1][3:]
print(my_info)
print('姓名:', name_str)
print('工号:', id_str)
cnt = 0
for person in person_list:
cnt = cnt +1
if person.name == name_str and person.work_id == id_str:
print('已经有存储:\n 姓名:', name_str, ' 工号:', id_str)
break
if cnt == len(person_list):
cnt = 0
print('数据库中没有您的信息, 请至管理员添加信息')
def add_person():
person1 = Person('zhao', '349318')
person2 = Person('zhao', '1234')
person_list.append(person1)
person_list.append(person2)
if __name__ == '__main__':
add_person()
print('现在时间=', time.time())
print('二维码测试')
cap = cv2.VideoCapture(0)
while True:
is_succ, img = cap.read()
decode_img_info(img)
cv2.imshow('QRCode', img)
if (cv2.waitKey(1) == 27):
break
cap.release()
cv2.destroyAllWindows()