小程序中生成二维码weapp-qrcode.js


1.在小程序中引入weapp-qrcode.js到utils文件夹下:

2.在需要转化二维码的页面引入

const QRCode = require('../../utils/weapp-qrcode.js')
3.canvas的宽高需要自适应,那么需要进行尺寸转换
const W = wx.getSystemInfoSync().windowWidth;
const rate = 750.0 / W;

// 300rpx 在6s上为 150px
const qrcodeWidth = 300 / rate;

4.生成二维码

 getqrcode(item) {

    qrcode = new QRCode(`${item.couponUserId}`, {
      usingIn: this,
      image: '../../images/connection.png',
      width: qrcodeWidth,
      height: qrcodeWidth,
      colorLight: "white",
      correctLevel: QRCode.CorrectLevel.L
    });
    let _urls = config.previewHost
    qrcode.makeCode(`${_urls}/coupon-use?id=${item.couponUserId}`);
    
  },

其中`${item.couponUserId}`是wxml中canvas的id

text是需要转换为二维码的字符串

usingIn为可选参数,组件间中要使用,传this

image为默认生成二维码的图片

width和height分别是绘制出的二维码长宽,这里设置为跟canvas同样的长宽

colorDark和colorLight为二维码交替的两种颜色

correctLevel是二维码的纠错级别,默认为高级

若果你需要再次生成二维码可调用qrcode.makeCode('需要转换的地址')
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM