这是我的函数,我通过它将徽标添加到qr代码中。
const qrcode = require('qrcode');
const { createCanvas, loadImage } = require('canvas');
//Using canvas I am adding logo to center of image(center_image) and also passing width
async function create(dataForQRcode, center_image, width, cwidth) {
const canvas = createCanvas(width, width);
qrcode.toCanvas(canvas, dataForQRcode, {
errorCorrectionLevel: 'H',
version: 5,
margin: 1,
color: {
dark: '#000000',
light: '#ffffff',
},
});
const ctx = canvas.getContext('2d');
const img = await loadImage(center_image);
const center = (width - cwidth) / 2;
//and using drawImage I am adding logo to qr code
ctx.drawImage(img, center, center, cwidth, cwidth);
return canvas.toDataURL('image/jpeg', 1.0);
}
在这里,我调用函数并传递所有参数。
const QRcode = await create(
"Hello guys",
"https://images.pexels.com/photos/10013232/pexels-photo-10013232.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
140,
20
);
在那之后,我得到了中心有标志的二维码,但标志的质量很差。我不知道如何提高图像的质量。
在qrcode.toCanvas
选项中添加一个width
。
qrcode.toCanvas(canvas, dataForQRcode, {
errorCorrectionLevel: 'H',
version: 5,
margin: 1,
width: width,
color: {
dark: '#000000',
light: '#ffffff',
},
});