I am having trouble with downloading a QR code canvas image with a background. The download works fine, but my device is unable to scan the code properly due to an incomplete border. I need to add a white background behind it to make it larger.
Current image: https://i.sstatic.net/DYJTx.png
Expected image: https://i.sstatic.net/gMgzI.png
This is my current code:
<a id="download-qr">
<div id="qrCode"></div>
$('#qrCode').qrcode({
render: "canvas",
minVersion: 8,
maxVersion: 8,
ecLevel: 'M',
size: 200,
mode: 0,
text: code,
});
var canvas = $("#qrCode").children(":first")[0];
var ctx = canvas.getContext("2d");
ctx.globalCompositeOperation = 'destination-over';
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
var img = canvas.toDataURL("image/jpeg");
var dl = document.getElementById('download-qr');
dl.setAttribute('href', img);
dl.setAttribute('download', 'test.jpg');
Your assistance would be greatly appreciated. Thank you very much.