Python二维码生成识别实例详解
前言
在JavaWeb开发中,一般使用Zxing来生成和识别二维码,但是,Zxing的识别有点差强人意,不少相对模糊的二维码识别率很低。不过就最新版本的测试来说,识别率有了现显著提高。
对比
在没接触Python之前,曾使用Zbar的客户端进行识别,测了大概几百张相对模糊的图片,Zbar的识别速度要快很多,识别率也比Zxing稍微准确那边一丢丢,但是,稍微模糊一点就无法识别。相比之下,微信和支付宝的识别效果就逆天了。
代码案例
#-*-coding:utf-8-*-
importos
importqrcode
importtime
fromPILimportImage
frompyzbarimportpyzbar
"""
#升级pip并安装第三方库
pipinstall-Upip
pipinstallPillow
pipinstallpyzbar
pipinstallqrcode
"""
defmake_qr_code_easy(content,save_path=None):
"""
GenerateQRCodebydefault
:paramcontent:ThecontentencodedinQRCodeparams
:paramsave_path:ThepathwherethegeneratedQRCodeimagewillbesavedin.
Ifthepathisnotgiventheimagewillbeopenedbydefault.
"""
img=qrcode.make(data=content)
ifsave_path:
img.save(save_path)
else:
img.show()
defmake_qr_code(content,save_path=None):
"""
GenerateQRCodebygivenparams
:paramcontent:ThecontentencodedinQRCode
:paramsave_path:ThepathwherethegeneratedQRCodeimagewillbesavedin.
Ifthepathisnotgiventheimagewillbeopenedbydefault.
"""
qr_code_maker=qrcode.QRCode(version=2,
error_correction=qrcode.constants.ERROR_CORRECT_M,
box_size=8,
border=1,
)
qr_code_maker.add_data(data=content)
qr_code_maker.make(fit=True)
img=qr_code_maker.make_image(fill_color="black",back_color="white")
ifsave_path:
img.save(save_path)
else:
img.show()
defmake_qr_code_with_icon(content,icon_path,save_path=None):
"""
GenerateQRCodewithaniconinthecenter
:paramcontent:ThecontentencodedinQRCode
:paramicon_path:Thepathoficonimage
:paramsave_path:ThepathwherethegeneratedQRCodeimagewillbesavedin.
Ifthepathisnotgiventheimagewillbeopenedbydefault.
:exceptionFileExistsError:Ifthegivenicon_pathisnotexist.
Thiserrorwillberaised.
:return:
"""
ifnotos.path.exists(icon_path):
raiseFileExistsError(icon_path)
#First,generateanusualQRCodeimage
qr_code_maker=qrcode.QRCode(version=4,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=8,
border=1,
)
qr_code_maker.add_data(data=content)
qr_code_maker.make(fit=True)
qr_code_img=qr_code_maker.make_image(fill_color="black",back_color="white").convert('RGBA')
#Second,loadiconimageandresizeit
icon_img=Image.open(icon_path)
code_width,code_height=qr_code_img.size
icon_img=icon_img.resize((code_width//4,code_height//4),Image.ANTIALIAS)
#Last,addtheicontooriginalQRCode
qr_code_img.paste(icon_img,(code_width*3//8,code_width*3//8))
ifsave_path:
qr_code_img.save(save_path)
else:
qr_code_img.show()
defdecode_qr_code(code_img_path):
"""
DecodethegivenQRCodeimage,andreturnthecontent
:paramcode_img_path:ThepathofQRCodeimage.
:exceptionFileExistsError:Ifthegivencode_img_pathisnotexist.
Thiserrorwillberaised.
:return:Thelistofdecodedobjects
"""
ifnotos.path.exists(code_img_path):
raiseFileExistsError(code_img_path)
#Here,setonlyrecognizeQRCodeandignoreothertypeofcode
returnpyzbar.decode(Image.open(code_img_path),symbols=[pyzbar.ZBarSymbol.QRCODE],scan_locations=True)
if__name__=="__main__":
##简易版
#make_qr_code_easy("make_qr_code_easy","make_qr_code_easy.png")
#results=decode_qr_code("make_qr_code_easy.png")
#iflen(results):
#print(results[0].data.decode("utf-8"))
#else:
#print("Cannotrecognize.")
#
##参数版
#make_qr_code("make_qr_code","make_qr_code.png")
#results=decode_qr_code("make_qr_code.png")
#iflen(results):
#print(results[0].data.decode("utf-8"))
#else:
#print("Cannotrecognize.")
#
#带中间logo的
#make_qr_code_with_icon("https://blog.52itstyle.vip","icon.jpg","make_qr_code_with_icon.png")
#results=decode_qr_code("make_qr_code_with_icon.png")
#iflen(results):
#print(results[0].data.decode("utf-8"))
#else:
#print("Cannotrecognize.")
#识别答题卡二维码16识别失败
t1=time.time()
count=0
foriinrange(1,33):
results=decode_qr_code(os.getcwd()+"\\img\\"+str(i)+".png")
iflen(results):
print(results[0].data.decode("utf-8"))
else:
print("Cannotrecognize.")
count+=1
t2=time.time()
print("识别失败数量:"+str(count))
print("测试时间:"+str(int(round(t2*1000))-int(round(t1*1000))))
测试了32张精挑细选的模糊二维码:
识别失败数量:1 测试时间:130
使用最新版的Zxing识别失败了三张。
源码
https://gitee.com/52itstyle/Python/tree/master/Day13
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短