QRCode.js:使用 JavaScript 生成二维码

一、安装

npm install qrcodejs2 --save

二、导入项目

var QRCode = require('qrcode2');
// 或者
import QRCode from 'qrcode2';

三 、实例化QRCode对象

//qrcode是ref对象或者是为id的dom元素
new QRCode(document.getElementById('qrcode'), 'http://www.baidu.com');

// 或者配置一些选项
var qrcode = new QRCode("qrcode", {
    text: "http://www.baidu.com",
    width: 128,
    height: 128,
    colorDark : "#000",
    colorLight : "#fff",
    correctLevel: QRCode.CorrectLevel.L //容错率,L/M/H
});
qrcode.clear(); // 清除代码
qrcode.makeCode("http://www.w3cschool.cc"); // 生成另外一个二维码

原文地址:https://www.npmjs.com/package/qrcodejs2
qrcode另外一个版本

1、安装

npm install --save qrcode

2、使用

在javascript中

<!-- 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!');
})

在ES6/ES7中使用

import QRCode from 'qrcode' 
// import引入可能会导致报错,可以尝试require
// const QRCode = require('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)
  }
}

在这里插入图片描述

原文地址:https://www.npmjs.com/package/qrcode
api文档:https://www.npmjs.com/package/qrcode#api

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C+ 安口木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值