https://webar.qcloud.com;https://webar-static.tencent-cloud.com;https://aegis.qq.com;The URL of the authentication signature API (`get-ar-sign`)
https://webar-static.tencent-cloud.com
npm install tencentcloud-webar
workers in app.json:"workers": "miniprogram_npm/tencentcloud-webar/worker"
// The import method for versions earlier than 0.3.0 (one file)// import "../../miniprogram_npm/tencentcloud-webar/lib.js";// The import method for v0.3.0 or later (two files and the 3D module (optional))import '../../miniprogram_npm/tencentcloud-webar/lib.js';import '../../miniprogram_npm/tencentcloud-webar/core.js';// Initialize the 3D plugin (optional)import '../../miniprogram_npm/tencentcloud-webar/lib-3d.js';import { plugin3d } from '../../miniprogram_npm/tencentcloud-webar/plugin-3d'// Import `ArSdk`import { ArSdk } from "../../miniprogram_npm/tencentcloud-webar/index.js";
// wxml//Open the camera<cameradevice-position="{{'front'}}"frame-size="large" flash="off" resolution="medium"style="width: 750rpx; height: 134rpx;position:absolute;top:-9999px;"/>// The SDK outputs the processed image to the canvas in real time.<canvastype="webgl"canvas-id="main-canvas"id="main-canvas"style="width: 750rpx; height: 1334rpx;"></canvas>// Take a photo and draw the `ImageData` object onto the canvas<canvastype="2d"canvas-id="photo-canvas"id="photo-canvas"style="position:absolute;width:720px;height:1280px;top:-9999px;left:-9999px;"></canvas>// jsComponent({methods: {async getCanvasNode(id) {return new Promise((resolve) => {this.createSelectorQuery().select(`#${id}`).node().exec((res) => {const canvasNode = res[0].node;resolve(canvasNode);});});},// Initialize the camera typeasync initSdkCamera() {// Get the onscreen canvas. The SDK will output the processed image to the canvas in real time.const outputCanvas = await this.getCanvasNode("main-canvas");const sdk = new ArSdk({camera: {width:720,height:1280,},output: outputCanvas,loading: {enable: false,},auth: {licenseKey: LICENSE_KEY,appId: APP_ID,authFunc: authFunc},plugin3d: plugin3d // You can ignore this parameter (only available in v0.3.0 and later versions) if you don't need the 3D module.});this.sdk = sdksdk.setBeautify({whiten: 0.2});sdk.on('created', () => {// You can add your business logic in this callback. For details, see "Parameters and APIs".})}}})
APPID in the RT-Cube consoleinput parameter must be an image URL string.input. Make sure you have already inserted a camera tag in your page.getOutput, so you need to pass in an onscreen WebGL canvas. The SDK will output images onto this canvas.Component({...async takePhoto() {const {uint8ArrayData, width, height} = this.sdk.takePhoto(); // The `takePhoto` method returns the buffer data of the current image.const photoCanvasNode = await this.getCanvasNode('photo-canvas');photoCanvasNode.width = parseInt(width);photoCanvasNode.height = parseInt(height);const ctx = photoCanvasNode.getContext('2d');// Create an `ImageData` object with the data returned by the SDKconst imageData = photoCanvasNode.createImageData(uint8ArrayData, width, height);// Draw the `ImageData` object onto the canvasctx.putImageData(imageData,0,0,0,0,width,height);// Save the canvas as a local imagewx.canvasToTempFilePath({canvas: photoCanvasNode,x: 0,y: 0,width: width,height: height,destWidth: width,destHeight: height,success: (res) => {// Save the photowx.saveImageToPhotosAlbum({filePath: res.tempFilePath});}})}})
stopRecord to stop shooting. If you don't do this, an error may occur.Component({methods: {// Start shootingstartRecord() {this.sdk.startRecord()}// Stop shootingasync stopRecord() {const res = await this.sdk.stopRecord();// Save the videowx.saveVideoToPhotosAlbum({filePath: res.tempFilePath})}}})
Feedback