当前位置:网站首页>Generate QR code in wechat applet
Generate QR code in wechat applet
2022-06-28 04:49:00 【Low code preacher】
Usually in wechat applet , Generating QR code is a very common scenario , But check the official documents , There is no ready-made component to use .
After searching , The generation of two-dimensional code components generally requires the help of a third-party library . The implementation steps are divided into :
- Download third-party libraries
- Install third party libraries
- wxml Generate two-dimensional code in
- js Introduction in
Download third-party libraries
There is a ready-made Library , Can generate QR code . I'm usually used to it gitee download . Enter the download address in the browser
https://gitee.com/WeiDoctor/weapp-qrcode-canvas-2d
The download method is to click the clone button 
After downloading, unzip ,dist The files in the directory are what we need 
Install third party libraries
The library installation method is also relatively simple , We can now create a new one under the applet directory utils Folder 
take dist In the catalog js Copy it all to utils Under the table of contents 
This completes the installation of the library
wxml Generate two-dimensional code in
QR code usage canvas Component generation , So you need to be in wxml Add... To the file canvas Components
<canvas type="2d" style="width: 260px; height: 260px;" id="myQrcode"></canvas>

js Third party libraries are introduced into
If you want to use a third-party library, you need to use import Sentence introduction , The specific code is as follows
// pages/test/index.js
import drawQrcode from '../../utils/weapp.qrcode.esm.js'
Page({
/** * Initial data of the page */
data: {
},
/** * Life cycle function -- Monitor page loading */
onLoad(options) {
const query = wx.createSelectorQuery()
query.select('#myQrcode')
.fields({
node: true,
size: true
})
.exec((res) => {
var canvas = res[0].node
// Calling method drawQrcode Generate qr code
drawQrcode({
canvas: canvas,
canvasId: 'myQrcode',
width: 260,
padding: 30,
background: '#ffffff',
foreground: '#000000',
text: ' Personal QR code information ',
})
// Get temporary path ( After you get it , Do whatever you want )
wx.canvasToTempFilePath({
canvasId: 'myQrcode',
canvas: canvas,
x: 0,
y: 0,
width: 260,
height: 260,
destWidth: 260,
destHeight: 260,
success(res) {
console.log(' QR code temporary path :', res.tempFilePath)
},
fail(res) {
console.error(res)
}
})
})
},
/** * Life cycle function -- Listening page first rendering completed */
onReady() {
},
/** * Life cycle function -- Monitor page display */
onShow() {
},
/** * Life cycle function -- Monitor page hidden */
onHide() {
},
/** * Life cycle function -- Monitor page uninstall */
onUnload() {
},
/** * Page related event handler -- Monitor user pull-down action */
onPullDownRefresh() {
},
/** * Handling function of page pull bottom event */
onReachBottom() {
},
/** * Users click the upper right corner to share */
onShareAppMessage() {
}
})
We just need to modify it text The value in the attribute can
summary
This article introduces the method of generating two-dimensional code for small programs , With the help of the third-party library, the development speed can be greatly improved .
边栏推荐
猜你喜欢
随机推荐
E-week finance Q1 mobile banking has 650million active users; Layout of financial subsidiaries in emerging fields
TACo:一种关于文字识别的数据增强技术
2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠,即两个区间的左右端点不可以完全一样。
Short video platform development, click links and pictures to automatically jump to a new page
[matlab traffic light identification] traffic light identification [including GUI source code 1908]
Idle interrupt cannot be cleared
C语言全局变量(c文件和h文件中的全局变量、静态全局变量)使用注意事项
Why are cloud vendors targeting this KPI?
Lazy loading and preloading of pictures
Lamaba expression learning and common functional interfaces
Annual comprehensive analysis of China's audio market in 2022
Function and working principle of controller
10:00面试,10:02就出来了 ,问的实在是太...
玩转双指针
[CSP-J2020] 优秀的拆分
Moonbeam integrates coin98, giving users more choices on the multi chain road
Win10 how to delete the large file hiberfil sys
Ppt production tips
Mask's miserable and inspirational childhood, who is introverted by campus violence
华为9年经验的软件测试总监工作感悟—写给还在迷茫的朋友






![[csp-j2020] excellent splitting](/img/05/90f9cf71791b3cdc37073eaf5db989.png)
![[NOIP2002 普及组] 过河卒](/img/6c/31fa210e08c7fd07691a1c5320154e.png)
