欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序實現(xiàn)虎年春節(jié)頭像制作

 更新時間:2022年02月01日 10:32:28   作者:hackun  
春節(jié)來臨之際,看到有網(wǎng)友分享了網(wǎng)頁版的虎年頭像制作工具。本文將為大家介紹一個虎年春節(jié)頭像制作小程序,文中的示例代碼講解詳細,需要的可以參考一下

馬上就到春節(jié)了,今天看到有網(wǎng)友分享了網(wǎng)頁版的虎年頭像制作工具,感覺很不錯,正好打算做個小程序練手沒啥主題,那就用這個試試吧。

先上最終效果圖:

說下實現(xiàn)流程

第一步:先獲取到當前微信的頭像圖片,主要代碼如下,注意默認獲取到的頭像圖片不是高清的,需要先轉換成高清圖片,避免生成之后很模糊。

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

這樣就實現(xiàn)了主要的功能了。

最后放上小程序碼,歡迎大家掃碼體驗一下:

最后 當前項目已開源:hackun666/new-year-face: wechat new year face maker (github.com)

祝大家春節(jié)快樂,虎年大吉!

到此這篇關于微信小程序實現(xiàn)虎年春節(jié)頭像制作的文章就介紹到這了,更多相關微信小程序內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論