带你开发一个虎年春节头像生成小程序

 4163

本篇文章带大家跟风做一个虎年春节头像制作小程序,获取微信头像,选择头像边框,即可合成不同的虎年春节头像,希望对大家有所帮助!

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

先上最终效果图:


带你开发一个虎年春节头像生成小程序


实现流程

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

  1. getUserProfile(e) {
  2.     console.log(e)
  3.     let that = this;
  4.     wx.getUserProfile({
  5.       desc: '仅用于生成头像使用',
  6.       success: (res) => {
  7.         var url = res.userInfo.avatarUrl;
  8.         while (!isNaN(parseInt(url.substring(url.length - 1, url.length)))) {
  9.           url = url.substring(0, url.length - 1)
  10.         }
  11.         url = url.substring(0, url.length - 1) + "/0";
  12.         res.userInfo.avatarUrl = url;
  13.         console.log(JSON.stringify(res.userInfo));
  14.         that.setData({
  15.           userInfo: res.userInfo,
  16.           hasUserInfo: true
  17.         })
  18.         that.drawImg();
  19.       }
  20.     });
  21. },

第二步:合成头像,把素材图片和第一步获取到的头像图片,获取到本地文件,然后利用小程序的cavas组件进行合成。

  1. drawImg() {
  2.     let that = this;
  3.     wx.showLoading({
  4.       title: '生成头像中...',
  5.     })
  6.     let promise1 = new Promise(function (resolve, reject) {
  7.       wx.getImageInfo({
  8.         src: that.data.userInfo.avatarUrl,
  9.         success: function (res) {
  10.           resolve(res);
  11.         }
  12.       })
  13.     });
  14.     var mask_id = that.data.now_mask;
  15.     let promise2 = new Promise(function (resolve, reject) {
  16.       wx.getImageInfo({
  17.         src: `../../assets/img/mask0${mask_id}.png`,
  18.         success: function (res) {
  19.           console.log(res)
  20.           resolve(res);
  21.         }
  22.       })
  23.     });
  24.     Promise.all([
  25.       promise1, promise2
  26.     ]).then(res => {
  27.       console.log(res)
  28.       var windowWidth = wx.getSystemInfoSync().windowWidth
  29.       var context = wx.createCanvasContext('myAvatar');
  30.       var size = windowWidth /750 * 500
  31.       // var size = 500
  32.       context.drawImage(res[0].path, 0, 0, size, size);
  33.       context.draw(true)
  34.       context.save();
  35.       context.drawImage('../../'+res[1].path, 0, 0, size, size);
  36.       context.draw(true)
  37.       context.save();
  38.     })
  39.     wx.hideLoading()
  40. },

第三步:下载合成的图片到本地相册。

  1. canvasToTempFile(){
  2.     if(!this.data.userInfo){
  3.       wx.showModal({
  4.         title: '温馨提示',
  5.         content: '请先点击上方获取微信头像',
  6.         showCancel: false,
  7.       })
  8.       return
  9.     }
  10.     var windowWidth = wx.getSystemInfoSync().windowWidth
  11.     var size = 500
  12.     // var dpr = 750 / windowWidth
  13.     wx.canvasToTempFilePath({
  14.       x: 0,
  15.       y: 0,
  16.       height: size,
  17.       width: size,
  18.       canvasId: 'myAvatar',
  19.       success: (res) => {
  20.         wx.saveImageToPhotosAlbum({
  21.           filePath: res.tempFilePath,
  22.           success: result => {
  23.             wx.hideLoading();
  24.             wx.showModal({
  25.               content: '图片已保存到相册,请前往微信去设置哟!',
  26.               showCancel: false,
  27.               success: function(res) {
  28.                 if (res.confirm) {
  29.                   console.log('用户点击确定');
  30.                 }
  31.               }
  32.             })
  33.           }, fail(e) {
  34.             wx.hideLoading();
  35.             console.log("err:" + e);
  36.           }
  37.         })
  38.       }
  39.     });
  40. },

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

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


原文地址:https://juejin.cn/post/7057807283463389191

作者:hackun

本文网址:https://www.zztuku.com/detail-10885.html
站长图库 - 带你开发一个虎年春节头像生成小程序
申明:本文转载于《掘金社区》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