在python中解码pdf417条形码

Decoding pdf417 barcode in python

提问人:Zaryab Ali 提问时间:11/7/2023 最后编辑:Christoph RackwitzZaryab Ali 更新时间:11/7/2023 访问量:70

问:

我在浏览器上搜索了很多,以找到一些在 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 条形码?

python opencv 条码 扫描

评论

0赞 Christoph Rackwitz 11/7/2023
您是否在问 OpenCV 是否可以使用条形码解码库?您能提供条形码的输入数据(图片)吗?
2赞 Ulrich Eckhardt 11/7/2023
“不起作用”——你到底是什么意思?您使用的图像中甚至有什么?也就是说,您可以使用各种命令行或在线工具来至少验证图像本身是否包含有效内容。
0赞 Zaryab Ali 11/8/2023
乌尔里希·埃克哈特,你是对的。我首先使用了一些在线工具来检查它是否正在解码 pdf417 条形码 - 然后我使用的正确工具对我有用。PDF417解码器模块对我有用。谢谢。

答: 暂无答案