【发布时间】:2013-06-24 22:27:57
【问题描述】:
我想在 python 中读取条形码。我搜索了支持条形码读取并且还支持 python 2.7 的库,但我没有找到任何东西。 有什么图书馆可以帮助我吗? 另外,如果您知道有关条形码读取的任何教程,请告诉我在哪里可以找到。
【问题讨论】:
-
条形码只是一个数字。我的一个条形码阅读器被计算机识别为键盘设备,因此可能不需要图书馆。
我想在 python 中读取条形码。我搜索了支持条形码读取并且还支持 python 2.7 的库,但我没有找到任何东西。 有什么图书馆可以帮助我吗? 另外,如果您知道有关条形码读取的任何教程,请告诉我在哪里可以找到。
【问题讨论】:
(迟到总比没有好......): Pyzbar 和 OpenCV 应该做你想做的事。
这是我在 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,并附有说明。
【讨论】:
我通过 USB 连接的条形码扫描仪在 Windows 10 中的作用类似于标准输入设备(如键盘)。当在记事本中打开的文本文件上扫描条形码时,扫描仪输出的数字直接出现在文本文件中,就像在键盘上打字一样。
因此,为了在 Windows 10 中读取 Python39 shell 或文件,我使用了 python 代码 'input("scan Barcode: ")' 并扫描了代码,它成功地为我提供了设备/扫描仪生成的条形码数字。
C:\Project\Python39>python
Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>>
>>>
>>> input("scan Barcode: ")
scan Barcode: 88H-1010UB-0TV
'88H-1010UB-0TV'
>>>
>>> myBarCodeId = input("Scan barCode please ...")
Scan barCode please ...TARNLK002563
>>>
>>> print(myBarCodeId)
TARNLK002563
>>>
>>> mibar=input("scan me please \n")
scan me please
88H-1010UB-0TV
>>>
>>> print(mibar)
88H-1010UB-0TV
>>>
【讨论】: