JS获取video黑框及设置最大分辨率

以下是源代码

navigator.mediaDevices.getUserMedia({video: true,audio:false}).then(stream => {
            var videoTrack = stream.getVideoTracks()[0];
            var capabilities = videoTrack.getCapabilities();
            console.log("摄像头支持的分辨率: " + JSON.stringify(capabilities.width) + "x" + JSON.stringify(capabilities.height));
            var constraints = {
                width: { ideal: capabilities.width.max },  // 设置希望的宽度
                height: { ideal: capabilities.height.max }   // 设置希望的高度
            };
            // 应用新的约束
            videoTrack.applyConstraints(constraints)

            const video = document.getElementById('video')
            video.srcObject = stream
            video.play()
            video.onloadedmetadata = function () {
                var videoWidth = video.videoWidth;
                var videoHeight = video.videoHeight;
                var elementWidth = video.clientWidth;
                var elementHeight = video.clientHeight;

                // 计算黑边的宽度
                var horizontalBlackBorderWidth = (elementWidth - (elementHeight * (videoWidth / videoHeight))) / 2;
                var verticalBlackBorderWidth = (elementHeight - (elementWidth * (videoHeight / videoWidth))) / 2;

            console.log("水平黑边宽度: " + horizontalBlackBorderWidth + "px");
            console.log("垂直黑边宽度: " + verticalBlackBorderWidth + "px");

                const text = document.getElementById('text')
                text.style.marginLeft = Math.max(horizontalBlackBorderWidth, 0) + 'px'
                text.style.display = 'block'
            }
        })

 

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    请登录后查看评论内容