python识别条形码函数_使用python读取条形码

I want to read barcodes in python. I searched for library that support barcode reading and also support python 2.7, but I didn't find anything.

Is there any library that can help me?

Also if you know any tutorial about barcode reading, please tell me where can I find that.

解决方案

(Better late than never...) :

Pyzbar and OpenCV should do what you want.

Here is the code I'm using with Python 3:

#!/usr/bin/python3

# -*- coding: Utf-8 -*-

from __future__ import print_function

import pyzbar.pyzbar as pyzbar

import numpy as np

import cv2

def decode(im) :

# Find barcodes and QR codes

decodedObjects = pyzbar.decode(im)

# Print results

for obj in decodedObjects:

print('Type : ', obj.type)

print('Data : ', obj.data,'\n')

return decodedObjects

# Display barcode and QR code location

def display(im, decodedObjects):

# Loop over all decoded objects

for decodedObject in decodedObjects:

points = decodedObject.polygon

# If the points do not form a quad, find convex hull

if len(points) > 4 :

hull = cv2.convexHull(np.array([point for point in points], dtype=np.float32))

hull = list(map(tuple, np.squeeze(hull)))

else :

hull = points;

# Number of points in the convex hull

n = len(hull)

# Draw the convext hull

for j in range(0,n):

cv2.line(im, hull[j], hull[ (j+1) % n], (255,0,0), 3)

# Display results

cv2.imshow("Results", im);

cv2.waitKey(0);

# Main

if __name__ == '__main__':

# Read image

im = cv2.imread('zbar-test.jpg')

decodedObjects = decode(im)

display(im, decodedObjects)

You can find this code here : https://www.learnopencv.com, with explanations.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值