Python二維碼生成識別實(shí)例詳解
前言
在 JavaWeb 開發(fā)中,一般使用 Zxing 來生成和識別二維碼,但是,Zxing 的識別有點(diǎn)差強(qiáng)人意,不少相對模糊的二維碼識別率很低。不過就最新版本的測試來說,識別率有了現(xiàn)顯著提高。
對比
在沒接觸 Python 之前,曾使用 Zbar 的客戶端進(jìn)行識別,測了大概幾百張相對模糊的圖片,Zbar的識別速度要快很多,識別率也比 Zxing 稍微準(zhǔn)確那邊一丟丟,但是,稍微模糊一點(diǎn)就無法識別。相比之下,微信和支付寶的識別效果就逆天了。
代碼案例
# -*- coding:utf-8 -*- import os import qrcode import time from PIL import Image from pyzbar import pyzbar """ # 升級 pip 并安裝第三方庫 pip install -U pip pip install Pillow pip install pyzbar pip install qrcode """ def make_qr_code_easy(content, save_path=None): """ Generate QR Code by default :param content: The content encoded in QR Codeparams :param save_path: The path where the generated QR Code image will be saved in. If the path is not given the image will be opened by default. """ img = qrcode.make(data=content) if save_path: img.save(save_path) else: img.show() def make_qr_code(content, save_path=None): """ Generate QR Code by given params :param content: The content encoded in QR Code :param save_path: The path where the generated QR Code image will be saved in. If the path is not given the image will be opened by default. """ 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") if save_path: img.save(save_path) else: img.show() def make_qr_code_with_icon(content, icon_path, save_path=None): """ Generate QR Code with an icon in the center :param content: The content encoded in QR Code :param icon_path: The path of icon image :param save_path: The path where the generated QR Code image will be saved in. If the path is not given the image will be opened by default. :exception FileExistsError: If the given icon_path is not exist. This error will be raised. :return: """ if not os.path.exists(icon_path): raise FileExistsError(icon_path) # First, generate an usual QR Code image 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, load icon image and resize it 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, add the icon to original QR Code qr_code_img.paste(icon_img, (code_width * 3 // 8, code_width * 3 // 8)) if save_path: qr_code_img.save(save_path) else: qr_code_img.show() def decode_qr_code(code_img_path): """ Decode the given QR Code image, and return the content :param code_img_path: The path of QR Code image. :exception FileExistsError: If the given code_img_path is not exist. This error will be raised. :return: The list of decoded objects """ if not os.path.exists(code_img_path): raise FileExistsError(code_img_path) # Here, set only recognize QR Code and ignore other type of code return pyzbar.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") # if len(results): # print(results[0].data.decode("utf-8")) # else: # print("Can not recognize.") # # # 參數(shù)版 # make_qr_code("make_qr_code", "make_qr_code.png") # results = decode_qr_code("make_qr_code.png") # if len(results): # print(results[0].data.decode("utf-8")) # else: # print("Can not recognize.") # # 帶中間 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") # if len(results): # print(results[0].data.decode("utf-8")) # else: # print("Can not recognize.") # 識別答題卡二維碼 16 識別失敗 t1 = time.time() count = 0 for i in range(1, 33): results = decode_qr_code(os.getcwd()+"\\img\\"+str(i)+".png") if len(results): print(results[0].data.decode("utf-8")) else: print("Can not recognize.") count += 1 t2 = time.time() print("識別失敗數(shù)量:" + str(count)) print("測試時(shí)間:" + str(int(round(t2 * 1000))-int(round(t1 * 1000))))
測試了32張精挑細(xì)選的模糊二維碼:
識別失敗數(shù)量:1 測試時(shí)間:130
使用最新版的 Zxing 識別失敗了三張。
源碼
https://gitee.com/52itstyle/Python/tree/master/Day13
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
tensorflow使用L2 regularization正則化修正overfitting過擬合方式
這篇文章主要介紹了tensorflow使用L2 regularization正則化修正overfitting過擬合方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05一篇文章弄懂Python關(guān)鍵字、標(biāo)識符和變量
這篇文章主要給大家介紹了關(guān)于Python關(guān)鍵字、標(biāo)識符和變量的相關(guān)資料,Python關(guān)鍵詞是Python保留的具有特定含義的特殊詞語,用于執(zhí)行某些操作,Python標(biāo)識符是用戶定義的名稱,而變量是計(jì)算機(jī)內(nèi)存中的一塊區(qū)域,存儲(chǔ)對象的內(nèi)存地址,以便引用對象的值,需要的朋友可以參考下2021-07-07pycharm 實(shí)現(xiàn)本地寫代碼,服務(wù)器運(yùn)行的操作
這篇文章主要介紹了pycharm 實(shí)現(xiàn)本地寫代碼,服務(wù)器運(yùn)行的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06基于Linux系統(tǒng)中python matplotlib畫圖的中文顯示問題的解決方法
下面小編就為大家?guī)硪黄贚inux系統(tǒng)中python matplotlib畫圖的中文顯示問題的解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06關(guān)于Python列表元素排序操作sort()、min()、max()函數(shù)用法
這篇文章主要介紹了關(guān)于Python列表元素排序操作sort()、min()、max()函數(shù)用法,python中提供了列表元素的操作函數(shù),那么這些函數(shù)該怎么使用呢,感興趣的朋友一起來看看吧2023-04-04numpy.transpose()實(shí)現(xiàn)數(shù)組的轉(zhuǎn)置例子
今天小編就為大家分享一篇numpy.transpose()實(shí)現(xiàn)數(shù)組的轉(zhuǎn)置例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12PyCharm 安裝與使用配置教程(windows,mac通用)
很多小伙伴下載安裝PyCharm后不會(huì)使用,這篇文章詳細(xì)介紹了PyCharm安裝與使用教程(windows,mac通用),需要的朋友可以參考下2021-05-05