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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值