2015-11-06 43 views
1

我有一個插件,在這裏我使用筆記本電腦或手機上的網絡攝像頭捕獲圖片,然後將其放置在畫布上。所有這些在Chrome中運行良好,但是在使用Firefox時,它不起作用。我懷疑它的navigator.getUserMedia導致在Firefox中的問題,因爲它已被棄用。使用網絡攝像頭捕獲配置文件圖片,並在畫布上顯示圖片

鏈接:https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getUserMedia

而谷歌開發者建議使用navigator.getUserMedia的,它也與我的應用程序兼容。

鏈接:https://developers.google.com/web/updates/2015/10/media-devices

所以,請不要認爲什麼樣的變化,應在下面的代碼來完成爲它工作在Firefox。

在此先感謝。

<script type="text/javascript"> 
var ctx = null; 
var canvas = document.getElementById("tmpImage"); 
var localMediaStream = null; 
var video = document.querySelector('video'); 

function snapshot() { 
    if (localMediaStream) { 
     ctx.drawImage(video, 0, 0); 
     var img = document.getElementById('CaptureImage'); 
     // "image/webp" works in Chrome 18. In other browsers, this will fall back to image/png. 
     img.src = canvas.toDataURL('image/webp'); 
    } 
} 

function hasGetUserMedia() { 
    // Note: Opera builds are unprefixed. 
    return !!(navigator.getUserMedia || navigator.webkitGetUserMedia || 
     navigator.mozGetUserMedia || navigator.msGetUserMedia); 
} 

function onFailSoHard(e) { 
    if (e.code == 1) { 
     alert('User denied access to their camera'); 
    } else { 
     alert('getUserMedia() not supported in your browser.'); 
    } 
} 

function start() { 

    if (hasGetUserMedia()) { 
     if (navigator.webkitGetUserMedia) 
      navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; 
     //var getUserMedia = navigator.webkitGetUserMedia || navigator.getUserMedia; 


     //var gumOptions = { video: true, toString: function() { return 'video'; } };  
     if (navigator.getUserMedia) { 

      navigator.getUserMedia({ 
       video: true, 
       audio: false 
      }, function (stream) { 
       if (navigator.webkitGetUserMedia) { 
        video.src = window.webkitURL.createObjectURL(stream); 
       } else { 
        video.src = stream; // Opera 
       } 
       localMediaStream = stream; 
      }, onFailSoHard); 
     } else { 
      video.src = 'somevideo.webm'; // fallback. 
     } 
    } 
} 

function stop() { 
    video.pause(); 
    video = document.getElementById('sourcevid'); 
    video.src = ""; 
    localMediaStream.stop(); 
} 

function ResizeCanvas() { 
    canvas.height = video.videoHeight; 
    canvas.width = video.videoWidth; 
} 

$(document).ready(function() { 
    ctx = canvas.getContext('2d'); 
}); 

回答

0

是,getUserMedia API是不斷髮展的。 新的語法是navigator.mediaDevices.getUserMedia。 Edge和Firefox已經實現了這個新的語法,它將返回一個Promise,迫使我們重寫我們的代碼... Chrome瀏覽器真的很晚,甚至仍然使用一些從未標準化的視頻約束語法。 該過程現在變得有點複雜,但有一些libraries處理這些情況。

我也寫我自己的實現應該處理最GUM功能的瀏覽器:

我不知道Chrome並允許getUserMedia從棧片斷所以這裏is a jsfiddle

var video,canvas,ctx; 
 
