我想在 python 中读取条形码。我搜索了支持条码读取和 python 2.7 的库,但没有找到任何东西。有什么图书馆可以帮助我吗?另外,如果您知道任何有关条形码阅读的教程,请告诉我在哪里可以找到。
小智 6
(迟到总比不到好......): 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)
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到此代码:https : //www.learnopencv.com,并附有说明。
您应该尝试使用zbar 和PyUSB,扫描“USB 串行”模式条形码,然后“保存”条形码以使此设置永久生效。现在您的 3310g 处于串行仿真模式,请注意新的 /dev/ttyACM0 或 /dev/ttyUSB0 设备。从 python 中通过简单的文件操作读取串口:
f = open('/dev/ttyACM0')
print f.read(13)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18690 次 |
| 最近记录: |