当前位置:   article > 正文

python zxing 识别条码_python条码识别

pyzxing识别条码

#written by DY

#http://dyblog.tk

#e-mail:duyun888888@qq.com

import cv2

from PILimport Image, ImageDraw, ImageFont

import csv

import pyzbar.pyzbar as pyzbar

import numpy

import time

#-----------------time----------

time1= (time.strftime("%Y!%m@%d(%H*%M)%S`", time.localtime()))

time2= time1.replace('!','年')

time3= time2.replace('@','月')

time4= time3.replace('(','日')

time5= time4.replace('*','时')

time6= time5.replace(')','分')

timenow= time6.replace('`','秒')

#-----------------time done----

barcodeData1= ''

found= set()

capture= cv2.VideoCapture(0,cv2.CAP_DSHOW)

Files= "qrcode+barcode.dat"#新建一个日志,方便查阅(不须要能够删除)

while(1):

ret,frame= capture.read()

test= pyzbar.decode(frame)

gray= cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

barcodes= pyzbar.decode(gray)

for barcodein barcodes:

(x, y, w, h)= barcode.rect

cv2.rectangle(frame, (x, y), (x+ w, y+ h), (255,255,0),2)

barcodeData= barcode.data.decode("utf-8")

barcodeType= barcode.type

if barcodeData==''or barcodeData!=barcodeData1:

barcodeData1= barcodeData

#======================能够不须要,这是在图片上进行标注,里面采用了字体文件==============================

img_PIL= Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

font= ImageFont.truetype('arialbd.ttf',25)#前面是字体文件,后面是字号

fillColor= (0,255,0)

position= (x, y-25)

strl= barcodeData

draw= ImageDraw.Draw(img_PIL)

draw.text(position, strl,font=font,fill=fillColor)

img_PIL.save('Identification_results.jpg','jpeg')#保存一张标记过的图片

#====================================================

print("Recognize result>>> type: {0}  content: {1}".format(barcodeType, barcodeData))

else:

pass

if barcodeTypenot in foundor barcodeDatanot in found:

withopen(Files,'a+') as w:

csv_write= csv.writer(w)

date= ['类型:'+barcodeType+'  识别结果:'+barcodeData+'   时间:'+timenow]

csv_write.writerow(date)

found.add(barcodeData)

cv2.imshow('qrcode+barcode',frame)

k= cv2.waitKey(1)

if k== 27:

break

#written by DY

#http://dyblog.tk

#e-mail:duyun888888@qq.com

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/243037
推荐阅读
相关标签
  

闽ICP备14008679号