微信小程序canvas 生成二维码图片,画图片,生成图片,将两个canvas结合并保存图片

ops/2024/10/25 13:28:20/

**需求实现步骤如下

  1. 先定义两个canvas
  2. 一个canvas myQrcode画二维码的图片
  3. 另一个canvas mycanvas画一个背景图,并把二维码画到这个canvas上,mycanvas这个canvas生成一张图片,返回图片的临时路径
  4. 最后保存图片到手机**

首先wxml,新版小程序>微信小程序canvas要注意写 type=“2d” id=“XXX”

<canvas type="2d" style="width: 750rpx;height: 1260rpx;position:fixed;top: 1000px;left: 0px;z-index: 9999999999;" id="mycanvas"></canvas>
<canvas type="2d" style="width: 200rpx;height: 200rpx;position:fixed;top: 1000px;left: 0px;z-index: 9999999999;" id="myQrcode"></canvas>

画图

在这里插入图片描述

安装weapp-qrcode-canvas-2d

npm install weapp-qrcode-canvas-2d --save

weapp-qrcode-canvas-2d github
inviteBg是返回的微信临时图片地址

import {loadImg
} from '../../utils/drawPoster'
Page({onLoad(){const ewmLink = getApp().api.requestBase + '/index?user_id=' + userInfo.userId;const img = 'https://jingdong-sauce.oss-cn-beijing.aliyuncs.com/images/b49632186701d80f507a0b0930a34435.jpg'loadImg('#mycanvas', '#myQrcode', ewmLink, img, this, (base64) => {console.log(base64)this.setData({inviteBg: base64,saveImgShow: true})})}
})

先通过wx.createSelectorQuery(),查询到两个canvas
然后画二维码图片drawCode,并生成二维码临时图片canvasToTempFilePath
canvas1.createImage() ctx.drawImage(img, 0, 0, canvas1.width / 3, canvas1.height / 3); 将背景图片画到canvas1上
canvas1.createImage() ctx.drawImage(img2, canvas1.width / 6 - 70, 340, 140, 140) 将二维码图片画到canvas1上
wx.canvasToTempFilePath 生成canvas1背景图和二维码图片结合的临时图片并返回
drawPoster.js

