如何在Web应用程序中从Web摄像机捕获图像

时间:2018-10-28 15:46:16

标签: javascript

我正在使用Java脚本从网络摄像头捕获图像,但无法正常工作,请帮忙。

  <div>
   <video id="video" width="400" height="300" autoplay>

        </video>
        <button id="snap">take photo</button>
        <canvas id="canvas" width="400" height="300"></canvas>
    </div>
    <script>
        var video = document.getElementById('video');
        if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia)
        {
            navigator.mediaDevices.get{ { video: true, Audio: true } }.then(function (stream) {
                video.src = window.URL.createObjectURL(stream);
            });
        }
        var canvas = document.getElementById('canvas');
        var context = canvas.getContext('2d');
        document.getElementById('snap').addEventListener('click', function(){
            context.drawImage(video, 0, 0, 400, 300);
        });
    </script>

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { navigator.mediaDevices.getUserMedia({ video: true, Audio: true } ).then(function (stream) { video.src = window.URL.createObjectURL(stream); }); }

我在听到错误消息时犯了错误,谢谢您的帮助!