html5-qrcode vs qr-scanner vs qrcode-reader vs qrious
Web 端二维码扫描与生成技术选型
html5-qrcodeqr-scannerqrcode-readerqrious类似的npm包:

Web 端二维码扫描与生成技术选型

html5-qrcodeqr-scanner 是现代 Web 端二维码扫描的主流方案,前者提供开箱即用的 UI 组件,后者更轻量且适合自定义界面。qrious 专注于二维码生成,基于 Canvas 渲染。qrcode-reader 是早期的纯 JS 解码库,目前已不再维护。开发者应根据项目是需要扫描还是生成,以及对 UI 控制权和包体积的要求来选择。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
html5-qrcode06,1302.63 MB4353 年前Apache-2.0
qr-scanner02,866524 kB118-MIT
qrcode-reader0283-179 年前Apache-2.0
qrious01,625-439 年前GPL-3.0

Web 端二维码扫描与生成方案深度对比

在 Web 开发中,处理二维码通常涉及两个方向:扫描(识别)和生成(绘制)。html5-qrcodeqr-scannerqrcode-reader 主要用于扫描,而 qrious 专注于生成。了解它们的底层差异能帮助你避免选型错误。

📷 扫描初始化与摄像头控制

扫描功能的核心在于如何调用摄像头并解析视频流。不同库对权限管理和视频元素的处理方式不同。

html5-qrcode 提供了封装好的类,支持直接绑定 DOM 元素。

// html5-qrcode: 初始化扫描实例
import { Html5Qrcode } from "html5-qrcode";

const html5QrCode = new Html5Qrcode("reader");
const config = { fps: 10, qrbox: { width: 250, height: 250 } };

html5QrCode.start({ facingMode: "environment" }, config, onScanSuccess);

qr-scanner 需要你自己提供 video 元素,控制更灵活。

// qr-scanner: 绑定现有 video 元素
import QrScanner from "qr-scanner";

const video = document.getElementById("video");
const scanner = new QrScanner(video, result => console.log(result));

scanner.start();

qrcode-reader 使用较旧的回调模式,且不支持直接处理视频流,通常需配合 getUserMedia 手动处理。

// qrcode-reader: 需手动获取图像数据
import QrcodeReader from "qrcode-reader";

const reader = new QrcodeReader();
reader.callback = (err, value) => { /* 处理结果 */ };
// 需手动将视频帧绘制到 canvas 再 decode
reader.decode(canvasContext.getImageData(...)); 

qrious 不支持扫描功能。

// qrious: 不支持扫描
// 该库仅用于生成二维码,无法用于识别摄像头数据

🎨 二维码生成能力

生成二维码是将文本或 URL 转换为图像的过程。这部分功能在扫描库中通常缺失,需要专用库。

html5-qrcode 主要专注于扫描,原生不支持生成。

// html5-qrcode: 不支持生成
// 该库设计初衷为解码,生成需引入额外库如 qrcode

qr-scanner 同样专注于扫描,不包含生成逻辑。

// qr-scanner: 不支持生成
// 需配合其他生成库使用,保持包体积轻量

qrcode-reader 仅用于读取,无法生成。

// qrcode-reader: 不支持生成
// 这是一个纯解码库,不具备编码能力

qrious 专为生成设计,支持多种渲染方式。

// qrious: 初始化并生成
import QRious from "qrious";

const qr = new QRious({
  element: document.getElementById("canvas"),
  value: "https://example.com",
  size: 200
});

🛠️ 维护状态与兼容性

库的维护状态直接影响项目的长期稳定性。废弃的库可能导致安全漏洞或无法在新浏览器中运行。

html5-qrcode 持续更新,社区活跃。

// html5-qrcode: 活跃维护
// 定期修复 bug 并适配新浏览器 API,适合生产环境

qr-scanner 由知名团队维护,稳定性高。

// qr-scanner: 活跃维护
// 专注于性能优化,适合对速度敏感的应用

qrcode-reader 已停止维护,不建议使用。

// qrcode-reader: 已废弃
// 最后更新于数年前,存在兼容性风险,应避免在新项目中使用

qrious 维护状态良好,功能稳定。

// qrious: 稳定维护
// 作为生成库,需求变化小,版本稳定可靠

📊 功能对比总结

特性html5-qrcodeqr-scannerqrcode-readerqrious
核心用途扫描 (带 UI)扫描 (无 UI)扫描 ( legacy )生成
摄像头支持✅ 自动管理✅ 手动绑定⚠️ 需手动处理❌ 不支持
生成二维码❌ 不支持❌ 不支持❌ 不支持✅ 支持
维护状态🟢 活跃🟢 活跃🔴 废弃🟢 稳定
自定义程度

💡 最终建议

扫描场景:优先选择 html5-qrcodeqr-scanner。如果你希望少写代码、快速上线,html5-qrcode 的内置 UI 能节省大量时间。如果你需要完全控制界面样式或追求更小的包体积,qr-scanner 是更好的选择。务必避开 qrcode-reader,因为它已不再维护。

生成场景:直接使用 qrious。它在 Canvas 渲染和配置灵活性上表现良好,且专门为此设计,无需引入沉重的扫描库。

混合场景:如果项目既需要扫描又需要生成,建议组合使用 qr-scanner(或 html5-qrcode)与 qrious。这样既能保证性能,又能明确分离关注点,避免引入不必要的代码。

如何选择: html5-qrcode vs qr-scanner vs qrcode-reader vs qrious

  • html5-qrcode:

    选择 html5-qrcode 如果你需要快速集成扫描功能且希望自带 UI 界面。它适合后台管理系统、活动签到等需要快速落地的场景,减少了自定义摄像头权限和界面布局的工作量。

  • qr-scanner:

    选择 qr-scanner 如果你需要极致的性能或完全自定义的扫描界面。它体积更小,依赖更少,适合对用户体验要求极高或需要深度定制扫描框样式的前端项目。

  • qrcode-reader:

    不要在新的项目中使用 qrcode-reader。该库已多年未更新,存在潜在的安全风险和兼容性问题。请迁移到 html5-qrcodeqr-scanner 以获得更好的维护支持。

  • qrious:

    选择 qrious 如果你的需求是生成二维码而非扫描。它支持 Canvas 和 SVG 渲染,配置灵活,适合生成用户名片、支付码或分享链接等静态二维码场景。

html5-qrcode的README

Html5-QRCode

Lightweight & cross platform QR Code and Bar code scanning library for the web

Use this lightweight library to easily / quickly integrate QR code, bar code, and other common code scanning capabilities to your web application.

Key highlights

Supports two kinds of APIs

  • Html5QrcodeScanner — End-to-end scanner with UI, integrate with less than ten lines of code.

  • Html5Qrcode — Powerful set of APIs you can use to build your UI without worrying about camera setup, handling permissions, reading codes, etc.

Support for scanning local files on the device is a new addition and helpful for the web browser which does not support inline web-camera access in smartphones. Note: This doesn't upload files to any server — everything is done locally.

CircleCI GitHub issues GitHub tag (latest by date) GitHub Codacy Badge Gitter

GitHub all releases npm

Demo at scanapp.orgDemo at qrcode.minhazav.dev - Scanning different types of codes

We need your help!

image Help incentivise feature development, bug fixing by supporting the sponsorhip goals of this project. See list of sponsered feature requests here.

ko-fi

Documentation

The documentation for this project has been moved to scanapp.org/html5-qrcode-docs.

Supported platforms

We are working continuously on adding support for more and more platforms. If you find a platform or a browser where the library is not working, please feel free to file an issue. Check the demo link to test it out.

Legends

  • Means full support — inline webcam and file based
  • Means partial support — only file based, webcam in progress

PC / Mac

Firefox
Firefox
Chrome
Chrome
Safari
Safari
Opera
Opera
Edge
Edge

Android

Chrome
Chrome
Firefox
Firefox
Edge
Edge
Opera
Opera
Opera-Mini
Opera Mini
UC
UC

IOS

Safari
Safari
Chrome
Chrome
Firefox
Firefox
Edge
Edge
**

* Supported for IOS versions >= 15.1

Before version 15.1, Webkit for IOS is used by Chrome, Firefox, and other browsers in IOS and they do not have webcam permissions yet. There is an ongoing issue on fixing the support for iOS - issue/14

Framework support

The library can be easily used with several other frameworks, I have been adding examples for a few of them and would continue to add more.

Html5VueJsElectronJsReactLit

Supported Code formats

Code scanning is dependent on Zxing-js library. We will be working on top of it to add support for more types of code scanning. If you feel a certain type of code would be helpful to have, please file a feature request.

CodeExample
QR Code
AZTEC
CODE_39
CODE_93
CODE_128
ITF
EAN_13
EAN_8
PDF_417
UPC_A
UPC_E
DATA_MATRIX
MAXICODE*
RSS_14*
RSS_EXPANDED*

*Formats are not supported by our experimental integration with native BarcodeDetector API integration (Read more).

Description - View Demo

See an end to end scanner experience at scanapp.org.

This is a cross-platform JavaScript library to integrate QR code, bar codes & a few other types of code scanning capabilities to your applications running on HTML5 compatible browser.

Supports:

  • Querying camera on the device (with user permissions)
  • Rendering live camera feed, with easy to use user interface for scanning
  • Supports scanning a different kind of QR codes, bar codes and other formats
  • Supports selecting image files from the device for scanning codes

How to use

Find detailed guidelines on how to use this library on scanapp.org/html5-qrcode-docs.

Demo


Scan this image or visit blog.minhazav.dev/research/html5-qrcode.html

For more information

Check these articles on how to use this library:

Screenshots

screenshot
Figure: Screenshot from Google Chrome running on MacBook Pro

Documentation

Find the full API documentation at scanapp.org/html5-qrcode-docs/docs/apis.

Extra optional configuration in start() method

Configuration object that can be used to configure both the scanning behavior and the user interface (UI). Most of the fields have default properties that will be used unless a different value is provided. If you do not want to override anything, you can just pass in an empty object {}.

fps — Integer, Example = 10

A.K.A frame per second, the default value for this is 2, but it can be increased to get faster scanning. Increasing too high value could affect performance. Value >1000 will simply fail.

qrboxQrDimensions or QrDimensionFunction (Optional), Example = { width: 250, height: 250 }

Use this property to limit the region of the viewfinder you want to use for scanning. The rest of the viewfinder would be shaded. For example, by passing config { qrbox : { width: 250, height: 250 } }, the screen will look like:

This can be used to set a rectangular scanning area with config like:

let config = { qrbox : { width: 400, height: 150 } }

This config also accepts a function of type

/**
  * A function that takes in the width and height of the video stream 
* and returns QrDimensions.
* 
* Viewfinder refers to the video showing camera stream.
*/
type QrDimensionFunction =
    (viewfinderWidth: number, viewfinderHeight: number) => QrDimensions;

This allows you to set dynamic QR box dimensions based on the video dimensions. See this blog article for example: Setting dynamic QR box size in Html5-qrcode - ScanApp blog

This might be desirable for bar code scanning.

If this value is not set, no shaded QR box will be rendered and the scanner will scan the entire area of video stream.

aspectRatio — Float, Example 1.777778 for 16:9 aspect ratio

Use this property to render the video feed in a certain aspect ratio. Passing a nonstandard aspect ratio like 100000:1 could lead to the video feed not even showing up. Ideal values can be:

ValueAspect RatioUse Case
1.3333344:3Standard camera aspect ratio
1.77777816:9Full screen, cinematic
1.01:1Square view

If you do not pass any value, the whole viewfinder would be used for scanning. Note: this value has to be smaller than the width and height of the QR code HTML element.

disableFlip — Boolean (Optional), default = false

By default, the scanner can scan for horizontally flipped QR Codes. This also enables scanning QR code using the front camera on mobile devices which are sometimes mirrored. This is false by default and I recommend changing this only if:

  • You are sure that the camera feed cannot be mirrored (Horizontally flipped)
  • You are facing performance issues with this enabled.

Here's an example of a normal and mirrored QR Code

Normal QR CodeMirrored QR Code

rememberLastUsedCamera — Boolean (Optional), default = true

If true the last camera used by the user and weather or not permission was granted would be remembered in the local storage. If the user has previously granted permissions — the request permission option in the UI will be skipped and the last selected camera would be launched automatically for scanning.

If true the library shall remember if the camera permissions were previously granted and what camera was last used. If the permissions is already granted for "camera", QR code scanning will automatically * start for previously used camera.

supportedScanTypes - Array<Html5QrcodeScanType> | []

This is only supported for Html5QrcodeScanner.

Default = [Html5QrcodeScanType.SCAN_TYPE_CAMERA, Html5QrcodeScanType.SCAN_TYPE_FILE]

This field can be used to:

  • Limit support to either of Camera or File based scan.
  • Change default scan type.

How to use:

function onScanSuccess(decodedText, decodedResult) {
  // handle the scanned code as you like, for example:
  console.log(`Code matched = ${decodedText}`, decodedResult);
}

let config = {
  fps: 10,
  qrbox: {width: 100, height: 100},
  rememberLastUsedCamera: true,
  // Only support camera scan type.
  supportedScanTypes: [Html5QrcodeScanType.SCAN_TYPE_CAMERA]
};

let html5QrcodeScanner = new Html5QrcodeScanner(
  "reader", config, /* verbose= */ false);
html5QrcodeScanner.render(onScanSuccess);

For file based scan only choose:

supportedScanTypes: [Html5QrcodeScanType.SCAN_TYPE_FILE]

For supporting both as it is today, you can ignore this field or set as:

supportedScanTypes: [
  Html5QrcodeScanType.SCAN_TYPE_CAMERA,
  Html5QrcodeScanType.SCAN_TYPE_FILE]

To set the file based scan as defult change the order:

supportedScanTypes: [
  Html5QrcodeScanType.SCAN_TYPE_FILE,
  Html5QrcodeScanType.SCAN_TYPE_CAMERA]

showTorchButtonIfSupported - boolean | undefined

This is only supported for Html5QrcodeScanner.

If true the rendered UI will have button to turn flash on or off based on device + browser support. The value is false by default.

Scanning only specific formats

By default, both camera stream and image files are scanned against all the supported code formats. Both Html5QrcodeScanner and Html5Qrcode classes can be configured to only support a subset of supported formats. Supported formats are defined in enum Html5QrcodeSupportedFormats.

enum Html5QrcodeSupportedFormats {
  QR_CODE = 0,
  AZTEC,
  CODABAR,
  CODE_39,
  CODE_93,
  CODE_128,
  DATA_MATRIX,
  MAXICODE,
  ITF,
  EAN_13,
  EAN_8,
  PDF_417,
  RSS_14,
  RSS_EXPANDED,
  UPC_A,
  UPC_E,
  UPC_EAN_EXTENSION,
}

I recommend using this only if you need to explicitly omit support for certain formats or want to reduce the number of scans done per second for performance reasons.

Scanning only QR code with Html5Qrcode

const html5QrCode = new Html5Qrcode(
  "reader", { formatsToSupport: [ Html5QrcodeSupportedFormats.QR_CODE ] });
const qrCodeSuccessCallback = (decodedText, decodedResult) => {
    /* handle success */
};
const config = { fps: 10, qrbox: { width: 250, height: 250 } };

// If you want to prefer front camera
html5QrCode.start({ facingMode: "user" }, config, qrCodeSuccessCallback);

Scanning only QR code and UPC codes with Html5QrcodeScanner

function onScanSuccess(decodedText, decodedResult) {
  // Handle the scanned code as you like, for example:
  console.log(`Code matched = ${decodedText}`, decodedResult);
}

const formatsToSupport = [
  Html5QrcodeSupportedFormats.QR_CODE,
  Html5QrcodeSupportedFormats.UPC_A,
  Html5QrcodeSupportedFormats.UPC_E,
  Html5QrcodeSupportedFormats.UPC_EAN_EXTENSION,
];
const html5QrcodeScanner = new Html5QrcodeScanner(
  "reader",
  {
    fps: 10,
    qrbox: { width: 250, height: 250 },
    formatsToSupport: formatsToSupport
  },
  /* verbose= */ false);
html5QrcodeScanner.render(onScanSuccess);

Experimental features

The library now supports some experimental features which are supported in the library but not recommended for production usage either due to limited testing done or limited compatibility for underlying APIs used. Read more about it here. Some experimental features include:

How to modify and build

  1. Code changes should only be made to /src only.

  2. Run npm install to install all dependencies.

  3. Run npm run-script build to build JavaScript output. The output JavaScript distribution is built to /dist/html5-qrcode.min.js. If you are developing on Windows OS, run npm run-script build-windows.

  4. Testing

    • Run npm test
    • Run the tests before sending a pull request, all tests should run.
    • Please add tests for new behaviors sent in PR.
  5. Send a pull request

    • Include code changes only to ./src. Do not change ./dist manually.
    • In the pull request add a comment like
    @all-contributors please add @mebjas for this new feature or tests
    
    • For calling out your contributions, the bot will update the contributions file.
    • Code will be built & published by the author in batches.

How to contribute

You can contribute to the project in several ways:

  • File issue ticket for any observed bug or compatibility issue with the project.
  • File feature request for missing features.
  • Take open bugs or feature request and work on it and send a Pull Request.
  • Write unit tests for existing codebase (which is not covered by tests today). Help wanted on this - read more.

Support 💖

This project would not be possible without all of our fantastic contributors and sponsors. If you'd like to support the maintenance and upkeep of this project you can donate via GitHub Sponsors.

Sponsor the project for priortising feature requests / bugs relevant to you. (Depends on scope of ask and bandwidth of the contributors).

webauthor@ ben-gy bujjivadu

Help incentivise feature development, bug fixing by supporting the sponsorhip goals of this project. See list of sponsered feature requests here.

Also, huge thanks to following organizations for non monitery sponsorships

Credits

The decoder used for the QR code reading is from Zxing-js https://github.com/zxing-js/library