uqr
在任何运行时普遍生成二维码,输出为 ANSI、Unicode 或 SVG。
在任何运行时通用生成二维码,可输出为ANSI、Unicode或SVG格式。ES模块,零依赖,支持Tree Shaking。
安装
# Using npm
npm install uqr
# Using yarn
yarn add uqr
# Using pnpm
pnpm add uqr
用法
import {
encode,
renderANSI,
renderSVG,
renderUnicode,
renderUnicodeCompact,
} from 'uqr'
const svg = renderSVG('Hello, World!')
const ansi = renderANSI('https://192.168.1.100:3000', {
// Error correction level
ecc: 'L',
// Border width
border: 2,
})
// display QR Code in terminal
console.log(ansi)
API
encode
将纯文本或二进制数据编码为由二维数组表示的二维码。
import { encode } from 'uqr'
const {
data, // 2D array of boolean, representing the QR Code
version, // QR Code version
size, // size of the QR Code
} = encode(text, options)
renderANSI
将二维码渲染为ANSI彩色字符串。
import { renderANSI } from 'uqr'
const string = renderANSI(text, options)
console.log(string)
renderUnicode
将二维码渲染为每个像素的Unicode字符串。默认情况下,它使用█
和░
表示黑白像素,并且可以自定义。
import { renderUnicode } from 'uqr'
const string = renderUnicode(text, {
blackChar: '█',
whiteChar: '░',
// ...other options
})
renderUnicodeCompact
使用Unicode字符▀
、▄
、█
、
将二维码两行渲染为一行。当你想在高度受限的终端中显示二维码时,这很有用。
import { renderUnicodeCompact } from 'uqr'
const string = renderUnicodeCompact(text, options)
console.log(string)
renderSVG
将二维码渲染为SVG字符串。
import { renderSVG } from 'uqr'
const string = renderSVG(text, options)
致谢
二维码生成算法修改自 Project Nayuki 的 nayuki/QR-Code-generator。
命令行界面(CLI)的渲染灵感来源于 qrcode-terminal。
许可
MIT 许可证