【避坑指“难”】小程序Canvas绘制二维码,base64转图片

核心原理: 生成Base64形式的二维码,qrcode-base64

安装npm i qrcode-base64@1.0.0

代码实现

1、引入该库
const QR = require("qrcode-base64");
2、封装绘制二维码函数
  drawQRcode() { //绘制二维码
    var that = this    
    const imgData = QR.drawImg('http://xxx.xxx.xxx/二维码链接', {
      typeNumber: 4,
      errorCorrectLevel: 'M',
      size: 500,
      canvasId: 'oss-canvas'
    })
    const fs = wx.getFileSystemManager()
    const times = new Date().getTime()
    const codeimg = wx.env.USER_DATA_PATH + '/' + times + '.png' //保证图片路径的唯一性
    fs.writeFile({
      filePath: codeimg,  
      data: imgData.slice(22),
      encoding: 'base64',
      success: (res) => {
        that.setData({
          qrImage: codeimg //二维码图片
        })
      }
    });
  },

为什么用 FileSystemManager.writeFile

因为base64 格式图片数据,无法被 getImageInfo直接调用,首先将 base64 数据 —> ArrayBuffer 数据,然后通过使用FileSystemManager.writeFile 将 ArrayBuffer 数据—>本地用户路径的二进制图片文件,此时该二进制图片文件存储在wx.env.USER_DATA_PATH 中,Canvas可以直接根据该路径绘制出二维码图片。

3、Canvas 绘制二维码图片
getCanvas(){
	var that = this
	wx.showLoading({
      title: '图片处理中',
      mask: true,
      icon: 'loading',
      duration: 1000
    })
    that.canvas = wx.createCanvasContext("image-canvas", that)
    that.canvas.drawImage(that.data.qrImage, that.data.width , that.data.height , 38, 38) 
    setTimeout(function () { //导出图片
      wx.canvasToTempFilePath({
        canvasId: "image-canvas",
        x: that.data.width,
        y: that.data.height,
        width: that.data.width,
        height: that.data.height,
        destWidth: that.data.width * that.data.pixelRatio,
        destHeight:  that.data.height * that.data.pixelRatio,
        fileType: 'png',
        quality: 1,
        success: function (res) {
          that.setData({
            localImage: res.tempFilePath,
          })
          wx.hideLoading()
        }
      })
    }, 250)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用第三方库 `qrcode`,该库可以在后端生成二维码图片,然后将图片换为 base64 编码的字符串,再将字符串返回给前端,前端可以使用 `<canvas>` 元素将 base64 编码的字符串换为图片并显示在页面上。 以下是一个使用 `qrcode` 库在 Node.js 中生成二维码图片并返回 base64 编码字符串的示例代码: ```javascript const qrcode = require('qrcode'); const canvas = require('canvas'); // 生成二维码图片 qrcode.toCanvas('https://example.com', { errorCorrectionLevel: 'H' }, function (err, qrCanvas) { if (err) throw err; // 将 canvas 换为 base64 编码的字符串 const qrImage = qrCanvas.toDataURL(); // 返回 base64 编码字符串 res.send(qrImage); }); ``` 在前端使用 `<canvas>` 元素将 base64 编码的字符串换为图片并显示: ```html <canvas id="qr-canvas"></canvas> <script> const canvas = document.getElementById('qr-canvas'); const ctx = canvas.getContext('2d'); const img = new Image(); img.onload = function () { ctx.drawImage(img, 0, 0); }; img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAMAAAC...'; </script> ``` 在 Taro 开发小程序中,可以在页面中使用 `<canvas>` 元素,并在 `componentDidMount()` 生命周期函数中获取 canvas 上下文对象,然后从后端获取 base64 编码的二维码图片,并用 `drawImage()` 方法将图片绘制canvas 上。示例代码如下: ```jsx import Taro, { Component } from '@tarojs/taro'; import { Canvas } from '@tarojs/components'; class Qrcode extends Component { componentDidMount() { const ctx = Taro.createCanvasContext('qr-canvas', this.$scope); Taro.request({ url: 'https://example.com/qrcode', success: res => { const img = new Image(); img.onload = function () { ctx.drawImage(img, 0, 0); }; img.src = res.data; } }); } render() { return ( <Canvas id="qr-canvas" style={{ width: '300px', height: '300px' }}></Canvas> ); } } ``` 在上面的代码中,我们使用 `<Canvas>` 组件创建了一个 300 x 300 像素的 `<canvas>` 元素,并在 `componentDidMount()` 生命周期函数中获取 canvas 上下文对象,然后使用 `Taro.request()` 方法从后端获取 base64 编码的二维码图片,并在图片加载完成后使用 `drawImage()` 方法将图片绘制canvas 上。注意,这里需要使用 `Image` 对象来加载图片,因为 `drawImage()` 方法只支持加载 `Image` 对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

椰卤工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值