cocos creator使用头条小游戏相机功能作为背景

it2023-05-28  80

cocos creator使用头条小游戏相机功能作为背景

版本:2.4.2

const { ccclass, property } = cc._decorator; @ccclass export default class TestVideo extends cc.Component { private camera: any = null; private video: any = null; private videoTexture: cc.Texture2D = null; @property(cc.Node) cameraNode:cc.Node = null; private frame: number = 0; onLoad() { this.startCamera(); } startCamera() { console.log('startCamera') this.camera = tt.createCamera(); tt.setKeepScreenOn(); // 保持屏幕常亮 this.camera.start('front', true).then(video => { console.log('front') this.video = video; console.log('this.video='+JSON.stringify(this.video)) this.initVideo(); // cocos视频映射应该在camera初始完成之后 }).catch(err => { tt.showToast({ title: '摄像机需要授权' }); console.log(err); }); this.camera.setBeautifyParam(1, 1, 1, 1); //设置美白、磨皮、大眼、瘦脸, 范围:[0, 1] } initVideo() { console.log('initVideo') this.videoTexture = new cc.Texture2D(); this.videoTexture.initWithElement(this.video); this.videoTexture.handleLoadedTexture(); this.cameraNode.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(this.videoTexture); this.setVideoWidth(cc.view.getVisibleSize().width) //固定宽度进行视频缩放 this.cameraNode.width = this.video.width; //设置在游戏界面画的视频宽度 this.cameraNode.height = this.video.height; //设置在游戏界面画的视频高度 console.log(`cameraNodewidth=${this.cameraNode.width} cameraNodeheight=${this.cameraNode.height}`) console.log(`videowidth=${this.video.width} videoheight=${this.video.height}`) this.cameraNode.rotation = -90 } setVideoWidth(width: number) { if (this.video) { this.video.width = width; this.video.height = this.video.videoHeight / this.video.videoWidth * width; } } update(dt) { this.frame++; if (this.frame >= 5) { // this.startDetector(); //每五帧进行一次人脸检测 this.frame = 0 } if (this.videoTexture && this.video) { // console.log('updatevideoTexture') this.videoTexture.update({ image: this.video, flipY: true }) } } }
最新回复(0)