2024年09月20日 建站教程
如何利用nodejs生成二维码并下载二维码,下面web建站小编给大家详细介绍一下具体代码的使用!
生成二维码代码:
const qrCode = require('qrcode')
class QrController {
async create (ctx) {
const { text = 'Luban', options } = ctx.request.body
const qrOptions = {
type: 'image/png', //生成图片类型:image/png、image/jpeg、 image/web
width: 180, //二维码的宽度
margin: 5, //二维码的内边距
scale: 1, //放缩的倍数
color: {
dark: '#000000', //背景色
light: '#ffffff' //前景色
},
errorCorrectionLevel: 'M', //纠错级别
quality: 1
}
Object.assign(qrOptions, options)
const imgData = await qrCode.toDataURL(text, qrOptions)
return ctx.success({ imgData })
}
}
module.exports = new QrController()
下载二维码代码:
const a = document.createElement('a')
const event = new MouseEvent('click')
a.download = '下载二维码'
a.href = this.imgSrc
a.dispatchEvent(event)
本文链接:http://so.lmcjl.com/news/13476/