微信小程序如何实现虎年春节头像制作
发表于:2024-10-09 作者:千家信息网编辑
千家信息网最后更新 2024年10月09日,这篇文章主要介绍微信小程序如何实现虎年春节头像制作,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!先上最终效果图:说下实现流程第一步:先获取到当前微信的头像图片,主要代码如下,注
千家信息网最后更新 2024年10月09日微信小程序如何实现虎年春节头像制作
这篇文章主要介绍微信小程序如何实现虎年春节头像制作,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
先上最终效果图:
说下实现流程
第一步:先获取到当前微信的头像图片,主要代码如下,注意默认获取到的头像图片不是高清的,需要先转换成高清图片,避免生成之后很模糊。
getUserProfile(e) { console.log(e) let that = this; wx.getUserProfile({ desc: '仅用于生成头像使用', success: (res) => { var url = res.userInfo.avatarUrl; while (!isNaN(parseInt(url.substring(url.length - 1, url.length)))) { url = url.substring(0, url.length - 1) } url = url.substring(0, url.length - 1) + "/0"; res.userInfo.avatarUrl = url; console.log(JSON.stringify(res.userInfo)); that.setData({ userInfo: res.userInfo, hasUserInfo: true }) that.drawImg(); } }); },
第二步:合成头像,把素材图片和第一步获取到的头像图片,获取到本地文件,然后利用小程序的cavas组件进行合成。
drawImg() { let that = this; wx.showLoading({ title: '生成头像中...', }) let promise1 = new Promise(function (resolve, reject) { wx.getImageInfo({ src: that.data.userInfo.avatarUrl, success: function (res) { resolve(res); } }) }); var mask_id = that.data.now_mask; let promise2 = new Promise(function (resolve, reject) { wx.getImageInfo({ src: `../../assets/img/mask0${mask_id}.png`, success: function (res) { console.log(res) resolve(res); } }) }); Promise.all([ promise1, promise2 ]).then(res => { console.log(res) var windowWidth = wx.getSystemInfoSync().windowWidth var context = wx.createCanvasContext('myAvatar'); var size = windowWidth /750 * 500 // var size = 500 context.drawImage(res[0].path, 0, 0, size, size); context.draw(true) context.save(); context.drawImage('../../'+res[1].path, 0, 0, size, size); context.draw(true) context.save(); }) wx.hideLoading() },
第三步:下载合成的图片到本地相册。
canvasToTempFile(){ if(!this.data.userInfo){ wx.showModal({ title: '温馨提示', content: '请先点击上方获取微信头像', showCancel: false, }) return } var windowWidth = wx.getSystemInfoSync().windowWidth var size = 500 // var dpr = 750 / windowWidth wx.canvasToTempFilePath({ x: 0, y: 0, height: size, width: size, canvasId: 'myAvatar', success: (res) => { wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: result => { wx.hideLoading(); wx.showModal({ content: '图片已保存到相册,请前往微信去设置哟!', showCancel: false, success: function(res) { if (res.confirm) { console.log('用户点击确定'); } } }) }, fail(e) { wx.hideLoading(); console.log("err:" + e); } }) } }); },
这样就实现了主要的功能了。
以上是"微信小程序如何实现虎年春节头像制作"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
头像
图片
程序
生成
制作
内容
相册
篇文章
高清
温馨
代码
价值
兴趣
功能
小伙
小伙伴
效果
效果图
文件
更多
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
加强网络安全建设有用吗
滕州天气预报软件开发
h5连接怎么更换服务器
保定长城软件开发薪资
三维力控数据库组态
潜伏期间网络安全问题
国外招软件开发吗
美剧关于网络安全的
http怎么创建服务器
mysql更新不了数据库
杭州豌豆尖尖网络技术有限公司
南京大聪明网络技术有限公司
建多个数据库
剑网3服务器没有响应
计算机网络技术需要什么要求
游戏服务器高负载怎么办
亿铭互联网科技有限公司
中海油网络安全外包
在sql中怎么还原数据库
黄岛区ios软件开发公司
数据库检查工具
sw无人管理服务器
运动手环数据库
铁路供电网络安全防护装置厂家
广州新华互联网科技学校校服
比较成熟的软件开发方式是
爆布式软件开发
华为服务器出售河南国资委
c语言怎么调用vf数据库
1年级最简单网络安全绘画