var settings = { 
 
    video_constraints: { 
 
    width: { 
 
     min: 1200, 
 
     max: 1920 
 
    }, 
 
    height: { 
 
     min: 720, 
 
     max: 1080 
 
    }, 
 
    require: ["width", "height"], 
 
    facingMode: "user", 
 
    }, 
 
    audio: false, 
 
} 
 

 
function getStream(video) { 
 
    video.streaming = false; 
 
    if (navigator.webkitGetUserMedia) 
 
    setWebkitConstraints(); 
 
    navigator.getUserMedia = navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia; 
 
    if (!navigator.getUserMedia && !navigator.mediaDevices) { 
 
    alert('GUM is not supported by this browser'); 
 
    return false; 
 
    } 
 
    var opts = { 
 
    video: settings.video_constraints, 
 
    audio: settings.audio 
 
    }; 
 
    var then = function(stream) { 
 
    if (navigator.mozGetUserMedia) 
 
     video.mozSrcObject = stream; 
 
    else { 
 
     var URL = window.URL || window.webkitURL; 
 
     video.src = URL.createObjectURL(stream); 
 
    } 
 
    video.play(); 
 
    loopStart(video); 
 
    }; 
 
    var error = function(err) { 
 
    console.log("An error occured! ", err) 
 
    if (err.name.indexOf('Permission') == 0) return; 
 
    }; 
 

 
    if (navigator.mediaDevices) { 
 
    navigator.mediaDevices.getUserMedia(opts).then(then, error).catch(error); 
 
    } else { 
 
    navigator.getUserMedia(opts, then, error); 
 
    } 
 
}; 
 
// handle webkit old and deprecated constraint syntax 
 
var setWebkitConstraints = function() { 
 
    var wkConstraints = { 
 
    mandatory: {} 
 
    }; 
 
    var c = settings.video_constraints; 
 
    for (var i in c) { 
 
    switch (i) { 
 
     case 'width': 
 
     for (j in c[i]) { 
 
      switch (j) { 
 
      case 'max': 
 
       wkConstraints.mandatory.maxWidth = c[i][j]; 
 
       break; 
 
      case 'min': 
 
       wkConstraints.mandatory.minWidth = c[i][j]; 
 
       break; 
 
      case 'exact': 
 
       wkConstraints.mandatory.minWidth = wkConstraints.mandatory.maxWidth = c[i][j]; 
 
       break; 
 
      } 
 
     }; 
 
     break; 
 

 
     case 'height': 
 
     for (var j in c[i]) { 
 
      switch (j) { 
 
      case 'max': 
 
       wkConstraints.mandatory.maxHeight = c[i][j]; 
 
       break; 
 
      case 'min': 
 
       wkConstraints.mandatory.minHeight = c[i][j]; 
 
       break; 
 
      case 'exact': 
 
       wkConstraints.mandatory.minHeight = wkConstraints.mandatory.maxHeight = c[i][j]; 
 
       break; 
 
      } 
 
     }; 
 
     break; 
 
     default: 
 
     break; 
 
    } 
 
    } 
 
    settings.video_constraints = wkConstraints; 
 
}; 
 
var loopStart= function(video){ 
 
\t \t 
 
\t \t if (!video.streaming) { 
 
\t \t \t if(video.videoHeight === 0){ 
 
\t \t \t \t window.setTimeout(function() { 
 
\t \t \t \t \t video.pause(); 
 
\t \t \t \t \t video.play(); 
 
\t \t \t \t \t loopStart(video); 
 
\t \t \t \t \t }, 100); 
 
\t \t \t \t } 
 
\t \t \t else { 
 
\t \t \t \t video.streaming = true; 
 
\t \t \t \t video.dispatchEvent(new Event('streaming')); 
 
\t \t \t \t } 
 
\t \t \t }else{return;} 
 
\t \t }; 
 

 
(function init(){ 
 
    video = document.createElement('video'); 
 
    canvas = document.createElement('canvas'); 
 
    document.body.appendChild(canvas); 
 
    ctx = canvas.getContext('2d'); 
 
    video.addEventListener('streaming', copy2canvas, false); 
 
    getStream(video); 
 
    })() 
 
function copy2canvas(){ 
 
\t console.log('succesfully streaming'); 
 
    canvas.width = video.videoWidth; 
 
    canvas.height = video.videoHeight; 
 
    draw(); 
 
    } 
 
function draw(){ 
 
    ctx.drawImage(video, 0,0); 
 
    requestAnimationFrame(draw); 
 
    }
canvas{border: 1px solid red}

2

使用官方adapter.js填充。問題解決了。

E.g. this fiddle適用於所有支持的瀏覽器。

navigator.mediaDevices.getUserMedia({ video: true }) 
    .then(stream => video.srcObject = stream) 
    .catch(e => console.error(e)); 

也支持modern constraints syntax

相關問題