@chillerlan/qrcode

中文文档
1.0.1发布于 2024/08/25MITESM + CJS零依赖近 30 天下载 2276

A QR Code generator - ported from php-qrcode (https://github.com/chillerlan/php-qrcode)

原始使用文档
文档目录3 节
  1. Features
  2. Documentation
  3. Quickstart

chillerlan/js-qrcode

A javascript port of chillerlan/php-qrcode, a QR Code library based on the implementation by Kazuhiko Arase.

NPM version License CodeCov Build

Overview

Features

  • Creation of Model 2 QR Codes, Version 1 to 40
  • ECC Levels L/M/Q/H supported
  • Mixed mode support (encoding modes can be combined within a QR symbol). Supported modes:
    • numeric
    • alphanumeric
    • 8-bit binary
  • Flexible, easily extensible output modules, built-in support for the following output formats:
    • Markup types: SVG, etc.
    • String types: JSON, plain text, etc.
    • Raster image types via Canvas

Documentation

The API is very similar to the PHP version of this library, and you can refer to its documentation and examples for most parts.

Key differences:

  • Class constants of the PHP version are regular constants here, e.g. QRMatrix::M_DATA becomes M_DATA, Version::AUTO is VERSION_AUTO and EccLevel:L to ECC_L - see index.js for the proper names of all symbols.
  • No multimode support for Kanji and Hanzi character sets (handling/converting non-UTF8 strings in javascript is a mess), no ECI support for the same reason.
  • save-to-file is not supported. You can re-implement the method QROutputAbstract::saveToFile() in case you need it. It's called internally, but it has no function.
  • No QR Code reader included
  • The usual javascript quirks:
    • the internal structure of some classes may deviate from their PHP counterparts
    • key-value arrays become objects, causing inconsistent return values in some cases, not to mention the inconsistent loop types for each (pun intended)
    • numbers may act strange
    • magic getters and setters come with downsides, see this comment

An API documentation created with jsdoc can be found at https://chillerlan.github.io/js-qrcode/ (WIP).

Quickstart

Server-side, in nodejs:

import {QRCode} from './dist/js-qrcode-node-src.cjs';

let data   = 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net';
let qrcode = (new QRCode()).render(data);

// do stuff
console.log(qrcode);

Client-side, in a webbrowser:

<div id="qrcode-container"></div>
<script type="module">
    import {QRCode} from './dist/js-qrcode-es6-src.js';
    
    // an SVG image as base64 data URI will be returned by default
    let qrcode = (new QRCode()).render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');

    // append it to the DOM
    let img = document.createElement('img');
    img.alt = 'QRCode';
    img.src = qrcode
    
    document.getElementById('qrcode-container').appendChild(img);
</script>

QR codes are awesome!

Have a look in the examples folder for some more usage examples.

License notice

Parts of this code are ported to js (via php) from the ZXing project and licensed under the Apache License, Version 2.0.

Trademark Notice

The word "QR Code" is a registered trademark of DENSO WAVE INCORPORATED
https://www.qrcode.com/en/faq.html#patentH2Title

版本与发布记录

@chillerlan/qrcode 共发布 2 个版本,首次发布于 2024/08/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.1latest2024/08/252 年前
1.0.02024/08/252 年前

关于 @chillerlan/qrcode 的常见问题

@chillerlan/qrcode 是什么?

A QR Code generator - ported from php-qrcode (https://github.com/chillerlan/php-qrcode)

如何安装 @chillerlan/qrcode?

在项目目录运行 npm install @chillerlan/qrcode,或使用 pnpm add / yarn add / bun add。

@chillerlan/qrcode 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/chillerlan__qrcode 是否存在。

@chillerlan/qrcode 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@chillerlan/qrcode 可以商用吗?

@chillerlan/qrcode 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chillerlan/qrcode 的下载量有多少?

根据 npm 官方下载统计,@chillerlan/qrcode 在 2026/10/10 – 2026/11/08 这 30 天内被下载 2,276 次,日均约 76 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chillerlan/qrcode 还在维护吗?

最新版本 1.0.1 发布于 2024/08/25(2 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/08/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。