Skip to content
Advertisement

how to add logo in the middle of the qr code using nodejs

I am trying to create a logo and try to add a logo in the middle of the qr code and i am able to generate the qr code but unable to get the qr code with logo on the middle i have tried this code but unable to get the result getting this error

Error: You need to specify a canvas element

and i am using this library https://github.com/soldair/node-qrcode

and here is the tried code

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 = "./Capture.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 "";
  }
};
getQRcodeImage();

Advertisement

Answer

const QRCode = require("qrcode");
const { createCanvas, loadImage } = require("canvas");
async function create(dataForQRcode, center_image, width, cwidth) {
  const canvas = createCanvas(width, width);
  QRCode.toCanvas(
    canvas,
    dataForQRcode,
    {
      errorCorrectionLevel: "H",
      margin: 1,
      color: {
        dark: "#000000",
        light: "#ffffff",
      },
    }
  );

  const ctx = canvas.getContext("2d");
  const img = await loadImage(center_image);
  const center = (width - cwidth) / 2;
  ctx.drawImage(img, center, center, cwidth, cwidth);
  return canvas.toDataURL("image/png");
}

async function main() {
  const qrCode = await create(
    "http://shauryamuttreja.com/qr/",
    "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAABEVBMVEX/////vgCfB6khKzb/vAD+vxf/ugCYAKP/1oacAKaXALD/uQCcAKf///3/8M7/wQDw3fEbJjL/13/z5PT/8tb/5Kr68vr/+u3nzOm5YsD//P/MkdH//PTr1e3cteD16vbWqdrIh83gvOOpMrL/5rP/6bzlxuf/9d+zUrv/ykf/35z/0mvDfcn/3ZQAFjgOHCrQm9WvRbf/xTP/8dK9bMT/1HXJi8//xjv/zVrAdMayTrqsObT/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QMo2d2O65jyy7e5gjQMKEDt7lQMEVMirV7p5C164BhJaT718O4i6GSlG1eeKaEsfECC3HdsbofO3ZOxeK7EL/KqOPb9F7RHUIx2UM02EzRzv11n+Zbiq73az0T646Gfc65FdWOn25ctxrbC78xwZeokSJEiVKlChRokSJEiVKlChRokSJEiVKlChRIlj/ApgIIGDBCEskAAAAAElFTkSuQmCC",
    150,
    50
  );

  console.log(qrCode);
}

main();

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement