示例效果:
可以修改背景图片的宽高,二维码的宽高、位置,头像的宽高、位置等。
代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="jquery.min.js"></script>
<script src="jquery.qrcode.min.js"></script>
<script>
$(function () {
$("#qr").qr({
qr: {
text: "abc",
// width: 100,
// height: 100,
size: 100,
// left: 100,
// top: 10,
// colorDark: "#000000",
// colorLight: "#ffffff",
// correctLevel: 2,
},
bg: {
src: "bg.jpg",
// width: 400,
// height: 400,
scale: 0.5,
},
head: {
src: "head.jpg",
// width: 20,
// height: 20,
scale: 0.1,
// left: 100,
// top: 100,
}
});
});
jQuery.fn.qr = function (x) {
if (!x.qr) {
return false;
}
var _this = this;
var qr = {}, bg = {}, head = {};
var qrImage, bgImage, headImage;
var canvas = document.createElement("canvas");
var canvasContext = canvas.getContext("2d");
setBg();
function setBg() {
if (x.bg && x.bg.src) {
bgImage = new Image();
bgImage.src = x.bg.src;
bgImage.onload = function () {
bg.width = x.bg.scale ? (x.bg.scale * bgImage.width) : (x.bg.width ? x.bg.width : bgImage.width);
bg.height = x.bg.scale ? (x.bg.scale * bgImage.height) : (x.bg.height ? x.bg.height : bgImage.height);
setQr();
}
} else {
setQr();
}
}
function setQr() {
qr.text = x.qr.text ? x.qr.text : "";
qr.width = x.qr.size ? (x.qr.size) : (x.qr.width ? x.qr.width : 256);
qr.height = x.qr.size ? (x.qr.size) : (x.qr.height ? x.qr.height : 256);
qr.colorDark = x.qr.colorDark ? x.qr.colorDark : "#000000";
qr.colorLight = x.qr.colorLight ? x.qr.colorLight : "#ffffff";
qr.correctLevel = x.qr.correctLevel ? x.qr.correctLevel : 2;
qr.left = bg.width ? (x.qr.left ? x.qr.left : (bg.width - qr.width) / 2) : 0;
qr.top = bg.height ? (x.qr.top ? x.qr.top : (bg.height - qr.height) / 2) : 0;
qrImage = new Image();
qrImage.src = _this.qrcode(qr).find("canvas").get(0).toDataURL("image/png");
qrImage.onload = function () {
setHead();
}
}
function setHead() {
if (x.head && x.head.src) {
headImage = new Image();
headImage.src = x.head.src;
headImage.onload = function () {
head.width = x.head.scale ? (x.head.scale * headImage.width) : (x.head.width ? x.head.width : headImage.width);
head.height = x.head.scale ? (x.head.scale * headImage.height) : (x.head.height ? x.head.height : headImage.height);
head.left = x.head.left ? x.head.left : qr.left + (qr.width - head.width) / 2;
head.top = x.head.top ? x.head.top : qr.top + (qr.height - head.height) / 2;
showImage();
}
} else {
showImage();
}
}
function showImage() {
if (x.bg && x.bg.src) {
canvas.width = bg.width;
canvas.height = bg.height;
canvasContext.drawImage(bgImage, 0, 0, bg.width, bg.height);
} else {
canvas.width = qr.width;
canvas.height = qr.height;
}
canvasContext.drawImage(qrImage, qr.left, qr.top, qr.width, qr.height);
if (x.head && x.head.src) {
canvasContext.drawImage(headImage, head.left, head.top, head.width, head.height);
}
_this.empty();
_this.append('<img src="' + canvas.toDataURL("image/jpeg", 0.8) + '">');
}
}
</script>
</head>
<body>
<div id="qr"></div>
</body>
</html>