快照后转动网络摄像头

时间:2014-08-13 17:23:20

标签: javascript getusermedia

我使用此代码来获取GetUserMedia()并拍摄快照。拍摄快照后,我想让相机关闭,有关如何完成此操作的提示吗?

$("#btnSaves").click(function () {

    var video = document.querySelector('#webcam');
    var button = document.querySelector('#screenshot-button');
    var canvas = document.querySelector('#screenshot-canvas');
    var ctx = canvas.getContext('2d');

    navigator.getUserMedia = (navigator.getUserMedia ||
                    navigator.webkitGetUserMedia ||
                    navigator.mozGetUserMedia ||
                    navigator.msGetUserMedia);
    if (navigator.getUserMedia) {
        navigator.getUserMedia
                    (
                      { video: true },
                      function (localMediaStream) {
                          video.src = window.URL.createObjectURL(localMediaStream);
                      }, onFailure);
    }
    else {
        onFailure();
    }
    button.addEventListener('click', snapshot, false);

    function snapshot() {
        canvas.width = video.videoWidth;
        canvas.height = video.videoHeight;
        ctx.drawImage(video, 0, 0);
    }
});

1 个答案:

答案 0 :(得分:1)

你应该保存对你的流的引用,如下所示:

function (localMediaStream) {
    myStream = localMediaStream;
    video.src = window.URL.createObjectURL(myStream);
}, onFailure);

稍后您可以使用:

myStream.stop(); 
myStream = null;