How to Make a Barcode Reader in Python?

Posted in /  

How to Make a Barcode Reader in Python?
vinaykhatri

Vinay Khatri
Last updated on November 24, 2024

    We all know what a barcode looks like, but do you know what a barcode actually is?

    A barcode is a visual representation of machine language data that uses whitespace and black lines to write data. To read barcode data, we actually use the barcode reader. But as a Python developer, we can develop one on our own.

    So, here in this article, we will discuss how to make a barcode reader in Python. We will be writing the Python code that can read the barcode and other visual data representations, such as the QR code. Before diving into the Python program, let's first install the required libraries.

    Install Libraries

    For this tutorial, we will be using the Python pyzbar and opencv-python libraries. With OpenCV, we will read the image of a barcode and a QR code, and with pyzbar , we will decode the same.

    Run the following command on your terminal or command prompt to install the pyzbar and OpenCV library for your Python environment.

    pip3 install pyzbar opencv-python

    For this tutorial, we will be using the following bar_code.jpg image and read the data of both.

    In the above image, you can see that the image contains both bar as well as QR code, and we will be using the same to decode all the machine visual representation code.

    Now you are all set, open any best Python IDE or text editor and start coding.

    How to Make a Barcode Reader in Python?

    Python Code

    Let's start with importing the required modules.

    import cv2 as cv
    from pyzbar import pyzbar

    Now, load the image that contains a barcode or QR code with the help of Python openCV imread() method.

    image = cv.imread("bar_code.jpg")

    Next, decode the image with the pyzbar decode() method.

    #decode image
    barcodes = pyzbar.decode(image)

    The decode method returns a list of all barcodes present in the image. Now, loop through all the barcodes and grab their X-axis, Y-axis, width, and height with the help of the rect property, then decode the specific barcode , convert its data to string format, and print it in the console and above the code itself.

    for barcode in barcodes:
        x,y,w,h = barcode.rect
    
        #draw rectange over the code
        cv.rectangle(image, (x,y), (x+w, y+h), (255,0,0), 4)
    
        #convert into string
        bdata = barcode.data.decode("utf-8")
        btype = barcode.type
        text = f"{btype}-->, {bdata}"
        print("----")
        print(text)
        print("----")
    
        cv.putText(image, text,(x,y-10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255),0)
    • The rect property returns the location of the bar code in the image.
    • cv.rectange() draws the rectangle around the bar code based on the location provided by rect.
    • The barcode.data.decode("utf-8") converts data into the string format.
    • The type returns the type of code.
    • data returns the data.
    • The cv.putText() method writes text data above the visual code.

    Now let's show the detected barcode and QR code and their data with the imshow() method.

    cv.imshow("image", image)
    cv.waitKey(0)

    Put all the code together and execute.

    #Python program to decode/detect/read barcode/QR code in an image.

    import cv2 as cv
    from pyzbar import pyzbar
    
    image = cv.imread("bar_code.jpg")
    
    #decode image
    barcodes = pyzbar.decode(image)
    
    for barcode in barcodes:
        x,y,w,h = barcode.rect
    
        #draw rectange over the code
        cv.rectangle(image, (x,y), (x+w, y+h), (255,0,0), 4)
    
        #convert into string
        bdata = barcode.data.decode("utf-8")
        btype = barcode.type
        text = f"{bdata}, {btype}"
        print("----")
        print(text)
        print("----")
        #write text on the image
        cv.putText(image, text,(x,y-10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255),0)
    
    cv.imshow("image", image)
    cv.waitKey(0)

    Output

    ----
    QRCODE---> upi://pay?pa=paytmqr2810050501011DHDRNXBDONU@paytm&pn=Paytm%20Merchant&mc=5499&mode=02&orgid=000000&paytmqr=2810050501011DHDRNXBDONU&sign=MEUCIQC0tKkWo/6/tm6sd158UJ2eis5fQePEQ1qJ0LZofdjIgwIgbpOMRfJdHNsBbzrjwowNULCwCZA3a631IOd/YoAg3RM=
    ----
    ----
    CODABAR---> A1234567890A
    ----

    As you can see that the code type and data are printed in the console as well as in the image.

    Conclusion

    In this Python tutorial, we learned how we could read, detect and decode machine visual codes like barcode and QR code in Python, i.e., how to make a barcode reader in Python? We can read all sorts of machine visual code with the same Python program and the pyzbar library.

    People are also reading:

    Leave a Comment on this Post

    0 Comments