前端生成附带有背景图、可添加文字的二维码
npm install jr-qrcod --save
const qrcode = require(\'jr-qrcode\');
<img id=\'mix_img\'/>
//如果需要二维码和图片一起生成,可以选择如下方式
const getImageData = (text,img,name,tel) => { //text生成的二维码链接 img图片链接
const qrcodes = qrcode.getQrBase64(text)//转换base格式
let canvas = document.createElement(\'canvas\')//创建画布
const width = document.documentElement.clientWidth//获取浏览器宽度
const height = document.documentElement.clientHeight//获取浏览器高度
canvas.width = width//给画布设置宽度
canvas.height = height//给画布设置高度
let ctx = canvas.getContext("2d")//此对象是内建的 HTML5 对象,拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。
loadImg([qrcodes,img]).then(([img1, img2]) => {
ctx.drawImage(img2, 0, 0, width, height)//设置图片宽高坐标
ctx.drawImage(img1, ((width + width) / 7.8), (height - (width) / 0.89), width / 2, width / 2) //前两个参数来调整二维码的横纵坐标,后两个参数来调整二维码的大小
ctx.font="24px 黑体"
ctx.fillStyle = \'#2E2E2E\';
ctx.fillText(`${name}邀请您加入洪涛科技`, ((width + width) / 7.5), (height - (width) / 1.9), width / 2, width / 2)
ctx.font="16px 黑体"
ctx.fillStyle = \'white\';
ctx.fillText(`${name} ${tel}`, ((width + width) / 4.5), (height - (width) / 0.78), width / 2, width / 2)
ctx.save()//保存
let imageURL = canvas.toDataURL("image/png")//转换图片为dataURL
document.getElementById(\'mix_img\').setAttribute(\'src\',imageURL);//放到img标签里
localSaveImg = imageURL;
})
}
调用方法,传入对应的参数:
```javascript
getImageData(tgUrl,shareBg,"hello","world");