How to Make a Barcode Reader in Python

By | September 27, 2021
How to Make a Barcode Reader in Python

We all know what a barcode looks like, but do you know what actually a barcode is. A barcode is a visual representation of machine language data that use whitespace and black line to write data. To read barcode data we actually use the barcode reader. But we are Python developer, and when its come to software instead of buying we try to but our own.  And here in this article, we will be writing the Python code which can read the barcode and other visual data representation such as QR code.

Before diving into the Python Program let’s install the required libraries.

Vamware

Install Libraries

For this tutorial, we will be using the Python pyzbar and opencv-pythonlibraries, with OpenCV we will read the image of barcode and QR code and with pyzbarwe will decode the barcode and QR code of the image.

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 your Best Python IDE and text editor and start coding, with us.

Python code

let’s start with importing the required modules.

import cv2 as cv
from pyzbar import pyzbar

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

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

Now decode the image with 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 barcodesand grab their X-axis, Y-axis, width, and height with the help of rectproperty, then decode the specific barcode, convert their 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 rectproperty returns the location of the bar code in the image.

cv.rectange()draw the rectangle around the bar code based on the location provided by the rect.

The barcode.data.decode("utf-8")convert the data into the string format.

The typereturn the type of code.

The datareturn the data.

The cv.putText()write text data above the visual code.

Now let’s show detected barcode and their data with 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 can we read, detect and decode Machine visual codes like Barcode and QR code in Python. We can read all sorts of Machine visual code with the same Python program and pyzbar library.

People are also reading: 

Leave a Reply

Your email address will not be published. Required fields are marked *