Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

微信小程序中生成二维码工具:weapp.qrcode.js #2

Open
yingye opened this issue May 3, 2018 · 0 comments
Open

微信小程序中生成二维码工具:weapp.qrcode.js #2

yingye opened this issue May 3, 2018 · 0 comments

Comments

@yingye
Copy link
Owner

yingye commented May 3, 2018

前言

在近期的小程序开发中,有一个离线生成二维码的需求。当时想到了一些优秀的前端开源库 jquery-qrcodenode-qrcode,由于小程序中没有DOM的概念,这些库在小程序中并不适用。

所以,针对微信小程序的特点,封装了 weapp.qrcode.js ,用于在小程序中快速生成二维码。效果如下图:

clipboard.png

下面来介绍一下使用方法:

使用

创建canvas标签

先在 wxml 文件中,创建绘制的 canvas,并定义好 width, height, canvasId 。由于小程序没有动态创建标签的api,所以这一步不能省略。

<canvas style="width: 200px; height: 200px;" canvas-id="myQrcode"></canvas>

调用绘制方法

由于微信小程序不支持引入NPM包,可以将dist目录下,weapp.qrcode.min.js 拷贝至项目中。

如果你的小程序使用了支持引入NPM包的框架,如 wepy , 也可以直接安装 weapp-qrcode NPM包。

npm install weapp-qrcode --save

引入 js 文件后,调用 drawQrcode() 绘制二维码。

import drawQrcode from 'weapp-qrcode'
// 或者,将 dist 目录下,weapp.qrcode.min.js 复制到项目目录中
// import drawQrcode from '../../utils/weapp.qrcode.min.js'

drawQrcode({
  width: 200,
  height: 200,
  canvasId: 'myQrcode',
  text: 'https://github.com/yingye'
}

API说明

参数 说明 示例
width 必须,二维码宽度,与canvaswidth保持一致 200
height 必须,二维码高度,与canvasheight保持一致 200
canvasId 必须,绘制的canvasId 'myQrcode'
text 必须,二维码内容 'https://github.com/yingye'
typeNumber 非必须,二维码的计算模式,默认值-1 8
correctLevel 非必须,二维码纠错级别,默认值为高级,取值:{ L: 1, M: 0, Q: 3, H: 2 } 1
background 非必须,二维码背景颜色,默认值白色 '#ffffff'
foreground 非必须,二维码前景色,默认值黑色 '#000000'

如果想更深入的了解二维码的原理,推荐大家阅读 二维码的生成细节和原理

原始文档

https://github.com/yingye/weapp-qrcode ,如果觉得还不错,记得给个star奥~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant