我在浏览器上搜索了很多,以找到一些在 python 中读取/解码 pdf417 条形码的方法 - 但对我来说没有任何作用。
首先我尝试了简单的模块“pyzbar”:
import cv2
from pyzbar.pyzbar import decode
imagePath = '/home/zaryab/Downloads/barcode.png'
img = cv2.imread(imagePath)
detectedBarcodes = decode(img)
if not detectedBarcodes:
print("Barcode Not Detected or your barcode is blank/corrupted!")
else:
for barcode in detectedBarcodes:
(x, y, w, h) = barcode.rect
cv2.rectangle(img, (x-10, y-10),
(x + w+10, y + h+10),
(255, 0, 0), 2)
if barcode.data!="":
print(barcode.data)
print(barcode.type)
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
以上代码适用于 code128 类型条形码,但不适用于 pdf417 条形码。
然后我尝试了模块'pdf417decoder:
from PIL import Image as PIL
from pdf417decoder import PDF417Decoder
imagePath = '/home/zaryab/Downloads/barcode.png'
image = PIL.open(imagePath)
decoder = PDF417Decoder(image)
if (decoder.decode() > 0):
decoded = decoder.barcode_data_index_to_string(0)
print(decoded)
else:
print("No Barcode Found!")
这也不起作用,总是进入其他部分并打印“未找到条形码!”。
我应该如何解码 pdf417 条形码?
您似乎已经尝试了几个库来解码 PDF417 条形码,但没有成功。与 Code 128 等更简单的条形码类型相比,解码 PDF417 条形码可能会更加复杂。要解码 PDF417 条形码,您可以使用
pdf417
库。如果该库不适合您,则条形码图像质量、库配置或其他因素可能存在问题。以下是一些排除 PDF417 条形码故障并可能解码的步骤:
图像质量:
库安装:
pdf417
库。您可以使用 pip 安装它:pip install pdf417
图像格式:
图像预处理:
检查多个条形码:
条形码位置:
库配置:
pdf417
库可能具有影响条形码识别的配置选项。检查库的文档以了解可能需要调整的任何配置设置。示例条形码图像:
以下是如何使用
pdf417
库解码 PDF417 条形码的示例:
from pdf417 import PDF417Decoder
imagePath = '/path/to/your/barcode.png'
decoder = PDF417Decoder()
barcode_data = decoder.decode(imagePath)
if barcode_data:
decoded_text = barcode_data[0][0] # Assuming you have only one barcode
print(decoded_text)
else:
print("No Barcode Found!")
如果您仍然面临条形码识别问题,请考虑提高条形码图像的质量或使用可能提供更好结果的其他库或工具,或者查阅您正在使用的特定库的文档和社区以获取故障排除提示。