How to generate QR code together with the image in a center

Viewed 5939

how I can generate QR code in react js or node js?

Maybe you know a library or package for generating QR code together with the image in a center?)

Example

Example QR-code

2 Answers

This function will create qrcode with a logo at centre.

const qrcode= require('qrcode');

const getQRcodeImage = async () => {
    try {        
        let canvas=await qrcode.toCanvas(`my sample text`);

        //adding a log at center        
        const imgDim={width:30,height:30}; //logo dimention
        var context = canvas.getContext('2d');
        var imageObj = new Image();  
        imageObj.src = './images/logo.png';        
        imageObj.onload = function() {
          context.drawImage(imageObj, 
          canvas.width / 2 - imgDim.width / 2,
          canvas.height / 2 - imgDim.height / 2,imgDim.width,imgDim.height);
        };        
        return canvas;
    }catch (e) {
        console.error(e);
        return "";
    }
}

Usage:

$(document).ready(function () {
    getQRcodeImage().then(res=>{$("#qrcodeAuth").html( res);});
}
Related