当前位置: 首页 > java >正文

微信小程序 自定义图片分享-绘制数据图片以及信息文字

一 、需求

从数据库中读取头像,姓名电话等信息,当分享给女朋友时,每个信息不一样

二、实现方案

1、先将数据库中需要的头像姓名信息读取出来加载到data 数据项中

data:{firstName:'', // 姓名img:'', // 头像shareImage:'',// 存储临时图片
}

2、当进入页面时,就产生图片,分享时直接分享 在小程序onLoad函数中实现

    // 生成分享图片generateShareImage() {// 进入页面就下载头像到临时地址中const imgUrl = app.globalData.base_url +'uploads/lawyer/'+this.data.imgwx.downloadFile({url: imgUrl,success: (res) => {if (res.statusCode === 200) {// 下载成功,获取临时路径const tempFilePath = res.tempFilePath;console.log("666"+tempFilePath)// 开始绘制 注意这里this.drawPoster(tempFilePath);}},fail: (err) => {console.error('图片下载失败:', err);}});},// 绘制海报drawPoster(avatarPath){// createCanvasContext 绘制方法const ctx = wx.createCanvasContext('shareCanvas');// 绘制背景图(可以是本地或网络路径)const bgImagePath = '/images/sharebg.jpg'; // 替换为你的背景图地址// 1. 绘制背景图ctx.drawImage(bgImagePath, 0, 0, this.data.canvasWidth, this.data.canvasHeight);// 2. 设置字体样式ctx.setFontSize(10);ctx.setFillStyle('black');// 3. 绘制姓名头像if(avatarPath){ctx.save()ctx.arc(40, 30, 11 * 2, 0, 2 * Math.PI) // 圆形边框//ctx.strokeStyle = '#1A1A1A' // 设置绘制圆形边框的颜色ctx.stroke() // 绘制出圆形,默认为黑色,可通过 ctx.strokeStyle = '#FFFFFF',设置想要的颜色ctx.clip()ctx.drawImage(avatarPath,  8, 6, 70, 70) // 图像大小ctx.restore()}// ctx.fillText(`${this.data.firstName}`, 100, 50);drawMultiLineText(ctx,`${this.data.firstName}`,80,28,50, 14, 14);drawMultiLineText(ctx,`${this.data.title}`,80,45,50, 14, 10);// ctx.fillText(`${this.data.mobilePhoneNumber}`, 10, 90);drawMultiLineText(ctx,`${this.data.mobilePhoneNumber}`,35,70,100, 14, 10);// ctx.fillText(`${this.data.email}`,10,110);drawMultiLineText(ctx,`${this.data.email}`,35,90,110, 14, 10);drawMultiLineText(ctx,`${this.data.address_details}`,35,109,90, 16, 10);// 5. 绘制完成ctx.draw(false, () => {// 6. 将 Canvas 导出为临时图片路径wx.canvasToTempFilePath({canvasId: 'shareCanvas',success: (res) => {// res.tempFilePath 是生成的临时图片路径console.log(this.data.mobilePhoneNumber)this.setData({shareImage: res.tempFilePath});// 触发分享this.onShareAppMessage();},fail: (err) => {wx.showToast({ title: '生成图片失败', icon: 'none' });}});});},
onLoad(options){// 调用绘制方法this.generateShareImage()
}

3、分享图片

onShareAppMessage 触发分享

 onShareAppMessage() {return {title:'标题',//标题path: '/pages/index/index',//路径imageUrl: this.data.shareImage//图片}}

http://www.xdnf.cn/news/5815.html

相关文章:

  • React系列——HOC高阶组件的封装与使用
  • 使用ZYNQ芯片和LVGL框架实现用户高刷新UI设计系列教程(第十二讲)
  • (2)python开发经验
  • 下周,Coinbase将被纳入标普500指数
  • windows c++ (9) 程序内注册服务并修改登录账户
  • 使用 `aiohttp` 构建高效的异步网络爬虫系统
  • 一次讲清 FP32 / FP16 / BF16 / INT8 / INT4
  • VR和眼动控制集群机器人的方法
  • 青少年编程与数学 02-019 Rust 编程基础 10课题、函数、闭包和迭代器
  • 机器学习中分类模型的常用评价指标
  • 设计模式系列(03):设计原则(二):DIP、ISP、LoD
  • SpringBoot与Eventuate Tram整合 - 实现转账最终一致性系统
  • 解锁生命周期评价密码:OpenLCA、GREET 与 R 语言的融合应用
  • 基于 Amazon Bedrock 和 Amazon Connect 打造智能客服自助服务 – 设计篇
  • 【阿里云】阿里云 Ubuntu 服务器无法更新 systemd(Operation not permitted)的解决方法
  • Java Solon v3.3.0 发布(国产优秀应用开发基座)
  • Spring Boot Swagger 安全防护全解析:从旧版实践到官方规范
  • Spring Boot 跨域问题全解:原理、解决方案与最佳实践
  • Tomcat和Nginx的主要区别
  • 【MySQL】第三弹——表的CRUD进阶(一)数据库约束
  • 地址簿模块-01.需求分析
  • D-Pointer(Pimpl)设计模式(指向实现的指针)
  • 在VSCode中接入DeepSeek的指南
  • 【时时三省】(C语言基础)使用字符串处理函数
  • 基于Spring Boot+Layui构建企业级电子招投标系统实战指南
  • 人脸识别系统中的隐私与数据权利保障
  • ‌OPE.AI开放平台:一站式企业AI应用引擎
  • 前端学习(2)—— CSS详解与使用
  • centos7.x下,使用宝塔进行主从复制的原理和实践
  • 博客系统技术需求文档(基于 Flask)