微信小程序显示二维码?

wxml

html 复制代码
 <canvas style="width: 100%;height: 100%;margin-left: 20%;" id="Canvase" type="2d"></canvas>

js

javascript 复制代码
// pages/code/code.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
      wx.request({
        url: '接口数据', //仅为示例,并非真实的接口地址
        data: {
          userid: 36,
          scienceid: 1,
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          console.log(res);

          const fs = wx.getFileSystemManager();
          var codeimg = wx.env.USER_DATA_PATH + '/' + '.jpg';
          fs.writeFile({
            filePath: codeimg,
            data: res.data.slice(22), // code就是接口返回的base64数据(分割掉前面的data:image/png;base64,)
            encoding: 'base64',
            success: () => {
              // console.log(codeimg);
              wx.createSelectorQuery().select('#Canvase').fields({
                  node: true,
                  size: true
                })
                .exec((res) => {
                  console.log(res);
                  let ctx = res[0].node.getContext('2d'); //getContext返回Canvas 的绘图上下文              
                  let canvas = res[0].node;
                  const bg = canvas.createImage();
                  //背景图片
                  const image = canvas.createImage();
                  // 图片高清化
                  const dpr = wx.getSystemInfoSync().pixelRatio;
                  res[0].node.width = res[0].node.width * dpr;
                  res[0].node.height = res[0].node.height * dpr;
                  // 设置背景图片src
                  image.src = 'https://pic.imgdb.cn/item/647d31011ddac507cc160e75.jpg'
                  bg.src = codeimg;
                  image.onload = function () {
                    ctx.drawImage(image, 0, 0, 289, 370)
                    ctx.drawImage(bg, 76, 175, 140, 140);

                  }
                  // 将图片保存需要的实例,不写保存可以不需要
                  that.setData({
                    ctx: canvas
                  })  
                })



            }
          })

        }
      })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
相关推荐
ON.LIN2 小时前
云边去水印小程序详细搭建教程(附源码)
小程序
2501_915909065 小时前
iOS test 测试怎么做?功能、性能、兼容、稳定与安全五类测试指南
android·ios·小程序·https·uni-app·iphone·webview
Ai-_Man10 小时前
从AI公式到可编辑文档:MathType格式转换技术分析教程
人工智能·ai·小程序
show43318 小时前
2026视频处理小程序技术选型指南:链接解析+OCR+ASR+AI配音多引擎对比
小程序·ocr·音视频
凡科网小帆1 天前
2026小程序商城平台哪家强,小程序商城平台选型怎么做
大数据·小程序·小程序商城平台
游戏开发爱好者81 天前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战
android·运维·ci/cd·小程序·uni-app·自动化·iphone
Greg_Zhong1 天前
微信小程序 + 腾讯云人体分析:从 0 到 1 实现 AI 抠图打卡合照(细节待更新~)
人工智能·微信小程序·腾讯云·ai抠图-ai打卡拍照
游戏开发爱好者81 天前
iOS 推送怎么配置,APNs 推送证书、设备库与群发
android·ios·小程序·https·uni-app·iphone·webview
snow@li1 天前
小程序:200知识点/速查手册
小程序
不吃辣4902 天前
vibe coding | 如何做一个AI制图小程序?
人工智能·小程序·ai编程