python 读取条形码以及二维码

#在AI运用中经常需要读取物体的条码信息,这一步在很多的场景应用中变得非常重要#

本文就来浅谈一下pyzbar的运用。

一、安装pyzbar的库:

pip install pyzbar -i https://pypi.tuna.tsinghua.edu.cn/simple

二、pyzbar库的介绍:

pyzbar库是一个基于Python的二维码生成和识别库,它提供了丰富的API和工具,方便用户快速实现二维码生成和识别功能。pyzbar库支持多种二维码格式,如QR Code、EAN-13等,并且可以在多种操作系统上运行。

三、生成条码:

要使用pyzbar生成二维码,需要使用pyzbar.pyzbar模块中的create_qrcode函数。该函数接受一个字符串参数,用于生成对应的二维码图片。下面是一个示例代码:

from pyzbar.pyzbar import create_qrcode  
# 要生成二维码的字符串  
data = "This is QR code!"  
# 生成二维码图片并保存到本地  
qrcode = create_qrcode(data, "D:\\log\\QRcode.png")
在上面的代码中,我们首先导入了create_qrcode函数,然后定义了一个要生成二维码的字符串。接着,我们调用create_qrcode函数将字符串转换为二维码图片,并将图片保存到本地D:\\log文件夹中为"QRcode.png"。

四、识别条码:

要使用pyzbar识别二维码,需要使用pyzbar.pyzbar模块中的decode函数。该函数接受一个图像参数,用于识别其中的所有码,并且用绿色框框框出来,然后重新保存图片。下面是一个示例代码:

def readBarcode_file(*kws):
    barcode_list.clear()
    filename=kws
    file=format(filename[0])
    img = cv2.imread(file)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    barcodes = pyzbar.decode(gray)

    for barcode in barcodes:
        barcode_data = barcode.data.decode("utf-8")
        barcode_list.append(barcode_data)
    print("readbarcode",barcode_list)
    for barcode in barcodes:
        # Get the barcode coordinates and draw a rectangle around it
        x, y, w, h = barcode.rect
        # print('x=', x, 'y=', y, 'w=', w, 'h=', h)
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

        # Get the barcode type and data
        barcode_type = barcode.type
        barcode_data = barcode.data.decode("utf-8")
    img_resize= cv2.resize(img,(400,300))
    cv2.imwrite("re_capture.png", img_resize)
    return barcode_list

五、附上一个完整的代码:

import pyzbar.pyzbar as pyzbar
import cv2
barcode_list=[]
def readBarcode_file(*kws):
    barcode_list.clear()
    filename=kws
    file=format(filename[0])
    img = cv2.imread(file)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    barcodes = pyzbar.decode(gray)

    for barcode in barcodes:
        barcode_data = barcode.data.decode("utf-8")
        barcode_list.append(barcode_data)
    print("readbarcode31",barcode_list)
    for barcode in barcodes:
        # Get the barcode coordinates and draw a rectangle around it
        x, y, w, h = barcode.rect
        # print('x=', x, 'y=', y, 'w=', w, 'h=', h)
        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

        # Get the barcode type and data
        barcode_type = barcode.type
        barcode_data = barcode.data.decode("utf-8")
        # self.Text_Info(barcode_data)
        # value_list.append(barcode_data)
    img_resize= cv2.resize(img,(400,300))
    cv2.imwrite("re_capture.png", img_resize)
    return barcode_list
if __name__ == '__main__':
    file = "your path with file" # 改成你实际要读取的图片
    readBarcode_file(file)

本文使用环境为:Windows 10 ,python 3.10

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kamach_83

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值