准备工作
1. 在 微信公众平台 注册并登录小程序。
3. 符合接入要求,并申请试用视立方 WEBAR License,详见 腾讯云视立方控制台。
接入配置
1. 使用组件前需在小程序工程的
app.json
中声明要使用的插件:{"plugins": {"webarPlugin": {"version": "1.0.4","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-imagelicenseKey="{{licenseKey}}"appId="{{appId}}"authFunc="{{authFunc}}"plugin3d="{{plugin3d}}"bind:created="onArCreated"style="width: 100vw; height: 100vh"/>
3. 在 page 的
.js
文件中使用 webarContext 设置美颜特效。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 | 是 | |
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 实例方法
鉴权方法 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 };