小程序开发:接入腾讯云的人像动漫化api接口

一方 / 2024-02-29 / 原文

接口如下:

 

图片的传参方式有两种,一种是传图片的base64,一种是图片url:

 

我打算免费版使用base64,如果付费用户支持永久存储历史的图片记录(图片存储到腾讯云对象存储中)。

前端框架我用的uview,所以我在页面简单使用了uview的上传组件,拿到图片本地路径后再转成base64,腾讯云的api接口在云对象里调用。

上传组件:

 

大概界面就是这样:

 

接下来就是把base64数据传给接口了。

腾讯云的这个API Explorer功能还挺好用的,有请求的实例代码。

 

按他的实例,安装一下
tencentcloud-sdk-nodejs-ft。

切换到对应的云对象目录下,npm i tencentcloud-sdk-nodejs-f:

 

index.obj.js完整代码:

const tencentcloud = require("tencentcloud-sdk-nodejs-ft")

// 云对象教程:
https://uniapp.dcloud.net.cn/uniCloud/cloud-obj

// jsdoc语法提示教程:
https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129

// 腾讯云相关的api

module.exports = {

_before: function() { // 通用预处理器

},

// 统一的发送方法

async send(data) {

let system_config = await uniCloud.callFunction({

name: "get_system_config",

data: {

config_key: 'tencent_oss'

}

})

let Config = system_config['result'][0]['config_value']

const FtClient = tencentcloud.ft.v20200304.Client

const clientConfig = {

credential: {

secretId: Config.secretId,

secretKey: Config.secretKey,

},

region: Config.region,

profile: {

httpProfile: {

endpoint: "ft.tencentcloudapi.com",

},

},

}

// 实例化要请求产品的client对象,clientProfile是可选的

const client = new FtClient(clientConfig)

return new Promise((resolve, reject) => {

client.FaceCartoonPic(data).then(

(data) => {

resolve(data)

},

(err) => {

reject(err)

}

)

})

},

// 人像动漫化接口

async faceCartoonPic(image_base64) {

let data = {

"Image": image_base64,

"RspImgType": "url"

}

return await module.exports.send(data)

},

}

调用:

const tencentcloudapi = uniCloud.importObject('tencentcloudapi')

let result = await tencentcloudapi.faceCartoonPic(this.current_img_base64)

console.log('result', result)

效果:

 

 

 

生成正式版后出现问题了:

 

本地开发却没有这个问题,应该是
tencentcloud-sdk-nodejs-ft版本问题。

在uniapp的官方论坛搜索了下,果然找到了解决方案:

https://ask.dcloud.net.cn/question/139972

 

更新云对象根目录的package.json后,再重新上传部署。

问题解决!

欢迎大家使用我的小程序新上线的人像转动漫功能:微信搜索《一方云知》,找到小程序后拉到最下面有一个人像转动漫的图标,点进去就可以用啦,目前是免费使用哦。

这篇文章就到这里啦!如果你对文章内容有疑问或想要深入讨论,欢迎在评论区留言,我会尽力回答。同时,如果你觉得这篇文章对你有帮助,不妨点个赞并分享给其他同学,让更多人受益。

想要了解更多相关知识,可以查看我以往的文章,其中有许多精彩内容。记得关注我,获取及时更新,我们可以一起学习、讨论技术,共同进步。

感谢你的阅读与支持,期待在未来的文章中与你再次相遇!