使用网络摄像头捕获图像并使用画布保存

时间:2016-04-04 07:16:57

标签: javascript

我创建了一个简单的HTML表单,用户应该可以使用他的设备网络摄像头拍摄自己的照片。我在表单中使用了这段HTML:

<div class="form-group">
    <div id="camera">
        <div class="center clear">
            <video id="video" class="picCapture" autoplay=""></video>
            <button id="snap" type="button" class="btn btn-default" onclick="return false;">Take Picture</button>
            <canvas id="canvas" class="picCapture"></canvas>
        </div>
    </div>
</div> 

和这个JS(catch-pic.js):

  // Put event listeners into place

      window.addEventListener("DOMContentLoaded", function() {
        // Grab elements, create settings, etc.
        var canvas = document.getElementById("canvas"),
          context = canvas.getContext("2d"),
          video = document.getElementById("video"),
          videoObj = { "video": true },
          errBack = function(error) {
            console.log("Video capture error: ", error.code);
          };

        // Put video listeners into place
        if(navigator.getUserMedia) { // Standard
          navigator.getUserMedia(videoObj, function(stream) {
            video.src = stream;
            video.play();
          }, errBack);
        } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
     ---28--- navigator.webkitGetUserMedia(videoObj, function(stream){
            video.src = window.URL.createObjectURL(stream);
            video.play();
          }, errBack);
        } else if(navigator.mozGetUserMedia) { // WebKit-prefixed
          navigator.mozGetUserMedia(videoObj, function(stream){
            video.src = window.URL.createObjectURL(stream);
            video.play();
          }, errBack);

    }

在我的本地机器中,每件事都有效。我可以在我的表单中看到网络摄像头的输出,并能够保存图片。但现在我将应用程序上传到服务器,我无法从网络摄像头获取流出现。 我检查了控制台,我打印出这两个:

getUserMedia() is deprecated on insecure origins, and support will be removed in the future. You should consider switching your application to a secure origin, such as HTTPS. See https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins for more details.
catch-pic.js:28 Video capture error:  undefined

我在catch-pic.js中标记了第28行。我真的不明白为什么这个只在我的机器上工作而不在其他人...任何想法? THX

1 个答案:

答案 0 :(得分:0)

我发现你的代码是这样的:

<script>

        // Put event listeners into place
        window.addEventListener("DOMContentLoaded", function() {
            // Grab elements, create settings, etc.
            var canvas = document.getElementById("canvas"),
                context = canvas.getContext("2d"),
                video = document.getElementById("video"),
                videoObj = { "video": true },
                errBack = function(error) {
                    console.log("Video capture error: ", error.code); 
                };

            // Put video listeners into place
            if(navigator.getUserMedia) { // Standard
                navigator.getUserMedia(videoObj, function(stream) {
                    video.src = stream;
                    video.play();
                }, errBack);
            } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
                navigator.webkitGetUserMedia(videoObj, function(stream){
                    video.src = window.webkitURL.createObjectURL(stream);
                    video.play();
                }, errBack);
            } else if(navigator.mozGetUserMedia) { // WebKit-prefixed
                navigator.mozGetUserMedia(videoObj, function(stream){
                    video.src = window.URL.createObjectURL(stream);
                    video.play();
                }, errBack);
            }

            // Trigger photo take
            document.getElementById("snap").addEventListener("click", function() {
                context.drawImage(video, 0, 0, 640, 480);
            });
        }, false);

    </script>

并运行此link