Coder Social home page Coder Social logo

wxapp-qrcode's Introduction

微信小程序-二维码生成器

本项目用于在微信小程序中生成二维码,也可用于第三方框架Mpvue,Taro等。

生成预览

jsh5css.cn

  • 可支持输入中文文本

安装

git clone https://github.com/demi520/wxapp-qrcode.git

使用

1.创建canvas节点,以及设置canvas-id。(可以控制该区域不显示,但是必须要存在)

<canvas style="width: 686rpx;height: 686rpx;background:#f1f1f1;" canvas-id="mycanvas"/>

2.引入qrcode.js,将utils/qrcode.js 文件复制到自己工程里,并引入。

// 注意: 这里xxx是你自己的路径
let QR = require("xxxx/qrcode.js")  // require方式
import QR from 'xxxx/qrcode.js'    // es6的方式

3.在js文件中,定义相关的方法,要注意在data中创建imagePath(最终生成的图片路径),可以将img的src属性绑定imagePath

createQrCode: function (content, canvasId, cavW, cavH) {
  //调用插件中的draw方法,绘制二维码图片
  //this.canvasToTempImage 为绘制完成的回调函数,可根据自己的业务添加
  QR.api.draw(content, canvasId, cavW, cavH, this, this.canvasToTempImage);
},

//获取临时缓存图片路径,存入data中
canvasToTempImage: function (canvasId) {
  let that = this;
  wx.canvasToTempFilePath({
    canvasId,   // 这里canvasId即之前创建的canvas-id
    success: function (res) {
      let tempFilePath = res.tempFilePath;
      console.log(tempFilePath);
      that.setData({       // 如果采用mpvue,即 this.imagePath = tempFilePath
        imagePath:tempFilePath,
      });
    },
    fail: function (res) {
      console.log(res);
    }
  });
}

4.绑定事件,调用createQrCode,生成二维码

createQrCode ('wxapp-qrcode', 'mycanvas', 300, 300)

FAQ

自定义组件中不能生成qrcode?

封装方法时: 添加上this, QR.api.draw(url, canvasId, cavW, cavH, this); 可参考qrcode.js 768行,wx.createCanvasContext

如何适配不同屏幕大小的canvas?

可参考 pages/main/index.js 中的 setCanvasSize 方法。

Ps

感谢微信小程序|联盟 @amis提供的素材和创意;

测试有其他问题请回帖哦,感激!!

wxapp-qrcode's People

Contributors

wyuanyang avatar z-hnan avatar ovenslove avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.