<script charset="utf-8" src="https://webar-static.tencent-cloud.com/ar-sdk/resources/latest/webar-sdk.umd.js"></script><script src="https://webar-static.tencent-cloud.com/docs/examples/js-sha256/0.9.0/sha256.min.js"></script>
/** ----- 鉴权配置 ----- *//*** 腾讯云账号 APPID** 进入[腾讯云账号中心](https://console.tencentcloud.com/developer) 即可查看 APPID*/const APPID = ''; // 此处请填写您自己的参数/*** Web LicenseKey** 上述准备工作中创建的 license*/const LICENSE_KEY = ''; // 此处请填写您自己的参数/*** 计算签名用的密钥 Token** 注意:此处仅用于 DEMO 调试,正式环境中请将 Token 保管在服务端,签名方法迁移到服务端实现,通过接口提供,前端调用拉取签名,参考* 获取签名信息(https://trtc.io/zh/document/50099?platform=web&product=beautyar#e4ce3483-41f7-4391-83ae-f4b61e221ea0)*/const token = ''; // 此处请填写您自己的参数/** ----------------------- *//*** 定义获取签名方法** 注意:此处方案仅适用于 DEMO 调试,正式环境中签名方法推荐在服务端实现,通过接口提供,前端调用拉取签名* 如:* async function () {* return fetch('http://xxx.com/get-ar-sign').then(res => res.json());* };*/const getSignature = function () {const timestamp = Math.round(new Date().getTime() / 1000);const signature = sha256(timestamp + token + APPID + timestamp).toUpperCase();return { signature, timestamp };};// ar sdk 基础配置参数const config = {module: {beautify: true,handLandmark: true // 戒指试戴需要开启 handLandmark 模块},auth: {licenseKey: LICENSE_KEY,appId: APPID,authFunc: getSignature},// 此处使用内置 Camera 方式设置输入;自定义流的方式请参考 https://trtc.io/zh/document/50102?platform=web&product=beautyarcamera: {width: 1080,height: 720,mirror: true,},}// config 传入 ar sdk,初始化 ArSdk 实例const { ArSdk } = window.AR;const ar = new ArSdk(config);ar.on('error', (e) => {console.log(e);});
let makeupList, glassesList, contactLensesList, headwearList, ringListlet customEffectList// created回调里可以获取内置素材列表ar.on('created', async () => {// 获取内置女性类美妆makeupList = await ar.getEffectList({Type: 'Preset',Label: ['Makeup', 'Woman']})// 获取内置眼镜美妆glassesList = await ar.getEffectList({Type: 'Preset',Label: ['glasses']})// 获取内置美瞳美妆contactLensesList = await ar.getEffectList({Type: 'Preset',Label: ['Iris']})// 获取内置头戴素材headwearList = await ar.getEffectList({Type: 'Preset',Label: ['Headwear']})// 获取内置戒指素材ringList = await ar.getEffectList({Type: 'Preset',Label: ['rings']})// 有自定义素材的场景,设置 Type 为 'Custom' 以获取自定义素材// 自定义素材制作参考:https://trtc.io/zh/document/53887?platform=web&product=beautyarcustomEffectList = await ar.getEffectList({Type: 'Custom'})});
// 设置美妆素材function setMakeup() {ar.setEffect({id: makeupList[0].EffectId,})}// 设置眼镜素材function setGlasses() {ar.setEffect({id: glassesList[0].EffectId,})}// 设置美瞳素材function setContactLenses() {ar.setEffect({id: contactLensesList[0].EffectId,intensity: 0.6})}// 设置头戴素材function setHeadwear() {ar.setEffect({id: headwearList[0].EffectId,})}// 设置戒指素材function setRings() {ar.setEffect({id: ringList[0].EffectId,})}
ar.on('ready', async (e) => {// 获取ar sdk 输出的流const arOutputStream = await ar.getOutput();// 在 video 标签中预览效果const video = document.createElement("video");video.setAttribute("id", "webar-output-video");video.setAttribute("playsinline", "");video.setAttribute("autoplay", "");video.setAttribute("style", 'width:600px;height:auto;');video.srcObject = arOutputStream;document.body.appendChild(video);});
ar.on('ready', async (e) => {// localplayerContainer 为指定的 dom 容器 id,// 例如 html 中 <div style="display: inline-block;width: 1280px;height: auto;" id="localplayerContainer"></div>const player = await arSdk.initLocalPlayer('localplayerContainer')await player.play()});
Serving! ││ ││ - Local: http://localhost:57965 ││ - On Your Network: http://10.91.28.94:57965 ││ ││ This port was picked because 5000 is in use. ││ ││ Copied local address to clipboard!
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>webar virtual try on demo</title></head><body><div id="controls" style="margin-bottom: 20px;"><button type="button" onclick="setMakeup()">设置美妆</button><button type="button" onclick="setGlasses()">设置眼镜</button><button type="button" onclick="setContactLenses()">设置美瞳</button><button type="button" onclick="setHeadwear()">设置头戴</button><button type="button" onclick="setRings()">设置戒指</button></div><script charset="utf-8" src="https://webar-static.tencent-cloud.com/ar-sdk/resources/1.0.26-4/webar-sdk.umd.js"></script><script src="https://webar-static.tencent-cloud.com/docs/examples/js-sha256/0.9.0/sha256.min.js"></script><script>const APPID = '';const LICENSE_KEY = '';const token = '';const getSignature = function () {const timestamp = Math.round(new Date().getTime() / 1000);const signature = sha256(timestamp + token + APPID + timestamp).toUpperCase();return {signature,timestamp};};const config = {module: {beautify: true,handLandmark: true // 戒指试戴需要开启 handLandmark 模块},auth: {licenseKey: LICENSE_KEY,appId: APPID,authFunc: getSignature},camera: {width: 1080,height: 720,mirror: true,},}// config 传入 ar sdk,初始化 ArSdk 实例const { ArSdk } = window.AR;const ar = new ArSdk(config);ar.on('error', (e) => {console.log(e);alert(e.message);});let makeupList, glassesList, contactLensesList, headwearList, ringListlet customEffectList// created回调里可以获取内置素材列表ar.on('created', async () => {// 获取内置女性美妆makeupList = await ar.getEffectList({Type: 'Preset',Label: ['Makeup', 'Woman']})// 获取内置眼镜美妆glassesList = await ar.getEffectList({Type: 'Preset',Label: ['glasses']})// 获取内置美瞳美妆contactLensesList = await ar.getEffectList({Type: 'Preset',Label: ['Iris']})// 获取内置头戴素材headwearList = await ar.getEffectList({Type: 'Preset',Label: ['Headwear']})// 获取内置戒指素材ringList = await ar.getEffectList({Type: 'Preset',Label: ['rings']})// 有自定义素材的场景,设置 Type 为 'Custom' 以获取自定义素材// 自定义素材制作参考:https://trtc.io/zh/document/53887?platform=web&product=beautyarcustomEffectList = await ar.getEffectList({Type: 'Custom'})});function setMakeup() {ar.setEffect({id: makeupList[0].EffectId,})}function setGlasses() {ar.setEffect({id: glassesList[0].EffectId,})}function setContactLenses() {ar.setEffect({id: contactLensesList[0].EffectId,intensity: 0.6})}function setHeadwear() {ar.setEffect({id: headwearList[0].EffectId,})}function setRings() {ar.setEffect({id: ringList[0].EffectId,})}ar.on('ready', async (e) => {// 获取ar sdk 输出的流const arOutputStream = await ar.getOutput();// 在 video 标签中预览效果const video = document.createElement("video");video.setAttribute("id", "webar-output-video");video.setAttribute("playsinline", "");video.setAttribute("autoplay", "");video.setAttribute("style", 'width:600px;height:auto;');video.srcObject = arOutputStream;document.body.appendChild(video);});</script></body></html>
文档反馈