≡ Menu

How to generate and read QR Code in Python

QR code is a type of matrix barcode that is machine readable optical label which contains information about the item to which it is attached. In practice, QR codes often contain data for a locator, identifier, or tracker that points to a website or application, etc.

Problem Statement :

Generate and read QR codes in Python using qrcode and OpenCV libraries

Installing required dependencies:

pyqrcode module is a QR code generator. The module automates most of the building process for creating QR codes. This module attempts to follow the QR code standard as closely as possible. The terminology and the encoding used in pyqrcode come directly from the standard.

pip install pyqrcode

Install an additional module pypng to save image in png format:

pip install pypng

Import Libraries

import pyqrcode
import png
from pyqrcode import QRCode
import cv2
import numpy as np

Create QR Code:

# OUTPUT SECTION

# String which represents the QR code
s = "http://www.raucci.net"

# output file name

filename = "qrcode.png"

# Generate QR Code

img = pyqrcode.create (s)

# Create and save the svg file naming "brqr.svg"

img.svg("brqr.svg", scale=8)

# Create and save the svg file naming "brqr.png"

img.png("brqr.png", scale=6)
qr code file named brqr.png

Read QR Code

Here we will be using OpenCV for that, as it is popular and easy to integrate with the webcam or any video.

# INPUT SECTION

# read the QRCODE image

img = cv2.imread('brqr.png')

# initialize the cv2 QRCode detector
detector = cv2.QRCodeDetector()

val, pts, st_code=detector.detectAndDecode(img)

print(val)

Here the output

The output of code
{ 0 comments… add one }

Rispondi

Next post:

Previous post: