Amir
Amir

Reputation: 496

Reading barcodes using 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.

Upvotes: 5

Views: 24106

Answers (3)

Nataraju.K
Nataraju.K

Reputation: 19

my Barcode scanner connected via USB is acting like standard input device (like Keyboard) in Windows 10. When barcode is scanned on opened text file in Notepad, the scanner output digits directly appears in text file, like typing on Keyboard.

Hence for reading into Python39 shell or file in Windows 10, I used python code 'input("scan Barcode: ")' and scanned the code, which successfully gave me Barcode digits produced by device/scanner.

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
>>>

Upvotes: 1

Veilleuse
Veilleuse

Reputation: 334

(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.

Upvotes: 7

tusharmakkar08
tusharmakkar08

Reputation: 696

You should try using zbar and a PyUSB, scan "USB Serial"-mode barcode, then "Save" barcode to make this setting permanent. Now your 3310g is in serail emulation mode, note new /dev/ttyACM0 or /dev/ttyUSB0 device. Read serial port with simple file operations from python:

f = open('/dev/ttyACM0')
print f.read(13)

Upvotes: 2

Related Questions