日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 调用qrcode库实现二维码识别

發布時間:2024/3/12 python 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 调用qrcode库实现二维码识别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

python 實現二維碼識別ScanQRcode.py

""" 生成二維碼保存及對二維碼解碼輸出 運行需要安裝相應庫 """ import os import qrcode from PIL import Image from pyzbar import pyzbardef createQRCode1(content, save_path=None):"""創建二維碼圖片,并保存:param content:二維碼文本信息:param save_path:二維碼保存地址:return:"""qr = qrcode.QRCode(version=5, error_correction=qrcode.constants.ERROR_CORRECT_M, box_size=8, border=4)qr.add_data(data=content)qr.make(fit=True)img = qr.make_image(fill_color="black", back_color="white")if save_path:img.save(save_path)else:img.show()def createQRCode2(content, icon_path, save_path=None):"""創建帶中心圖片的二維碼,并保存:param content: 二維碼文本信息:param icon_path: 中心圖片地址:param save_path: 二維碼保存地址:return: 無返回值"""# 判斷中心圖片是否存在if not os.path.exists(icon_path):raise FileExistsError(icon_path)# 創建二維碼圖片qr = qrcode.QRCode(version=5, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=8, border=4)qr.add_data(data=content)qr.make(fit=True)img = qr.make_image(fill_color="black", back_color="white").convert('RGBA')# 調整二維碼圖片icon_img = Image.open(icon_path)code_width, code_height = img.sizeicon_img = icon_img.resize((code_width // 4, code_height // 4), Image.ANTIALIAS)img.paste(icon_img, (code_width * 3 // 8, code_width * 3 // 8))if save_path:img.save(save_path) # 保存二維碼圖片img.show() # 顯示二維碼圖片else:print("save error!")def decode_qr_code(code_img_path):"""識別二維碼,對二維碼進行編譯,返回值:param code_img_path: 二維碼的保存地址:return: 二維碼的編譯結果"""if not os.path.exists(code_img_path):raise FileExistsError(code_img_path)return pyzbar.decode(Image.open(code_img_path), symbols=[pyzbar.ZBarSymbol.QRCODE])if __name__ == "__main__":print("============QRcodetest===============")print(" 1、Make a QRcode ")print(" 2、Scan a QRcode ")print("=====================================")print("1、請輸入二維碼存儲信息:")code_Data = input('>>:').strip()print("正在編碼·······")# createQRCode2(code_Data, "img/QRcenter.jpg", "qrcode.png") # 內容,center圖片,生成二維碼圖片createQRCode1(code_Data, "qrcode.png")print("圖片已保存,名稱為:qrcode.png")results = decode_qr_code("qrcode.png")print("2、正在解碼:")if len(results):print("解碼結果是:")print(results[0].data.decode("utf-8"))else:print("無法識別")

通過攝像頭進行二維碼識別ScanQRbyVedio

''' ================================ test4:識別攝像頭中的條形碼或二維碼 (ps.僅識別二維碼碼成功) ================================ ''' import cv2 import pyzbar.pyzbar as pyzbardef decodeDisplay(image):barcodes = pyzbar.decode(image)for barcode in barcodes:# 提取二維碼的邊界框的位置# 畫出圖像中條形碼的邊界框(x, y, w, h) = barcode.rectcv2.rectangle(image, (x, y), (x + w, y + h), (225, 225, 225), 2)# 提取二維碼數據為字節對象,所以如果我們想在輸出圖像上# 畫出來,就需要先將它轉換成字符串barcodeData = barcode.data.decode("utf-8")barcodeType = barcode.type# 繪出圖像上條形碼的數據和條形碼類型text = "{} ({})".format(barcodeData, barcodeType)cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,.5, (225, 225, 225), 2)# 向終端打印條形碼數據和條形碼類型print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))return imagedef detect():camera = cv2.VideoCapture(0)while True:# 讀取當前幀ret, frame = camera.read()# 轉為灰度圖像gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)im = decodeDisplay(gray)cv2.waitKey(5)cv2.imshow("camera", im)# 如果按鍵q則跳出本次循環if cv2.waitKey(10) & 0xFF == ord('q'):breakcamera.release()cv2.destroyAllWindows()if __name__ == '__main__':detect()

總結

以上是生活随笔為你收集整理的python 调用qrcode库实现二维码识别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。