文件路径要加上一个r ,比如上面的写成r"C:\Users\Administrator\Desktop\test\test.jpg"
不行可以换另一种:
import zxing
reader = zxing.BarCodeReader()
barcode = reader.decode(r"C:\Users\Administrator\Desktop\test\test.jpg")
print(barcode.parsed)
--result
http://esf.whfgxx.org.cn/New/pub/RentView/20220514ZLXfRVE?refer=OJClCFBCL01YJJ3xrCMq283TX54J8UNJTOmcrONE&corp=6&org=100003138
解决方案是:你可以写两个函数,当用第一个得到为空的时候,用第二个函数
下面的识别度更好些:
import numpy as np
import cv2
from pyzbar import pyzbar
def get_qrcode(image_input, binary_max=230, binary_step=2):
if len(image_input.shape) >= 3:
image_input = cv2.cvtColor(image_input, cv2.COLOR_RGB2GRAY)
binary, _ = cv2.threshold(image_input, 0, 255, cv2.THRESH_OTSU)
res = []
while (binary < binary_max) and (len(res) == 0):
binary, mat = cv2.threshold(image, binary, 255, cv2.THRESH_BINARY)
res = pyzbar.decode(mat)
return res
image_file = r"C:\Users\Administrator\Desktop\test\test.jpg"
image = cv2.imdecode(np.fromfile(image_file,
dtype=np.uint8),
cv2.IMREAD_COLOR)
result = get_qrcode(image)
print(str(result[0][0]).replace('b', ''))