js 调用本地摄像头通过canvas进行截图

js 调用本地摄像头通过canvas进行截图

月光魔力鸭

2018-10-15 13:05 阅读 1687 喜欢 1 调用本地摄像头 canvas截图

如何通过js调用本地摄像头呢?获取后如何对视频进行截图呢?在这里跟大家做一个简易的Demo来实现以上几个功能。

涉及到的知识点

实现的功能点

html简单布局

以下先通过HTML我们来实现一个简单的布局,包括样式和按钮。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>H5 canvas 调用摄像头进行绘制</title>
    <style>
        html,body{
            width:100%;
            height:100%;
            padding: 0px;
            margin: 0px;
            overflow: hidden;
        }
        #canvas{
            width:500px;
            height:300px;
        }
        #video{
            width:500px;
            height:300px;
        }
        .btn{
            display:inline-block;
            text-align: center;
            background-color: #333;
            color:#eee;
            font-size:14px;
            padding:5px 15px;
            border-radius: 5px;
            cursor:pointer;
        }
    </style>
</head>
<body>
    <video id="video" autoplay="true" style="background-color:#ccc;display:none;"></video>
    <div style="width:500px;height:300px;margin:30px auto;">
        <canvas id="canvas" width="500px" height="300px">您的浏览器不支持H5 ,请更换或升级!</canvas>
        <span class="btn" filter="screenshot">截图</span>
        <span class="btn" filter="close">暂停</span>
        <span class="btn" filter="open">打开</span>
    </div>
    <div style="width:500px;height:300px;margin:40px auto;" id="show"></div>
</body>
</html>

样子差不多是这样的:

hahiahia 空白一片

我们将video进行了隐藏,然后加上了几个按钮,还有canvas以及最底部的图片展示区域(用来存放截图图片)。

js实现功能

这里先贴下核心代码:

navigator.getUserMedia({
    video : {width:500,height:300}
},function(stream){
    LV.video.srcObject = stream;
    LV.video.onloadedmetadata = function(e) {
        LV.video.play();
    };
},function(err){
    alert(err);//弹窗报错
})

相关的知识点可以参考:https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia

然后根据页面逻辑实现事件以及其他功能,包括:截图、暂停。

navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
    var events = {
        open : function(){
            LV.open();
        },
        close : function(){
            console.log(LV.timer);
            clearInterval(LV.timer);
        },
        screenshot : function(){
            //获得当前帧的图像并拿到数据
            var image = canvas.toDataURL('jpeg');
            document.getElementById('show').innerHTML = '<img src="'+image+'" style="width:500px;height:300px;" />'
        }
    };
    var LV = {
        video : document.getElementById('video'),
        canvas : document.getElementById('canvas'),
        timer : null,
        media : null,
        open :function(){
            if(!LV.timer){
                navigator.getUserMedia({
                    video : {width:500,height:300}
                },function(stream){
                    LV.video.srcObject = stream;
                    LV.video.onloadedmetadata = function(e) {
                    	LV.video.play();
                    };
                },function(err){
                    alert(err);//弹窗报错
                })
            }
            if(LV.timer){
                clearInterval(LV.timer);
            }
            //将画面绘制到canvas中
            LV.timer = setInterval(function(){
                LV.ctx.drawImage(LV.video,0,0,500,300);
            },15);
        },
        init : function(){
            LV.ctx = LV.canvas.getContext('2d');
            //绑定事件
            document.querySelectorAll('[filter]').forEach(function(item){
                item.onclick = function(ev){
                    var type = this.getAttribute('filter');
                    events[type].call(this,ev);
                }
            });
            return LV;
        }
    };
    LV.init();

原谅我放荡不羁的命名 ...


具体已经实现的demo可以点击 https://chrunlee.cn/demos/canvas-video/index.html

转载请注明出处: https://chrunlee.cn/article/js-local-video-canvas-screenshot.html


感谢支持!

赞赏支持
提交评论
评论信息 (请文明评论)
暂无评论,快来快来写想法...
推荐
在项目开发过程中,分页是少不了的,之前封装了一个分页组件,样式是基于bootstrap的样式,当然也可以自己来修改
对于web开发过程中的JS对象 Array ,我们真的充分使用了么?是不是理解了Array的全部?能够在合适的地点调用合适的函数,使用合适的属性?
web开发中,前台有时候会需要一个随机数或序列,通常来说,这个随机数可能只在当前页面中使用,并不需要太过严格,大体上重复率低即可。
突然来了一个调研任务,想要实现一个类似3D虚拟展厅类似的需求,主要就是放一些学生的作品,然后预览啥的,效果还是要全景的效果。 经过一番调查,最终锁定了以前从未接触过的krpano。
关于jquery,他的核心特点就是无new构造和链式调用,这里根据网上的资料,自己简单理解下,有助于消化核心代码。
Question from codewar,about all of array combinations.
codewars上的一个题目,这里记录下解决方法。
错误:The operation is insecure . 在使用canvas的 toDataUrl的时候报错,看了很多资料,基本上都说是设置跨域,但是我一直都有的啊。其实,只是顺序错了,setAttribute('crossOrigin','Anonymous') 要在设置 src之前设置。