Home > WeChat Applet > Mini Program Development > How to generate QR code with mini program

How to generate QR code with mini program

coldplay.xixi
Release: 2020-08-14 14:30:53
Original
8927 people have browsed it

How to generate a QR code from a small program: First, create a drawn canvas in the wxml file; then copy [weapp.qrcode.min.js] to the project; finally call [drawQrcode()] to draw QR code.

How to generate QR code with mini program

How to generate QR code by mini program:

1. Create canvas tag

First, create the canvas for drawing in the wxml file, and define width, height, and canvasId. Since the mini program does not have an API for dynamically creating labels, this step cannot be omitted.

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

2. Call the drawing method

Since the WeChat applet does not support the introduction of NPM packages, you can copy weapp.qrcode.min.js in the dist directory to the project.

If your applet uses a framework that supports the introduction of NPM packages, such as wepy, you can also install the weapp-qrcode NPM package directly.

npm install weapp-qrcode --save
Copy after login

After introducing the js file, call drawQrcode() to draw the QR code.

import drawQrcode from &#39;weapp-qrcode&#39;
// 或者,将 dist 目录下,weapp.qrcode.min.js 复制到项目目录中
// import drawQrcode from &#39;../../utils/weapp.qrcode.min.js&#39;
drawQrcode({
  width: 200,
  height: 200,
  canvasId: &#39;myQrcode&#39;,
  text: &#39;https://github.com/yingye&#39;
}
Copy after login

API Description

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

Related learning recommendations: WeChat Mini Program Development Tutorial

The above is the detailed content of How to generate QR code with mini program. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template