2017-07-19 211 views
1

我嘗試從網絡攝像頭拍攝網絡瀏覽器的快照。當我在下面使用此代碼時,它適用於移動設備(Google Chrome on Android),但不適用於Google Chrome on Desktop。視頻不顯示。我收到錯誤名稱:TrackStartErrorDevicesNotFoundError。它使用外部USB攝像頭進行測試。從網絡攝像頭拍攝網站快照

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <title>Display Webcam Stream</title> 
 
    <style> 
 
     #container { 
 
      margin: 0 auto; 
 
      max-width: 480px; 
 
     } 
 

 
     video, img { 
 
      max-width: 100%; 
 
      background-color: #f2f3f5; 
 
     } 
 

 
     video { 
 
      height: 400px; 
 
     } 
 
    </style> 
 
</head> 
 

 
<body> 
 
<div id="container"> 
 
    <video autoplay></video> 
 
</div> 
 
<script> 
 
    var video = document.querySelector('video'), canvas; 
 

 

 
    function takeSnapshot() { 
 
     var img = document.querySelector('img') || document.createElement('img'); 
 
     var context; 
 
     var width = video.offsetWidth, height = video.offsetHeight; 
 

 
     canvas = canvas || document.createElement('canvas'); 
 
     canvas.width = width; 
 
     canvas.height = height; 
 

 
     context = canvas.getContext('2d'); 
 
     context.drawImage(video, 0, 0, width, height); 
 

 
     img.src = canvas.toDataURL('image/png'); 
 
     document.body.appendChild(img); 
 
    } 
 

 
    if (navigator.mediaDevices === undefined) { 
 
     navigator.mediaDevices = {}; 
 
    } 
 

 
    if (navigator.mediaDevices.getUserMedia === undefined) { 
 
     navigator.mediaDevices.getUserMedia = function (constraints) { 
 

 
      var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia; 
 

 
      if (!getUserMedia) { 
 
       return Promise.reject(new Error('getUserMedia is not implemented in this browser')); 
 
      } 
 

 
      return new Promise(function (resolve, reject) { 
 
       getUserMedia.call(navigator, constraints, resolve, reject); 
 
      }); 
 
     } 
 
    } 
 

 
    var constraints = {audio: false, video: {width: 640, height: 480}}; 
 
    navigator.mediaDevices.getUserMedia(constraints) 
 
      .then(function (stream) { 
 
       if ("srcObject" in video) { 
 
        video.srcObject = stream; 
 
       } else { 
 
        video.src = window.URL.createObjectURL(stream); 
 
       } 
 

 
       video.onloadedmetadata = function (e) { 
 
        video.play(); 
 
       }; 
 

 
       video.addEventListener('click', takeSnapshot); 
 
      }) 
 
      .catch(function (err) { 
 
       console.log(err.name + ": " + err.message); 
 
      }); 
 
</script> 
 
</body> 
 
</html>

我出的任何選項:( 我做錯了嗎?它可以工作在最常用的桌面網絡瀏覽器。

回答

0

最有可能您的攝像頭或攝像頭驅動程序無法正常工作

這就是你所得到的兩個錯誤指向:

  • DevicesNotFoundError是Chrome在您請求視頻軌道(通過約束)但網絡攝像頭丟失時拋出的非規範錯誤。 Firefox/spec相當於NotFoundError

  • TrackStartError是當Chrome瀏覽器(在Windows上)由另一個應用程序使用攝像頭時拋出的非規格錯誤。 Firefox/spec相當於NotReadableError。所述MDN description指向其他原因,以及:

    儘管用戶獲准使用匹配裝置,在該防止對設備

    訪問的操作系統,瀏覽器,或網頁水平發生了硬件錯誤

來源:Common getUserMedia() Errors