pyzbar 識別QR二維碼
from PIL import Image
from pyzbar.pyzbar import decode# 打開圖像文件
image_path = 'qr01.jpg' # 替換為你的圖像路徑
image = Image.open(image_path)# 解碼圖像中的二維碼
decoded_objects = decode(image)# 輸出識別結果
for obj in decoded_objects:print("二維碼類型:", obj.type)print("二維碼數據:", obj.data.decode("utf-8"))
opencv識別識別QR二維碼?opencv-contrib-python==4.5.5.62
import cv2# 圖像路徑
image_path = 'qr01.jpg'# 檢查圖像是否存在
if not cv2.haveImageReader(image_path):print(f"錯誤: 圖像文件 '{image_path}' 不存在或無法讀取,請檢查路徑和文件名。")
else:# 讀取圖像img = cv2.imread(image_path)# 檢查圖像是否成功加載if img is None:print("錯誤: 圖像加載失敗,請檢查文件格式或路徑。")else:# 創建 QRCodeDetector 對象qr_decoder = cv2.QRCodeDetector()# 檢測并解碼二維碼data, bbox, _ = qr_decoder.detectAndDecode(img)# 輸出識別結果if data:print("二維碼數據:", data)else:print("未檢測到二維碼。請確保圖像是清晰的,并包含有效的二維碼。")