4

我想在 python 中读取条形码。我搜索了支持条形码读取并且还支持 python 2.7 的库,但我没有找到任何东西。有什么图书馆可以帮助我吗?另外,如果您知道有关条形码读取的任何教程,请告诉我在哪里可以找到。

4

3 回答 3

6

(迟到总比没有好......): PyzbarOpenCV应该做你想做的事。

这是我在 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 ,并附有说明。

于 2018-11-07T06:58:10.273 回答
2

您应该尝试使用zbarPyUSB,扫描“USB Serial”模式条形码,然后“保存”条形码以使此设置永久化。现在您的 3310g 处于串行仿真模式,请注意新的 /dev/ttyACM0 或 /dev/ttyUSB0 设备。从 python 中通过简单的文件操作读取串口:

f = open('/dev/ttyACM0')
print f.read(13)
于 2013-06-27T12:37:36.617 回答
0

我通过 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
>>>
于 2021-02-01T06:02:10.740 回答