服务器端生成二维码
官网:https://github.com/soldair/node-qrcode?tab=readme-ov-file#todataurltext-options-cberror-url-1
1.安装第三方库
npm install qrcode
2.使用qrcode(列举2种方式,官网有更多)
直接生成到文件中
let QRCode = require("qrcode")
QRCode.toFile('./qrcode.png', 'Hello World!', {
color: {
dark: '#fff',
light: '#000'
}
}, err => console.log(err))
生成dataurl
QRCode.toDataURL('http://www.google.com', (err, url) => {
console.log(url)
})
配置项(默认情况下,使用自动模式选择,也可手动配置)
-
错误纠正级别
-
二维码容量
- 编码模式
客户端生成二维码
官网:
https://github.com/davidshimjs/qrcodejs
1.在BootCDN引用这个库,也可npm安装这个库
<script src="https://cdn.bootcdn.net/ajax/libs/qrcodejs/1.0.0/qrcode.js"></script>
npm i qrcodejs
2.使用qrcode.js
<div id="qrcode"></div>
<script type="text/javascript">
var qrcode = new QRCode(document.getElementById("qrcode"), {
text: "Hello World!",
width: 128,
height: 128,
colorDark: "#000",
colorLight: "#fff",
correctLevel: QRCode.CorrectLevel.H
});
</script>