import drawQrcode from 'weapp-qrcode-canvas-2d'
// 假设你已经引入了 qrcode.js
export function loadImg(canvasId1, canvasId2, ewmLink, imgUrl, context, callback) {const query = wx.createSelectorQuery()query.select(canvasId1).fields({node: true,size: true})query.select(canvasId2).fields({node: true,size: true})query.exec(async (res) => {console.log(res)const canvas1 = res[0].nodeconst canvas2 = res[1].node// 调用方法drawQrcode生成二维码let img2Src = await drawCode(canvas2, canvasId2, ewmLink)console.log('img2Src', img2Src)const ctx = canvas1.getContext('2d')const dpr = wx.getSystemInfoSync().pixelRatiocanvas1.width = res[0].width * dprcanvas1.height = res[0].height * dprctx.scale(dpr, dpr)const img = canvas1.createImage()img.src = imgUrlimg.onload = () => {ctx.drawImage(img, 0, 0, canvas1.width / 3, canvas1.height / 3);console.log(ctx)const img2 = canvas1.createImage()img2.src = img2Srcimg2.onload = () => {ctx.drawImage(img2, canvas1.width / 6 - 70, 340, 140, 140)wx.canvasToTempFilePath({canvasId: canvasId1,canvas: canvas1,x: 0,y: 0,width: 414,height: 695,destWidth: 414,destHeight: 695,success(res) {console.log('合成图片路径', res.tempFilePath)callback(res.tempFilePath)},fail(res) {console.error(res)}})}}})}function drawCode(canvas, canvasId, ewmLink) {return new Promise(async (resolve, reject) => {await drawQrcode({canvas: canvas,canvasId: canvasId,width: 200,padding: 30,background: '#ffffff',foreground: '#000000',text: ewmLink,})wx.canvasToTempFilePath({canvasId: canvasId,canvas: canvas,x: 0,y: 0,width: 260,height: 260,destWidth: 260,destHeight: 260,success(res) {resolve(res.tempFilePath)console.log('二维码临时路径:', res.tempFilePath)},fail(res) {console.error(res)}})})}

保存图片

在这里插入图片描述
点击保存图片,至此就能把这个图片保存到手机里啦

    const query = wx.createSelectorQuery()query.select('#mycanvas').fields({node: true,size: true}).exec(res => {wx.canvasToTempFilePath({canvas: res[0].node,success: function (res) {console.log("uuuu22222")wx.saveImageToPhotosAlbum({filePath: res.tempFilePath,success: function () {console.log("33333")wx.showToast({title: '保存成功',})}})},fail: function (res) {console.log("444444")console.log(res);}}, _this);})

http://www.ppmy.cn/ops/128346.html

相关文章

《IDE 巧用法宝:使用技巧全解析与优质插件推荐》

在日常撸代码的时候&#xff0c;相信兄弟们在IDEA 中用到不少插件&#xff0c;利用插件&#xff0c;不仅可以提高工具效率&#xff0c;撸起代码来&#xff0c;也格外的娃哈哈…… 一、IntelliJ IDEA 作为一个资深 Java 程序员&#xff0c;除了 IDEA 中默认的插件&#xff0c;我…

如何训练 RAG 模型

训练 RAG&#xff08;Retrieval-Augmented Generation&#xff09;模型涉及多个步骤&#xff0c;包括准备数据、构建知识库、配置检索器和生成模型&#xff0c;以及进行训练。以下是一个详细的步骤指南&#xff0c;帮助你训练 RAG 模型。 1. 安装必要的库 确保你已经安装了必…

【纯血鸿蒙】鸿蒙专项测试

一、初步了解 随着信息技术的高速发展,移动应用与人们生活日益紧密,面向各类场景的应用层出不穷,什么样的应用更受用户青睐呢?在满足用户功能需求之上,一个好的应用要能运行稳定、流畅不卡顿、占用内存小、安全等级高,此外,最好还能提供更多创新便捷的附加能力。 为了匹…

什么是KKT 条件(Karush-Kuhn-Tucker 条件)

KKT 条件&#xff08;Karush-Kuhn-Tucker 条件&#xff09;是优化理论中的一组必要条件&#xff0c;适用于求解带有等式和不等式约束的非线性规划问题。当目标函数和约束条件是凸的时&#xff0c;KKT 条件也是找到最优解的充分条件。在支持向量机&#xff08;SVM&#xff09;的…

Vue前端,使用echarts图表库

文章目录 1. ECharts官网地址2. echarts安装方式3. 使用柱状图&#xff0c;饼图&#xff0c;折线图4. 运行效果图 1. ECharts官网地址 https://echarts.apache.org/handbook/zh/get-started/ 2. echarts安装方式 npm install echarts温馨提示&#xff1a;使用npm命令的前提&…

【Ubuntu20.04 Visual Studio Code安装】【VSCODE】

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、打开VSCOE官网二、下载VSODE的Ubuntu版本三、安装VSCODE软件包四、导入工作空间(添加工作空间目录)五、安装插件&#xff1a;1.安装简体中文包2.安装ros插件…

力扣_泰波那契数

本题目本质和爬楼梯是一样的&#xff0c;主要运用的是递归来解题。 class Solution:my_dict {}def tribonacci(self, n: int) -> int:if self.my_dict.get(n) is not None: # 先判断有没有计算过这个值return self.my_dict.get(n)tempResult 0if n > 3:tempResult s…

ASP.NET MVC-font awesome-localhost可用IIS不可用

环境&#xff1a; win10, .NET 6.0&#xff0c;IIS 问题描述 本地IIS正常显示&#xff0c;但放到远程服务器上&#xff0c;每个icon都显示?。同时浏览器的控制台报错&#xff1a; fontawesome-webfont.woff2:1 Failed to load resource: the server responded with a statu…