在开发一个取件的小程序与 nodejs 服务端的过程当中发现,若是使用网上现有的二维码生成 API ,存在某些问题好比请求须要较长时间,数据格式较为固定,因为总体业务上也不须要二维码的风格化,最好能够前端与 nodejs 服务端通用。html
通过简单的调研选择了 QRCode.js前端
QRCode.js 项目node
QRCode.js 是一个用于生成二维码的 JavaScript 库。主要是经过获取 DOM 的标签,再经过 HTML5 Canvas 绘制而成,不依赖任何库。他支持 nodejs 端,浏览器端,以及小程序端。git
npm install --save qrcode
复制代码
const QRCode = require('qrcode')
const options = {
errorCorrectionLevel: 'H',
type: 'terminal',
quality: 0.95,
margin: 1,
color: {
dark: '#000',
light: '#FFF',
},
}
复制代码
根据操做环境,有四个级别可供选择。github
Level | Error resistance |
---|---|
L (Low) | ~7% |
M (Medium) | ~15% |
Q (Quartile) | ~25% |
H (High) | ~30% |
级别越高,提供的抗错误性越好,但会下降符号的容量web
Mode | L | M | Q | H |
---|---|---|---|---|
Numeric | 7089 | 5596 | 3993 | 3057 |
Alphanumeric | 4296 | 3391 | 2420 | 1852 |
Byte | 2953 | 2331 | 1663 | 1273 |
Kanji | 1817 | 1435 | 1024 | 784 |
dark:二维码主体颜色,light:二维码背景颜色面试
Type:指按期望的输出类型,例如数据URL中的 image / png,image / jpeg,image / webp和utf8,SVG,字符串中的终端。npm
quality:设置图像的质量,范围为0-1。 默认值为0.92,仅适用于image / jpeg和image / webp类型canvas
width:设置图像的边长小程序
若是width过小而不能包含二维码的所有信息符号,则此选项将被忽略。
margin:设置图像的外边框距离
scale:设置每几个像素为一个信息点默认为 4
能够经过渲染到 Canvas 画布进行使用
<html>
<body>
<canvas id="canvas"></canvas>
<script src="bundle.js"></script>
</body>
</html>
复制代码
使用前须要使用打包工具构建依赖
var QRCode = require('qrcode')
var canvas = document.getElementById('canvas')
QRCode.toCanvas(canvas, 'sample text', function (error) {
if (error) console.error(error)
console.log('success!');
})
复制代码
或者直接渲染 base64 或者 图像文件 (png ,svg ,jpeg)
转成终端可输出的字符串
toString(text, [options], [cb(error, string)])
返回QR码的字符串表示形式。 若是选择的输出格式是svg,它将返回一个包含xml代码的字符串。
QRCode.toString([
{ data: 'qrcode test in nodejs', mode: 'byte' },
]
).then(qrImage => {
console.log("terminal", qrImage)
}).catch(err => {
console.error(err)
})
复制代码
转成包含二维码图片信息的 Base64 编码 url toDataURL(text, [options], [cb(error, url)])
返回包含QR码图像表示形式的数据URI。 目前仅适用于 image / png类型。
QRCode.toDataURL('qrcode test in nodejs', options).then(qrImage => {
console.log("URL", qrImage)
}).catch(err => {
console.error(err)
})
复制代码
转成图片形式存储 toFile(path, text, [options], [cb(error)])
将QR Code保存到图像文件。 若是未指定options.type,则将从文件扩展名中猜想格式。
QRCode.toFile('./images/qrCode.png', "qrcode test in nodejs", options)
.then(() => {
console.log("success")
}).catch(err => {
console.error(err)
})
复制代码
完整 node-server 端 demo
const QRCode = require('qrcode')
const options = {
errorCorrectionLevel: 'H',
type: 'terminal',
quality: 0.95,
margin: 1,
color: {
dark: '#000',
light: '#FFF',
},
}
QRCode.toString([
{ data: 'qrcode test in nodejs', mode: 'byte' },
]
).then(qrImage => {
console.log("terminal", qrImage)
}).catch(err => {
console.error(err)
})
QRCode.toDataURL('qrcode test in nodejs', options).then(qrImage => {
console.log("URL", qrImage)
}).catch(err => {
console.error(err)
})
QRCode.toFile('./images/qrCode.svg', "qrcode test in nodejs", options)
.then(() => {
console.log("success")
}).catch(err => {
console.error(err)
})
复制代码
QRcode 同时支持 ES5 / ES6 / ES7 的语法
import QRCode from 'qrcode'
// With Callback
QRCode.toString('http://www.google.com', function (err, string) {
if (err) throw err
console.log(string)
})
// With promises
QRCode.toDataURL('I am a pony!')
.then(url => {
console.log(url)
})
.catch(err => {
console.error(err)
})
// With async/await
const generateQR = async text => {
try {
console.log(await QRCode.toDataURL(text))
} catch (err) {
console.error(err)
}
}
复制代码
先在 wxml 文件中,建立绘制的 canvas,并定义好 width, height, canvasId 。
直接引入 js 文件,使用 drawQrcode() 绘制二维码。
因为目前微信小程序已全面支持 npm ,能够选择直接安装,记得要构建 npm 包
npm install weapp-qrcode --save
复制代码
基本使用
import drawQrcode from 'weapp-qrcode'
drawQrcode({
width: 200,
height: 200,
canvasId: 'myQrcode',
text: 'https://github.com/yingye'
})
复制代码
安装
$ npm install wxbarcode
复制代码
使用方法
import wxbarcode from 'wxbarcode'
wxbarcode.barcode('barcode', '1234567890123456789', 680, 200);
wxbarcode.qrcode('qrcode', '1234567890123456789', 420, 420);
复制代码
条形码
函数名:barcode
函数原型:barcode(id, code, width, height)
参数:
二维码
函数名:qrcode
函数原型:qrcode(id, code, width, height)
参数:
调研了不少解决方案,node-qrcode 解决了我大部分问题。小程序社区也有不少的方案,也能够在跨端框架中使用,因为不是生产级项目也没有去调研尝试。但基本应该足以知足需求。
若是你以为这篇内容对你挺有有帮助的话:
点赞支持下吧,让更多的人也能看到这篇内容(收藏不点赞,都是耍流氓 -_-)
关注公众号咸鱼爱前端,咱们一块儿学习一块儿进步。
以为不错的话,也能够阅读其余文章(感谢朋友的鼓励与支持🌹🌹🌹):