Integrating the SDK
Step 1. Configure a domain allowlist
As the SDK will request the backend to perform authentication and load resources, you need to configure a domain allowlist on the Mini Program backend.
1. Open the Mini Program backend and go to Development > Development Management > Development Settings > Server Domain Name. 2. Click Modify, configure the following domain names and save them.
Request domain names:
https://webar.qcloud.com;
https://webar-static.tencent-cloud.com;
https://aegis.qq.com;
The URL of the authentication signature API (`get-ar-sign`)
downloadFile domain name:
https://webar-static.tencent-cloud.com
Step 2. Install and build the npm package
1. Install:
npm install tencentcloud-webar
2. Open Weixin DevTools and select Tools > Build npm on the topbar.
3. Configure the path of workers
in app.json
:
"workers": "miniprogram_npm/tencentcloud-webar/worker"
Step 3. Import the files
import '../../miniprogram_npm/tencentcloud-webar/lib.js';
import '../../miniprogram_npm/tencentcloud-webar/core.js';
import '../../miniprogram_npm/tencentcloud-webar/lib-3d.js';
import { plugin3d } from '../../miniprogram_npm/tencentcloud-webar/plugin-3d'
import { ArSdk } from "../../miniprogram_npm/tencentcloud-webar/index.js";
Note
Because Mini Program has a 500 KB limit for file size, the SDK is provided as multiple JS files.
Starting from v0.3.0, the SDK is further split to support 3D. The 3D module can be loaded as needed. Before import, check your SDK version and use the corresponding import method.
Step 4. Initialize an instance
<camera
device-position="{{'front'}}"
frame-size="large" flash="off" resolution="medium"
style="width: 750rpx; height: 134rpx;position:absolute;top:-9999px;"
/>
<canvas
type="webgl"
canvas-id="main-canvas"
id="main-canvas"
style="width: 750rpx; height: 1334rpx;">
</canvas>
<canvas
type="2d"
canvas-id="photo-canvas"
id="photo-canvas"
style="position:absolute;width:720px;height:1280px;top:-9999px;left:-9999px;">
</canvas>
Component({
methods: {
async getCanvasNode(id) {
return new Promise((resolve) => {
this.createSelectorQuery()
.select(`#${id}`)
.node()
.exec((res) => {
const canvasNode = res[0].node;
resolve(canvasNode);
});
});
},
async initSdkCamera() {
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
});
this.sdk = sdk
sdk.setBeautify({
whiten: 0.2
});
sdk.on('created', () => {
})
}
}
})
Note
Before initializing the SDK, you need to configure the Mini Program APPID
in the RT-Cube console
Unlike web, for Mini Programs, the input
parameter must be an image URL string.
Camera configuration is the same as that for web. Pass in the camera parameters instead of input
. Make sure you have already inserted a camera tag in your page.
Mini Program does not support getOutput
, so you need to pass in an onscreen WebGL canvas. The SDK will output images onto this canvas.
Photo Taking and Shooting
The SDK works for both photo taking and shooting in a Mini Program.
The SDK will return an object containing the width, height, and buffer data. You can draw the data onto the 2D canvas on your page and export it as an image file.
Component({
...
async takePhoto() {
const {uint8ArrayData, width, height} = this.sdk.takePhoto();
const photoCanvasNode = await this.getCanvasNode('photo-canvas');
photoCanvasNode.width = parseInt(width);
photoCanvasNode.height = parseInt(height);
const ctx = photoCanvasNode.getContext('2d');
const imageData = photoCanvasNode.createImageData(uint8ArrayData, width, height);
ctx.putImageData(imageData,0,0,0,0,width,height);
wx.canvasToTempFilePath({
canvas: photoCanvasNode,
x: 0,
y: 0,
width: width,
height: height,
destWidth: width,
destHeight: height,
success: (res) => {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath
});
}
})
}
})
Note
When the Mini Program is switched to the background or the screen is locked, call stopRecord
to stop shooting. If you don't do this, an error may occur.
Component({
methods: {
startRecord() {
this.sdk.startRecord()
}
async stopRecord() {
const res = await this.sdk.stopRecord();
wx.saveVideoToPhotosAlbum({
filePath: res.tempFilePath
})
}
}
})
Was this page helpful?