节点-qrcode
QR 代码/2d条码生成器.
要点
- 在服务器和客户端上的工作( 用 svg 进行本地反应)
- CLI 工具
- QR 代码另存为图像
- 支持数字、 字母、 坎吉和字节模式
- 支持混合模式
- 支持中国人、希腊人和日本人
- 支持多字节字符( 如 emojis : smile :)
- 自动生成最佳数据压缩和最小QR码大小的优化片段
- 应用不可知读性, QR 代码的定义是应用不可知性
安装
在您的工程文件夹中 :
npm install --save qrcode
或,在全球安装用于 qrcode 从命令行中保存 qrcode 图像或生成您可以在终端中查看的图像.
npm install -g qrcode
使用量
国 际
Usage: qrcode [options] <input string>
QR Code options:
-v, --qversion QR Code symbol version (1 - 40) [number]
-e, --error Error correction level [choices: "L", "M", "Q", "H"]
-m, --mask Mask pattern (0 - 7) [number]
Renderer options:
-t, --type Output type [choices: "png", "svg", "utf8"]
-w, --width Image width (px) [number]
-s, --scale Scale factor [number]
-q, --qzone Quiet zone size [number]
-l, --lightcolor Light RGBA hex color
-d, --darkcolor Dark RGBA hex color
--small Output smaller QR code to terminal [boolean]
Options:
-o, --output Output file
-h, --help Show help [boolean]
--version Show version number [boolean]
Examples:
qrcode "some text" Draw in terminal window
qrcode -o out.png "some text" Save as png image
qrcode -d F00 -o out.png "some text" Use red as foreground color
如果没有指定,则从文件扩展名中猜测输出类型.
公认的扩展为 png, (中文(简体) ), svg 财务报告和审定财务报表 txt。 。 。 .
浏览器
node-qrcode 可以通过模块捆绑器在浏览器中使用 浏览器 财务报告和审定财务报表 网络软件包 或将预编成的捆绑包含在 build/ 文件夹.
模块捆绑器
<!-- index.html -->
<html>
<body>
<canvas id="canvas"></canvas>
<script src="bundle.js"></script>
</body>
</html>
// index.js -> bundle.js
var QRCode = require('qrcode')
var canvas = document.getElementById('canvas')
QRCode.toCanvas(canvas, 'sample text', function (error) {
if (error) console.error(error)
console.log('success!');
})
预编译捆绑
<canvas id="canvas"></canvas>
<script src="/build/qrcode.js"></script>
<script>
QRCode.toCanvas(document.getElementById('canvas'), 'sample text', function (error) {
if (error) console.error(error)
console.log('success!');
})
</script>
如果您安装通过 npm,预编译文件将在 node_modules/qrcode/build/ 文件夹.
预编集有支持 Internet Explorer 10+, Safari 5.1+, 以及所有常绿浏览器。 。 。 .
节点JS
需要模块 qrcode
var QRCode = require('qrcode')
QRCode.toDataURL('I am a pony!', function (err, url) {
console.log(url)
})
制作终端的 qrcode
var QRCode = require('qrcode')
QRCode.toString('I am a pony!',{type:'terminal'}, function (err, url) {
console.log(url)
})
ES6/ES7 (英语)
承诺和Async/Await可以用来代替回调功能.
import QRCode from 'qrcode'
// 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)
}
}
错误更正级别
出错校正能力允许成功扫描一个QR代码,即使该符号是脏的或被损坏. 根据操作环境可以选择四个等级.
更高等能提供更好的防出错,但能降低符号的能力.
如果QR 代码符号被损坏的可能性很低( 例如通过显示器显示)
可以安全使用低出错级别,例如 Low 或者说 Medium。 。 。 .
可能的水平如下:
| 职等 | 防错误 |
|---|---|
| L (低声) | ~7%( |
| 联合国 (ium | - 15% |
| Q 问 (夸特尔语) | 约25% |
| 页:1 (高声) | ~30% |
百分比表示受损地表的最大数量,之后该符号变得无法读取.
错误级别可以通过 options.errorCorrectionLevel 属性.
如果没有指定,则默认值为 M。 。 。 .
QRCode.toDataURL('some text', { errorCorrectionLevel: 'H' }, function (err, url) {
console.log(url)
})
QR 代码容量
能力取决于符号版本和错误校正级别. 另外编码模式也可能影响可存储数据的数量.
QR 代码版本从版本到版本 1个 改为版本 第40条。 。 。 .
每个版本都有不同数量的模块(黑白点),它们定义了符号的大小.
对第一版来说,他们是 21x21,用于版本2 25x25 e类.
更高的是版本,更多的是可存储的数据,当然更大的是QR代码符号.
下表列出每个编码模式和每个错误更正级别中可存储字符的最大数量.
| 模式 | L | 联合国 | Q 问 | 页:1 |
|---|---|---|---|---|
| 数字 | 7089 (英语) | 5596 (英语) | 第3993条 | 3057 (英语) |
| 数字 | 4296 (英语) | 3391 (英语) | 2420 (英语) | 1852 (英语) |
| 字节 | 第2953条 | 2331号 | 1663 (韩语) | 第1273号 |
| Kan | 1817 (英语) | 第1435号 | 第1024号 | 784个 |
说明: 最大字符编号在使用时可能不同 混合模式。 。 。 .
QR 码版本可以通过 options.version 属性.
如果没有指定版本,则会使用更合适的值. 除非需要特定版本,否则不需要此选项.
QRCode.toDataURL('some text', { version: 2 }, function (err, url) {
console.log(url)
})
编码模式
模式可以用更高效的方式来编码字符串.
一个模式可能比其他模式更适合,取决于字符串内容.
下表列出了所支持的模式:
| 模式 | 字符 | 压缩 |
|---|---|---|
| 数字 | 0、1、2、3、4、5、6、7、8、9 | 3个字符用10个比特表示 |
| 数字 | 0–9, A-Z(仅上例), 空格, $, %, *, +, -., /, : | 2个字符由11个比特代表 |
| Kan | 基于JIS X 0208的Shift JIS系统的字符 | 2个kanji代表13位 |
| 字节 | ISO/IEC 8859-1字符集中的字符 | 每个字符由8位代表 |
如果输入文本未知, 选择正确的模式可能很棘手 .
在这些情况下 字节 模式是最佳选择,因为所有字符都可以用它编码。 (见 多字节字符(中文(简体) )
然而,如果QR码读取器支持混合模式,则使用 自动模式 可能会产生更好的结果.
混合模式
混合模式也是可能的。 QR代码可以从一系列具有不同编码模式的段来生成,以优化数据压缩.
然而,从一种模式转换到另一种模式会产生成本,如果不考虑成本,可能导致最坏的结果.
见 手动模式 例如,如何指定有不同编码模式的片段.
自动模式
以 默认,使用自动模式选择.
输入字符串被自动分解为被优化的各段,以便使用混合模式产生尽可能最短的位流.
这是生成QR码的首选方式.
例如,字符串 ABCDE12345678 (英语). A1A 对 将分为3个段,模式如下:
| 部分 | 模式 |
|---|---|
| ABCDE 亚足联 | 数字 |
| 12345678 (英语) | 数字 |
| ? 吗? A1A 对 | 字节 |
任何其它的片段和模式的组合,都会使比特流更长.
如果您需要保持QR码大小小,此模式将产生最佳效果.
手动模式
如果自动模式对您不起作用或者您有特定的需求,也可以用相对模式手动指定每个段.
这样,不会在罩下应用分段优化.
片段列表可以作为对象数组传递 :
var QRCode = require('qrcode')
var segs = [
{ data: 'ABCDEFG', mode: 'alphanumeric' },
{ data: '0123456', mode: 'numeric' }
]
QRCode.toDataURL(segs, function (err, url) {
console.log(url)
})
坎吉模式
使用kanji模式可以优化从Shift JIS系统中编码字符.
不幸的是,没有办法从UTF-8编码的字符中计算出一个已转移的JIS值,因此需要从输入字符到SJIS值的转换表.
本表默认不包含在捆绑中,以尽量保持大小.
如果您的应用程序需要 kanji 支持, 您需要通过一个功能来将输入字符转换为合适的值 .
帮助方法由 lib 通过可选文件提供,您可以包含如下示例所示.
说明: 只有当您想要从数据压缩中受益时,才需要支持Kanji模式,否则仍有可能使用字节模式编码Kanji(见 多字节字符) (中文(简体) ).
var QRCode = require('qrcode')
var toSJIS = require('qrcode/helper/to-sjis')
QRCode.toDataURL(kanjiString, { toSJISFunc: toSJIS }, function (err, url) {
console.log(url)
})
使用预编译的捆绑 :
<canvas id="canvas"></canvas>
<script src="/build/qrcode.min.js"></script>
<script src="/build/qrcode.tosjis.min.js"></script>
<script>
QRCode.toCanvas(document.getElementById('canvas'),
'sample text', { toSJISFunc: QRCode.toSJIS }, function (error) {
if (error) console.error(error)
console.log('success!')
})
</script>
二进制数据
QR代码可以持有任意的以字节为基础的二进制数据. 如果尝试通过先将数据转换为JavaScript字符串来创建二进制QR代码,则会因为字符串编码增加了额外的字节而无法编码正版. 相反,你必须通过一个 Uint8ClampedArray 或兼容的数组,或节点 缓冲,内容如下:
// Regular array example
// WARNING: Element values will be clamped to 0-255 even if your data contains higher values.
const QRCode = require('qrcode')
QRCode.toFile(
'foo.png',
[{ data: [253,254,255], mode: 'byte' }],
...options...,
...callback...
)
// Uint8ClampedArray example
const QRCode = require('qrcode')
QRCode.toFile(
'foo.png',
[{ data: new Uint8ClampedArray([253,254,255]), mode: 'byte' }],
...options...,
...callback...
)
// Node Buffer example
// WARNING: Element values will be clamped to 0-255 even if your data contains higher values.
const QRCode = require('qrcode')
QRCode.toFile(
'foo.png',
[{ data: Buffer.from([253,254,255]), mode: 'byte' }],
...options...,
...callback...
)
类型脚本用户: 如果您正在使用 @ t类型/ qrcode,您需要添加一个 // @ts-ignore 在数据段之上,因为它期望 data: string。 。 。 .
多字节字符
对多字节字符的支持在最初的QR代码标准中并不存在,而是可以在字节模式下将UTF-8字符编码.
QR代码提供了一个通过ECI(扩展通道解释)来指定不同类型字符集的方法,但它在这个 lib 中还没有完全执行.
多数 QR 然而,代码阅读器即使没有ECI也能识别多字节字符.
注意,单个Kanji/Kana或Emoji最多可取取4个字节.
API 密码
浏览器 :
服务器 :
浏览器 API
create(text, [options])
创建 QR 代码符号并返回 qrcode 对象.
text
类型 : String|Array
要编码的文字或描述片段的对象列表.
options
见 QR 代码选项。 。 。 .
returns
类型 : Object
// QRCode object
{
modules, // Bitmatrix class with modules data
version, // Calculated QR Code version
errorCorrectionLevel, // Error Correction Level
maskPattern, // Calculated Mask pattern
segments // Generated segments
}
toCanvas(canvasElement, text, [options], [cb(error)])
toCanvas(text, [options], [cb(error, canvas)])
绘制 qr 代码符号来画画 .
若为 canvasElement 中省略新画布.
canvasElement
类型 : DOMElement
画出 QR 代码的 Canvas .
text
类型 : String|Array
要编码的文字或描述片段的对象列表.
options
见 选项。 。 。 .
cb
类型 : Function
回调函数调用完成 .
示例
QRCode.toCanvas('text', { errorCorrectionLevel: 'H' }, function (err, canvas) {
if (err) throw err
var container = document.getElementById('container')
container.appendChild(canvas)
})
toDataURL(text, [options], [cb(error, url)])
toDataURL(canvasElement, text, [options], [cb(error, url)])
返回包含 QR 代码图像的表示值的数据 URI .
如果提供, canvasElement 用于生成数据 URI.
canvasElement
类型 : DOMElement
画出 QR 代码的 Canvas .
text
类型 : String|Array
要编码的文字或描述片段的对象列表.
options
-
type类型 :
String
默认 :image/png数据URI格式.
可能的值是:image/png, (中文(简体) ),image/jpeg, (中文(简体) ),image/webp。 。 。 . -
rendererOpts.quality类型 :
Number
默认 :0.92A 数字之间
0财务报告和审定财务报表1如果请求的类型是图像质量image/jpeg或者说image/webp。 。 。 .
见 选项 用于其他设置.
cb
类型 : Function
回调函数调用完成 .
示例
var opts = {
errorCorrectionLevel: 'H',
type: 'image/jpeg',
quality: 0.3,
margin: 1,
color: {
dark:"#010599FF",
light:"#FFBF60FF"
}
}
QRCode.toDataURL('text', opts, function (err, url) {
if (err) throw err
var img = document.getElementById('image')
img.src = url
})
toString(text, [options], [cb(error, string)])
返回 QR 代码的字符串表达式.
text
类型 : String|Array
要编码的文字或描述片段的对象列表.
options
-
type类型 :
String
默认 :utf8输出格式.
可能的值是:terminal, (中文(简体) ),utf8,以及svg。 。 。 .
见 选项 用于其他设置.
cb
类型 : Function
回调函数调用完成 .
示例
QRCode.toString('http://www.google.com', function (err, string) {
if (err) throw err
console.log(string)
})
服务器 API
create(text, [options])
见 创建。 。 。 .
toCanvas(canvas, text, [options], [cb(error)])
绘制 qr 代码符号到 节点画布。 。 。 .
text
类型 : String|Array
要编码的文字或描述片段的对象列表.
options
见 选项。 。 。 .
cb
类型 : Function
回调函数调用完成 .
toDataURL(text, [options], [cb(error, url)])
返回包含 QR 代码图像的表示值的数据 URI .
仅与 image/png 类型.
text
类型 : String|Array
要编码的文字或描述片段的对象列表.
options
见 选项 用于其他设置.
cb
类型 : Function
回调函数调用完成 .
toString(text, [options], [cb(error, string)])
返回 QR 代码的字符串表达式.
如果所选输出格式是 svg 它将返回含有 xml 代码的字符串.
text
类型 : String|Array
要编码的文字或描述片段的对象列表.
options
-
type类型 :
String
默认 :utf8输出格式.
可能的值是:utf8, (中文(简体) ),svg, (中文(简体) ),terminal。 。 。 .
见 选项 用于其他设置.
cb
类型 : Function
回调函数调用完成 .
示例
QRCode.toString('http://www.google.com', function (err, string) {
if (err) throw err
console.log(string)
})
toFile(path, text, [options], [cb(error)])
保存 QR 图像文件代码 .
若为 options.type 未指定,格式将从文件扩展名中猜测.
公认的扩展为 png, (中文(简体) ), svg, (中文(简体) ), txt。 。 。 .
path
类型 : String
保存文件的路径 .
text
类型 : String|Array
要编码的文字或描述片段的对象列表.
options
-
type类型 :
String
默认 :png输出格式.
可能的值是:png, (中文(简体) ),svg, (中文(简体) ),utf8。 。 。 . -
rendererOpts.deflateLevel(仅png)类型 :
Number
默认 :9降能压平等分.
-
rendererOpts.deflateStrategy(仅png)类型 :
Number
默认 :3降压策略.
见 选项 用于其他设置.
cb
类型 : Function
回调函数调用完成 .
示例
QRCode.toFile('path/to/filename.png', 'Some text', {
color: {
dark: '#00F', // Blue dots
light: '#0000' // Transparent background
}
}, function (err) {
if (err) throw err
console.log('done')
})
toFileStream(stream, text, [options])
写入 QR 代码图像到流 。 仅与 png 目前的格式.
stream
类型 : stream.Writable
节点流.
text
类型 : String|Array
要编码的文字或描述片段的对象列表.
options
见 选项。 。 。 .
选项
QR 代码选项
version
类型 : Number
QR代码版本. 如果没有指定,则将计算更合适的值.
errorCorrectionLevel
类型 : String
默认 : M
错误校正级别 .
可能值为 low, medium, quartile, high 或者说 L, M, Q, H。 。 。 .
maskPattern
类型 : Number
面具图案用来遮住符号.
可能值为 0, (中文(简体) ), 1, (中文(简体) ), 2, (中文(简体) ), 3, (中文(简体) ), 4, (中文(简体) ), 5, (中文(简体) ), 6, (中文(简体) ), 7。 。 。 .
如果没有指定,则将计算更合适的值.
toSJISFunc
类型 : Function
内部用于将 kanji 转换为 Shift JIS 值的帮助函数 .
如果您需要坎吉模式的支持, 请提供此功能 .
渲染者选项
margin
类型 : Number
默认 : 4
确定安静区应该有多宽.
scale
类型 : Number
默认 : 4
尺度因子. 价值为 1 指每个模块1px(黑点).
small
类型 : Boolean
默认 : false
仅与终端渲染器有关。 输出更小的QR代码.
width
类型 : Number
强制输出图像的特定宽度 .
如果宽度太小无法包含 qr 符号, 此选项将被忽略 .
胜过 scale。 。 。 .
color.dark
类型 : String
默认 : #000000ff
黑色模块的颜色 。 值必须采用十六进制格式(RGBA).
注意: 暗色应该总是比 color.light。 。 。 .
color.light
类型 : String
默认 : #ffffffff
亮相模块的颜色 。 值必须采用十六进制格式(RGBA).
GS1 QR 码
这里对他们进行了很好的讨论。 但简而言之,任何 qrcode 生成器都会使 gs1 兼容 qrcode, 但是定义 gs1 qrcode 的是一个带有元数据来描述您的 gs1 信息的头.
https://github.com/soldair/node-qrcode/issues/45 互联网档案馆的存檔,存档日期2013-12-21
贷项
这个lib是基于"QRCode for JavaScript",Kazuhiko Arase感激地获得了麻省理工学院的许可.
许可证
"QR代码"是:
戴森·怀特(签名)