How to Generate QR Code in Node.js

Websolutionstuff | Sep-20-2021 | Categories : Node.js

In this example we will see how to generate QR Code in Node.js application. In this example we will use qrcode npm package for generate QR Code. we will create json data object and create QR Code for that json data object.

QR code has become an important part of life now a days. QR means “Quick Response”. It can store a large amount of data. QR scanner can instantly process the data by scanning it. In Node.js generate QR Code is very easy.

So, In this tutorial we are generate QR Code using qrcode package in Node.js.

 

Step 1 : Create Node Application

In this step create node application using below commands.

mkdir qr_code_example

cd qr_code_example

npm init

 

 

Step 2 : Install qrcode

In this step install the qrcode module using below command

npm install qrcode

 

 Step 3 : Import the QRCode package in the index.js file

In this step we can send emai with attachment. So, make sure your configurarion before sending email.

const qr = require('qrcode');
  
let data = {
    id: 1,
    name: "dell",
    email: "[email protected]"
};
  
let strData = JSON.stringify(data);
  
qr.toString(strData, {type:'terminal'},
                    function (err, code) {
   
    if(err) return console.log("error occurred !!");
   
    console.log(code);
});
  
qr.toDataURL(strData, function (err, code) {
    if(err) return console.log("error occurred !!");   

    console.log(code);
})

 

 

Define parameters to generate QR Code

const data = {
 errorCorrectionLevel: 'H',
 type: 'terminal',
 quality: 0.95,
 margin: 1,
 color: {
  dark: '#208698',
  light: '#FFF',
 },
}
let strData = JSON.stringify(data)

For Custom QR Code generate  you define diffrent parameters like above code.

  • 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.
  • Color specifies a QR Code image color.
  • Type specifies what type of output is expected like image/png, image/jpeg, image/webp in data URL and utf8, SVG, terminal in string.
  • 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.

 

Step 4 : Run index.js file

run index.js using below code :

node index.js

 


You might also like :

Recommended Post
Featured Post
How To Get Current Date And Time In Node.js
How To Get Current Date And Ti...

In this example we will see how to get current date and time in Node.js application. In Node.js date and time are handle...

Read More

Sep-01-2021

Laravel 9 Livewire File Upload Example
Laravel 9 Livewire File Upload...

In this article, we will see the laravel 9 livewire file upload example. Here, we will learn how to upload files us...

Read More

Dec-02-2022

Drag And Drop Div Using jQuery
Drag And Drop Div Using jQuery

in this article, we will see drag and drop div using a jquery example. For this example, we are using sortable js. ...

Read More

May-03-2022

Laravel 8 One To One Relationship Example
Laravel 8 One To One Relations...

In this example we will see laravel 8 one to one relationship example also you can use one to one relationship in l...

Read More

Nov-01-2021