QR code has become an important part of life. QR means “Quick Response”. It can store a large amount of data. QR scanner can instantly process the data by scanning it.
With the increase in digital devices, you can access any data by just scanning it. There are various use cases of QR Code like in sales & marketing, for eg. you can store all the details of a product in QR Code.
Many payment methods also allow you to scan a QR code and pay money to anyone. Some of the logistics industry heavily uses QR codes to track shipments.
In today's post, we are going to generate QR Codes using the popular node package “qrcode”.
Step 1: Install the QRcode package via npm
npm install --save qrcode
Step 2: Import the QRCode package in the index.js file
const QRCode = require('qrcode')
Step 3: Define parameters to generate QR Code
const opts = {
errorCorrectionLevel: 'H',
type: 'terminal',
quality: 0.95,
margin: 1,
color: {
dark: '#208698',
light: '#FFF',
},
}
const qrImage = await QRCode.toString('Hi testing QR code', opts)
a. Error correction capability allows you to successfully scan a QR Code even if the symbol is dirty or damaged. Four levels are available to choose from according to the operating environment. Higher levels offer a better error resistance but reduce the symbol's capacity.
b. Color specifies a QR code image color.
c. Type specifies what type of output is expected like image/png, image/jpeg, image/webp in data URL and utf8, SVG, terminal in string.
d. quality specifies the quality of the image in the range of 0–1. The default value is 0.92 & only available for type image/jpeg & image/webp.
e. for the text you can specify the mode of the text like numeric, alphanumeric, Byte & Kanji. Based on the type of mode it will be able to store data like numeric mode can store up to 7,089 characters & byte mode allows 2,953 characters to be stored.
f. version specifies the QR code version in the range of 1–40. If no version is specified, the more suitable value will be used. Each version has a different number of modules (black and white dots), which define the symbol's size. For version 1 they are 21x21, for version 2 25x25 e so on. Higher is the version, more are the storable data, and of course, bigger will be the QR Code symbol.
Output of the above Code
There are various methods available to create the QRCode like generate:
-
toDataURL to pass into HTML IMG tag.
-
toFile to create file image.
-
toString to create a string representation of the QR Code. If the output format is SVG it will return a string containing XML code.
-
toCanvas to draw QR code symbol to node canvas. You have to pass the document id where canvas should be drawn.
-
create to generate a QR Code symbol and returns a QRcode object.
Check out the different methods of QR code generation in the example here. You can generate any QR code using this module. For example, you can generate a QR Code for your E-commerce product details & store them on AWS S3. Or you can generate a QR Code to your payment portal for quick payment.
Thank you for reading.