Python programming blog

Thursday, March 25, 2021

Realtime QRcode scanner using python [ 30 lines ]

 Hello python programmers, This is AK, In this blog we're going to learn about how to create a QR code scanner using python programming.

Now a days Qr code is a much needed one for doing transactions, sharing links and encrypt the data like that.

So in this blog we're going to create a python program that is used to scan any type of QR code and it redirects you to the link that hidden in the Qr code.

 In the many tutorials about Qr code scanner using python. They're scanning the picture of Qr code and it redirects you to link that behind in the Qr code.

In this blog,We're not doing like that but we're going to capture the qr code by real time through the webcam of my PC.

Before starting this blog, You need to know about how this process is going to work,Firstly you need to open your webcam and you've to run your python program make it ready to scan the qr code.

You can take Qr code picture on your mobile and show the picture in front of your webcam.

It correctly identifies the qr code that present in your screen.And this program redirects you to link that hidden in the qr code.

So this is the process of how this program works.

Before start to create this program do like ,share, comment and subscribe these things will motivate me to produce more good contents in python programming.

 Let's start this video.

1.Creating the Qr code

First you need to create a qr code for performing the QR scanning script. You can take any Qr code.But in this video I want to show you how to create a qr code by using python.


For creating the qr code you need to install a python package. We all know how to install a python package in command prompt


Command: pip install pyqrcode

Command: pip install pypng ( Saves the qr code image in png format ) 

 

First import the pyqrcode and png library, From the pyqrcode you've to import the Qr code function.

 

import pyqrcode
import png
from pyqrcode import QRCode

And then create a variable called as link and in this variable you need to insert the link that you want to hide behind the qr code.

 

link = "www.example.com" 


So next call the instance pyqrcode.create and inside this object pass the parameter link.And the next step you need to mention the format of image that you want to save the qr code.


url = pyqrcode.create(link)
url.png('myqr.png', scale = 6)

Output after running this script.


2.Creating the Qr code scanner

For creating the Qr code scanner you've to install the OpenCv library in your terminal

Command: pip install opencv-python

Step:1 

First you need to import the opencv and webrowser library in your IDE.

 import cv2
import webbrowser

 

Step:2 

 Next we need to start the camera for the capturing the qr code .For that declare a variable called as cap and in this variable pass the instance cv2.VideoCapture(0).

Next process is we need to create a variable called detector and in this variable call the object cv2.QRCodeDetector(). 

 

cap = cv2.VideoCapture(0)
detector = cv2.QRCodeDetector() 

 

Step:3 

This step is very important you need to create a while loop and in this loop create a variable called a img and this will read your webcam screen continuously until this loop breaks

 while True:
    _, img = cap.read()


Step:4

Next create a variable called as data and this variable to used decode the qr code and if any data present in the qr code image it will break the loop and it opens the link in your browser. So this is the condition that I inserted here.

data, _ = detector.detectAndDecode(img)
    if data:
        a=data
        break
    cv2.imshow("QRCODEscanner", img)    
    if cv2.waitKey(1) == ord("q"):
        break

b=webbrowser.open(str(a))
cap.release()
cv2.destroyAllWindows()


 

Full code: Qrcode scanner code

Thanks for reading 💗

No comments:

Post a Comment