QR code is commonly use for bank payment and in goods store. The pixel dot image which human can not read, but contain rich amount of information. Today tutorial we are going to implement how to generate QR Code in Node.js. We will use npm package called "qrcode" to generate QR Code.
So let's follow the step below to create CQ generator in Node.js
Following step to generate qr code in node.js
Suppose you have create an empty directory, then launch terminal navigate to your current directory and run command below:
npm init
Run command below to install npm QR Code package.
npm install --save qrcode
We are going to import the package to index.js and implement generator QR code.
Create file in root directory index.js
const qr = require('qrcode');
let data =
{
id: 120,
name: "Xiao Xing",
email: "user@cambotutorial.com",
gender: "male",
department: "Investigation"
};
let strJson = JSON.stringify(data);
const config = {type:'terminal'};
// Display QR code to terminal
qr.toString(strJson, config, function(err, code)
{
if(err) return console.log("error occurred");
console.log(code);
});
// Display QR code in base64 string
qr.toDataURL(strJson, function (err, code)
{
if(err) return console.log("error occurred");
console.log(code)
});
let config = {
errorCorrectionLevel: 'H',
type: 'terminal',
quality: 0.95,
margin: 1,
color: {
dark:"#010599FF",
light:"#ff8c8c"
}
};
qr.toString(strJson, config, function(err, code){});
There are property to configure generate QR code for this example is specifiy for function .toString()
:
errorCorrectionLevel
: Error correction capability allows to successfully scan a QR Code even if the symbol is dirty or damaged. Supported value (L
, M
, Q
, H
).type
: Output format. Supported value (terminal
,utf8
,svg
).quality
: A Number between 0
and 1
indicating image quality if the requested type is image/jpeg
or image/webp
.margin
: Define how much wide the quiet zone should be.color.dark
: Ouput dot color.color.light
: Transparant background qr code.You can check for detail with different function qrcode
That's it. Now let's run command to launch index.js and see the result.
node index.js
Hope this short article help you. Have a nice day!
Source code: https://youtu.be/dEwtRmfDS9w
You might Also Like:
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him