Node.js:浏览器环境下使用qrcode生成二维码
[*]github: https://github.com/soldair/node-qrcode
[*]npmjs: https://www.npmjs.com/package/qrcode
[*]cdn: https://cdn.jsdelivr.net/npm/qrcode/build/qrcode.min.js
Node.js
安装
npm i qrcode
API
toDataURL(text, , )
## 参数:
text String|Array
options: {
version Type: Number
errorCorrectionLevel Type: String Default: M
# low, medium, quartile, high or L, M, Q, H
maskPattern Type: Number
# 0, 1, 2, 3, 4, 5, 6, 7.
toSJISFunc Type: Function
type Type: String Default: image/png
# image/png, image/jpeg, image/webp
quality Type: Number Default: 0.92
# Renderers options
marginType: Number Default: 4
scale Type: Number Default: 4
width Type: Number
color: {
darkType: String Default: #000000ff
light Type: String Default: #ffffffff
}
}
cb Type: Function
使用示例
import QRCode from 'qrcode'
// With promises
QRCode.toDataURL('I am a pony!')
.then(url => {
console.log(url)
})
.catch(err => {
console.error(err)
})
生成的二维码(边框和文字是我加的)
Browser
配合jquery使用
<script src="https://cdn.jsdelivr.net/npm/qrcode/build/qrcode.min.js"></script>
<script>
$(document).ready(function () {
QRCode.toDataURL(window.location.href, { errorCorrectionLevel: 'H' }, function (err, url) {
// console.log('QRCode', url)
$('#weixin-code').attr('src', url)
})
});
</script>
文档来源:51CTO技术博客https://blog.51cto.com/u_13567403/3039799
页:
[1]