使用python读取条形码 [英] Reading barcodes using python
本文介绍了使用python读取条形码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用python读取条形码.我搜索了支持条形码读取并且也支持python 2.7的库,但没有找到任何东西. 有没有可以帮助我的图书馆? 另外,如果您知道有关条形码读取的任何教程,请告诉我在哪里可以找到它.
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.
推荐答案
(迟到总比不到好...): Pyzbar 和 OpenCV 应该做您想要的.
(Better late than never...) : Pyzbar and OpenCV should do what you want.
这是我在Python 3中使用的代码.
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)
您可以在这里找到此代码: https://www.learnopencv.com ,并附有说明.
You can find this code here : https://www.learnopencv.com, with explanations.
这篇关于使用python读取条形码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文