0
点赞
收藏
分享

微信扫一扫

使用小程序实现AI动漫脸特效

文章目录

一、文章前言

二、具体流程及准备

三、开发步骤

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

/**
 * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    let ApiKey='这里填你所申请的ApiKey';
    let SecretKey='这里填你所申请的SecretKey';
    wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + ApiKey+'&client_secret='+SecretKey,
      method: 'POST',
      success: function (res) {
        that.setData({
          AccessToken:res.data.access_token
        });
      }
    });
  },

在这里插入图片描述

在这里插入图片描述

参数是否必选类型说明
image和url二选一stringbase64编码后大小不超过10M
url和image二选一string图片完整URL,URL长度不超过1024字节
typestringanime或者anime_mask。前者生成二次元动漫图,后者生成戴口罩的二次元动漫人像
mask_idstring在type参数填入anime_mask时生效
{
	'image': '这里放经过base64转换后的图片'
}

在这里插入图片描述

<view class="containerBox">
  <view class="leftBtn" bindtap="loadImage">
  <image src="../../image/xj.png"  class="btnImg"></image>
  上传图像
  </view>
  <view class="rightBtn" bindtap="identify">
  <image src="../../image/face.png"  class="btnImg"></image>
  动漫转换
  </view>
</view>
  loadImage() {
    let that = this;
    wx.chooseImage({
      count: 0,
      sizeType: ['original', 'compressed'], //原图 / 压缩
      sourceType: ['album', 'camera'], //相册 / 相机拍照模式
      success(res) {
        that.setData({
          imgSrc: res.tempFilePaths[0]
        });
        //将图片转换为Base64格式
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0],
          encoding: 'base64',
          success(data) {
            let baseData = data.data; //'data:image/png;base64,' + data.data;
            that.setData({
              baseData: baseData
            });
          }
        });
      }
    })
  },
<image src="{{imgSrc}}" class="showImg"></image>
参数是否必选类型说明
imagestring图片信息(总数据大小应小于10M,图片尺寸在1920x1080以下)

在这里插入图片描述

字段类型说明
log_iduint64唯一的log id,用于问题定位
imagestring处理后图片的Base64编码

在这里插入图片描述

<view class='cu-load load-modal'  wx:if="{{loadding}}">
  <view class='gray-green'>转换中..</view>
</view>

四、完整代码

<!--index.wxml-->
<view class="containerBox">
  <view class="leftBtn" bindtap="loadImage">
  <image src="../../image/xj.png"  class="btnImg"></image>
  上传图像
  </view>
  <view class="rightBtn" bindtap="identify">
  <image src="../../image/face.png"  class="btnImg"></image>
  动漫转换
  </view>
</view>
<view style="display:flex;">
  <image src="{{reproduction}}" class="showImg"></image>
  <image src="data:image/png;base64,{{baseImg}}" class="showImg"></image>

</view>

<view class='cu-load load-modal'  wx:if="{{loadding}}">
  <view class='gray-green'>转换中..</view>
</view>

<!--index.wxss-->
/* pages/anime/index.wxss */
page{background:white;}
/* pages/pubu/index.wxss */
.containerBox{
  width:750rpx;
  display:flex;
  height:62rpx;
  margin-top:20rpx;
}
.leftBtn{
  display: flex;
  width:181rpx;
  height:62rpx;
  color:white;
  border:1rpx solid #4FAFF2;
  background:#4FAFF2;
  border-radius:10rpx;
  text-align: center;
  line-height:62rpx;
  font-size:28rpx;
  margin-left: 108rpx;
}
.rightBtn{
  display: flex;
  width:181rpx;
  height:62rpx;
  color:white;
  border:1rpx solid #4FAFF2;
  border-radius:10rpx;
  text-align: center;
  line-height:62rpx;
  font-size:28rpx;
  margin-left: 172rpx;
  background:#4FAFF2;
}
.btnImg{
  width:50rpx;height:50rpx;margin-top:6rpx;margin-left:6rpx;
}
.showImg{
  width:300rpx;
  height:300rpx;
  margin-left:50rpx;
  margin-top:50rpx;
  border-radius:10rpx;
}
.resultImg{
  width:300rpx;
  height:300rpx;
  margin-left:50rpx;
  margin-top:25rpx;
  border-radius:50%;
}
.result{
  margin-top:20rpx;
}
.resultTitle{
  margin-left:75rpx;
  margin-top:10rpx;
  color:#2B79F5;
  font-size:25rpx;
}
.productTableTr{
  height: 80rpx;line-height: 80rpx;border-bottom: 5rpx solid #F8F8F8;display:flex;
}
.leftTr{
  width: 583rpx;height: 80rpx;line-height: 80rpx;
}
.rightTr{
  width: 119rpx;height: 80rpx;line-height: 80rpx;color: #FF2525;font-size: 26rpx;
}
.leftTrText{
  color: #2B79F5;font-size: 28rpx;margin-left: 15rpx;width: 283rpx;
}
.productDetailTable{
  width: 702rpx;margin-left: 24rpx;border:5rpx solid #F8F8F8;border-radius: 6rpx;
}
.copyBtn{
  color:white;background:#2B79F5;border-radius:8rpx;width:100rpx;height:50rpx;margin-top:15rpx;
}
 /**
   * 页面的初始数据
   */
  data: {
    token: '',
    imgSrc: '',
    baseData: '',
  },
   /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    let grant_type = 'client_credentials';
    let client_id = '';
    let client_secret = '';
    wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=' + grant_type + '&client_id=' + client_id + '&client_secret=' + client_secret,
      method: 'post',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          token: res.data.access_token
        });
      }
    })
  },
  loadImage() {
    let that = this;
    wx.chooseImage({
      count: 0,
      sizeType: ['original', 'compressed'], //原图 / 压缩
      sourceType: ['album', 'camera'], //相册 / 相机拍照模式
      success(res) {
        that.setData({
          imgSrc: res.tempFilePaths[0]
        });
        //将图片转换为Base64格式
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0],
          encoding: 'base64',
          success(data) {
            let baseData = data.data; //'data:image/png;base64,' + data.data;
            that.setData({
              baseData: baseData
            });
          }
        });
      }
    })
  },
举报

相关推荐

ai换脸教程

0 条评论