帮你快速理解、总结文档立即下载

图片&照片美颜

最近更新时间:2025-06-05 10:29:42
我的收藏
本文档指导您在原生微信小程序项目中接入 webar 图片美颜,uniapp 项目请参见 uniapp(小程序)接入。

准备工作

1. 在 微信公众平台 注册并登录小程序。
2. 在 微信公众平台 添加小程序插件:视立方 WEBAR。
3. 符合接入要求,并申请试用视立方 WEBAR License,详见 腾讯云视立方控制台。

接入配置

1. 使用组件前需在小程序工程的app.json中声明要使用的插件:
{
"plugins": {
"webarPlugin": {
"version": "latest",
"provider": "wx04445cff065100ed"
}
}
}
2. 在小程序目录安装 npm 包: tencentcloud-webar-wx
npm install tencentcloud-webar-wx
3. 打开微信开发者工具,工具 > 执行构建 npm。

使用 webar-image 组件接入美颜

设置美颜、滤镜、美妆、贴纸

webar-image 基于 webgl 相关能力,提供图片和照片美颜特效处理能力,支持导出和预览。
1. 在 page 的.json文件中定义需要引入的webar-image组件。
{
"usingComponents": {
"webar-image": "tencentcloud-webar-wx/webar-image"
}
}
2. 在 page 的.wxml文件中使用组件。
<webar-image
licenseKey="{{licenseKey}}"
appId="{{appId}}"
authFunc="{{authFunc}}"
plugin3d="{{plugin3d}}"
bind:created="onArCreated"
style="width: 100vw; height: 100vh"
/>
3. 在 page 的.js文件中使用 webarContext 设置美颜特效。
webar-image 会通过 created 事件返回 WebarContext,详情请参见 API 文档。
onArCreated(event) {
const webarContext = event.detail;
this.webarContext = webarContext;
// 设置美颜
webarContext.setBeautify({
whiten: 0.5,
dermabrasion: 0.6
});
// 设置滤镜
webarContext.setFilter(id, 1);
// 设置美妆、特效、贴纸
webarContext.setEffect([{ id, intensity:1 }]);
},

保存美化后照片

const path = await this.webarContext.download(false); // 保存图片,参数为是否存储到相册,返回值为图片路径

组件美颜特效属性说明

属性
类型
默认值
必填
说明
licenseKey
string
""
是
腾讯云视立方 licenseKey
appId
string
""
是
腾讯云 appid
authFunc
async function
null
是
需返回 signature,timestamp,详见 WEBAR 鉴权方法,传递时请不要包含在对象中
plugin3d
function
null
否
3d 特效插件,开启 3d 贴纸时需传入
beautify
Object
null
否
初始美颜参数,取值范围:0 - 1,可用: whiten、dermabrasion、lift、shave、eye、chin
url
string
null
否
图片地址,可以是http地址及微信文件系统路径。
bindcreated
event
null
否
美颜特效 SDK 初始化完成事件
bindready
event
null
否
美颜特效 SDK 加载完成事件
binderror
event
null
否
美颜特效 SDK 错误事件

webarContext 实例方法

获取可用滤镜列表、美妆列表、贴纸列表及设置美颜、滤镜、美妆、贴纸等方法,详见 API 文档。

鉴权方法 authFunc 生成方式

const config = {
appid: '您的腾讯云APPID',
token: '您的Token',
}
const authFunc = async function() {
const timestamp = Math.round(new Date().getTime() / 1000);
const signature = sha256(timestamp + config.token + config.appid + timestamp).toUpperCase();
return { signature, timestamp };
此方法仅测试时使用,为防止 token 泄露,发布时请使用服务端加密,详见 腾讯云-美颜特效 SDK 官网文档。

示例代码

详情请参见:示例项目。