微信小程序实现虎年春节头像制作

作者:hackun 时间:2024-04-16 08:47:47 

马上就到春节了,今天看到有网友分享了网页版的虎年头像制作工具,感觉很不错,正好打算做个小程序练手没啥主题,那就用这个试试吧。

先上最终效果图:

微信小程序实现虎年春节头像制作

说下实现流程

第一步:先获取到当前微信的头像图片,主要代码如下,注意默认获取到的头像图片不是高清的,需要先转换成高清图片,避免生成之后很模糊。

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);
         }
       })
     }
   });
 },

这样就实现了主要的功能了。

最后放上小程序码,欢迎大家扫码体验一下:

微信小程序实现虎年春节头像制作

最后 当前项目已开源:hackun666/new-year-face: wechat new year face maker (github.com)

祝大家春节快乐,虎年大吉!

来源:https://juejin.cn/post/7057807283463389191

标签:微信小程序,头像制作
0
投稿

猜你喜欢

  • 使用pipenv管理python虚拟环境的全过程

    2021-08-26 13:05:55
  • Python中实现输入一个整数的案例

    2022-05-28 18:42:21
  • sql 2005不允许进行远程连接可能会导致此失败的解决方法

    2024-01-25 17:34:59
  • 一文带你学会MySQL的select语句

    2024-01-16 01:07:30
  • IE下Flash内容刷新后消失问题

    2008-01-02 12:38:00
  • Python实现对字典分别按键(key)和值(value)进行排序的方法分析

    2021-06-05 00:36:32
  • 利用tkinter实现下拉框联动

    2022-02-12 14:55:27
  • 关于设计品质保证(DQA)的几点想法

    2007-11-16 16:55:00
  • 深入理解Python中装饰器的用法

    2022-04-25 18:59:31
  • Tensorflow分批量读取数据教程

    2023-04-15 20:02:05
  • 如何使用Python Matplotlib绘制条形图

    2023-09-21 04:41:46
  • lnmp下如何关闭Mysql日志保护磁盘空间

    2024-01-14 02:54:52
  • python实现图像边缘检测

    2022-03-17 15:35:11
  • python版本的仿windows计划任务工具

    2021-06-09 03:55:07
  • JavaScript简单实现的仿微博留言功能示例

    2024-04-17 10:01:33
  • Python 字符串操作详情

    2023-02-04 19:03:59
  • JavaScript判断前缀、后缀是否是空格的方法

    2024-04-22 22:38:04
  • Pytorch实现LSTM和GRU示例

    2022-02-08 09:14:44
  • 使用PDB简单调试Python程序简明指南

    2022-07-27 21:03:28
  • Python中移除List重复项的五种方法

    2021-12-11 20:15:38
  • asp之家 网络编程 m.aspxhome.com