我正在使用 pyzbar 和 opencv 制作条形码阅读器,但是当我使用 pyzbar.pyzbar 中的 decode 函数时,我收到此错误:
Traceback (most recent call last):
File "c:\Users\galax\Documents\barcodeScanner\barcodeReader.py", line 44, in <module>
BarcodeReader(image)
File "c:\Users\galax\Documents\barcodeScanner\barcodeReader.py", line 12, in BarcodeReader
detectedBarcodes = decode(img)
File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyzbar\pyzbar.py", line 207, in decode
pixels, width, height = _pixel_data(image)
File "C:\Users\galax\AppData\Local\Programs\Python\Python310\lib\site-packages\pyzbar\pyzbar.py", line 173, in _pixel_data
pixels, width, height = image
TypeError: cannot unpack non-iterable NoneType object
这是我的代码:
# Importing library
import cv2
from pyzbar.pyzbar import decode
# Make one method to decode the barcode
def BarcodeReader(image):
# read the image in numpy array using cv2
img = cv2.imread(image)
# Decode the barcode image
detectedBarcodes = decode(img)
# If not detected then print the message
if not detectedBarcodes:
print("Barcode Not Detected or your barcode is blank/corrupted!")
else:
# Traverse through all the detected barcodes in image
for barcode in detectedBarcodes:
# Locate the barcode position in image
(x, y, w, h) = barcode.rect
# Put the rectangle in image using
# cv2 to highlight the barcode
cv2.rectangle(img, (x-10, y-10),
(x + w+10, y + h+10),
(255, 0, 0), 2)
if barcode.data!="":
# Print the barcode data
print(barcode.data)
print(barcode.type)
#Display the image
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
# Take the image from user
image="barcode.png"
BarcodeReader(image)
我已经尝试过不同的条形码,但没有成功...... 我也尝试卸载并安装 pyzbar,但我也没有任何成功......
任何帮助将不胜感激🙂
我认为条形码已损坏,请尝试使用另一个条形码