Introduction: QR Code Scanner Using OpenCV in Python

In today's world we see QR code and Bar code are being used almost every where from product packaging to Online Payments and now-a-days we see QR codes even in restaurant to see the menu.

So no doubt that it is the big think now. But have you ever wondered how this QR code works or how it is scanned and we get the required information? If you don't know then you are in the right place for the answer.

In this Instructable you will learn how to make your won QR code Scanner using Python and OpenCV

Supplies

  1. Python (3.6,3.7,3.8 recommended)
  2. OpenCV Library
  3. Pyzbar Library

Step 1: Step 1: Importing Libraries

Lets start by importing our required libraries,

So we will be using 3 libraries

1. OpenCV

2. Numpy

3. Pyzbar

Step 2: Step 2: Access Webcam

Here we will access our webcam using VideoCapture function from OpenCV and also setting width and height of our output window.

Here important point is that if you are using your internal webcam then pass 0 in VideoCapture function and if you are using exteranl webcam pass 1

Now in line 6 we defining height of our output window as 640 (3 is use for height)

In line 7 we defining height of our output window as 480 (4 is use for height)

Step 3: Step 3: Reading Frames

Reading frames from webcam is very simple. You just need to add a while loop and inside while loop create two variables i.e. ret and frame read the frames using "cap.read()" .

Now all your frames would be stored in variable "frame"

Step 4: Step 4: Reading Data From Barcode

Now we will create a for loop in which we will read the data from barcode.

So we are going to using "decode" which we have imported to decode the data of QR code

and we will store it in variable "myData" and print to check if data is correct or not

Step 5: Step 5: Drawing Rectangle Around QR Code and Displaying Data

So first we will create a variable name pts which is points which will gives us 4 corner points of our QR code

Now using this points we will create a rectangle around our QR code as shown line 16-18

To display text will use myData variable where our data is been stored

Step 6:

And finally we are displaying our frame using "imshow" function in OpenCV

On Line 22-23 we have programmed that if we press "q" then program will terminate

Step 7: