黑白梦黑白梦

toggle navtoggle nav
  • 文章
  • 专栏
  • 文章
  • 专栏

qrcode 生成二维码

发布于 2021-10-13, 更新于 2023-04-08

常用的二维码生成库

https://github.com/soldair/node-qrcode

安装:

npm install --save qrcode

使用:

// index.js -> bundle.js
var QRCode = require("qrcode");
var canvas = document.getElementById("canvas");

QRCode.toCanvas(
  canvas,
  "http://bing.com/",
  {
    width: 122,
    margin: 0,
  },
  function (error) {
    if (error) console.error(error);
    console.log("success!");
  }
);

设置“错误更正级别”,越高二维码越复杂:

  • L (Low) ~7%
  • M (Medium) ~15% 默认值
  • Q (Quartile) ~25%
  • H (High) ~30%
QRCode.toDataURL(
  "some text",
  { errorCorrectionLevel: "H" },
  function (err, url) {
    console.log(url);
  }
);
本文收录于专栏

一些好用的 npm 前端开源库

收集一些好用的前端开源库,主要是 npm 包

©2015-2026 黑白梦 粤ICP备15018165号

联系: heibaimeng@foxmail.com