【问题标题】:How do i detect multiple web-cameras connected to a system and choose a specific camera at a time?如何检测连接到系统的多个网络摄像头并一次选择特定的摄像头?
【发布时间】:2014-10-21 14:54:43
【问题描述】:

我正在尝试在网络应用程序上使用两个摄像头。我的要求是从浏览器中检测这些摄像头,并一次选择一个摄像头来捕获图像并将其显示在网页中。这可能就像我们显示两个按钮一样,通过单击一个按钮 camera1 将被检测并捕获图像,当用户按下另一个按钮时,第二个相机将用于捕获图像。

我需要使用 HTML5 和 JavaScript 来实现这一点。我已经有一些参考资料并尝试了 navigator.webkitGetUserMedia() 和 MediaStreamTrack.getSources(),但使用 MediaStreamTrack.getSources() 时出现脚本错误。 错误:“无法在‘MediaStreamTrack’上执行‘getSources’:功能尚未实现”。

目前浏览器能够显示摄像头,用户需要在页面加载时选择摄像头,我们可以通过编程方式进行吗?

这里是代码链接:http://jsfiddle.net/xL8wzzx2/

<!DOCTYPE html>
<body>
<video id="video" autoplay></video>
<button id="snap">Capture</button>
<button id="new">New</button>
<canvas id="canvas" width="640" height="480"></canvas>
<button id="upload">Upload</button>
</body>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1 /jquery.min.js"></script>
<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); 
};


/* < Error: TypeError: MediaStreamTrack.getSources is not a function >*/

MediaStreamTrack.getSources(function(sources){
  var cams = _.filter(sources, function(e){ //only return video elements 
  return e.kind === 'video';
  });
  var camIds = _.map(cams, function (e) { // return only ids
    return e.id;
 });
});

// </Error  >If the above block is removed, then it is working properly.




// 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);
// Littel effects
$('#video').fadeOut('slow');
$('#canvas').fadeIn('slow');
$('#snap').hide();
$('#new').show();
// Allso show upload button
//$('#upload').show();
});
// Capture New Photo
document.getElementById("new").addEventListener("click", function() {
$('#video').fadeIn('slow');
$('#canvas').fadeOut('slow');
$('#snap').show();
$('#new').hide();
});
// Upload image to sever 
document.getElementById("upload").addEventListener("click", function(){
var dataUrl = canvas.toDataURL();
$.ajax({
type: "POST",
url: "camsave.php",
data: { 
imgBase64: dataUrl
}
}).done(function(msg) {
console.log('saved');
// Do Any thing you want
});
});
}, false);


// Upload image to sever 
document.getElementById("upload").addEventListener("click", function(){
var dataUrl = canvas.toDataURL();
$.ajax({
type: "POST",
url: "camsave.php",
data: { 
imgBase64: dataUrl
}
}).done(function(msg) {
console.log('saved');
// Do Any thing you want
});
});


</script>

谢谢。

【问题讨论】:

  • 可能。你能分享任何实际的代码吗?

标签: webcam getusermedia


【解决方案1】:

您可以在 chrome 中检测多个摄像头(就像现在一样)。它是在一个名为 getSources() 的 js 函数的帮助下完成的。您可以使用以下代码:

function gotSources(sourceInfos) {
  var cam_count = 1;
  for (var i = 0; i != sourceInfos.length; ++i) {
    var sourceInfo = sourceInfos[i];
    if (sourceInfo.kind == 'video') {
      opt_text = "camera" + cam_count;
      cam_count++;
      $("#sel_src").append(new Option(opt_text, sourceInfo.id));
    }
  }
}

function set_cam() {
    var camera_id = document.getElementById("sel_src").value;
    $.post("assign_cam.php", {
        cam_id: camera_id,
        cam_number: cam_no
      },
      function(data, status) {
        alert(data);
      }
    );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="sel_src_list" style="color:white">
  <br>Capture Image :
  <select id="sel_src" name="sel_src1"></select>&nbsp;&nbsp;&nbsp;
  <input type="button" id="cam1" name="cam1" onclick="set_cam()" value="Assign">
</div>
<div style="color:white" id="src_list"></div>
<script>
  navigator.getUserMedia = navigator.getUserMedia ||
    navigator.webkitGetUserMedia || navigator.mozGetUserMedia;

  if (typeof MediaStreamTrack === 'undefined') {
    alert('This browser does not support MediaStreamTrack.\n\nTry Chrome Canary.');
  } else {
    MediaStreamTrack.getSources(gotSources);
  }
</script>

要查看它是否正常工作,您应该将多个摄像头连接到您的 PC 并安装 chrome 浏览器(最新版本)。您必须将这些相机 ID 存储在一个页面中,并在另一个页面中使用它来获取应该用于捕获图像的按钮。为此,您可以使用以下查询:

  function show_cam2(cam_src_id) {
    $('#video').fadeOut('slow');
    $('#canvas').fadeOut('slow');
    $('#video2').fadeIn('slow');
    $('#canvas').fadeIn('slow');
    // Grab elements, create settings, etc.
    var canvas = document.getElementById("canvas2"),
      context = canvas.getContext("2d"),
      video = document.getElementById("video2"),
      videoObj = {
        "video": true
      },
      errBack = function(error) {
        console.log("Video capture error: ", error.code);
      };



    var constraints = {
      video: {
        optional: [{
          sourceId: cam_src_id
        }]
      }
    };




    // Put video listeners into place
    if (navigator.getUserMedia) { // Standard
      navigator.getUserMedia(constraints, function(stream) {
        video.src = stream;
        video.play();
      }, errBack);
    } else if (navigator.webkitGetUserMedia) { // WebKit-prefixed
      navigator.webkitGetUserMedia(constraints, function(stream) {
        video.src = window.webkitURL.createObjectURL(stream);
        video.play();
      }, errBack);
    } else if (navigator.mozGetUserMedia) { // WebKit-prefixed
      navigator.mozGetUserMedia(constraints, function(stream) {
        video.src = window.URL.createObjectURL(stream);
        video.play();
      }, errBack);
    }
    // Trigger photo take
    document.getElementById("snap2").addEventListener("click", function() {
      context.drawImage(video, 0, 0, 640, 480);
      // Littel effects
      $('#video2').fadeOut('slow');
      $('#canvas2').fadeIn('slow');
      $('#snap2').hide();
      $('#new2').show();
    });
    // Capture New Photo
    document.getElementById("new2").addEventListener("click", function() {
      $('#video2').fadeIn('slow');
      $('#canvas2').fadeOut('slow');
      $('#snap2').show();
      $('#new2').hide();
    });
    // Upload image to sever 
    document.getElementById("upload2").addEventListener("click", function() {
      var dataUrl = canvas.toDataURL();
      $.ajax({
        type: "POST",
        url: "camsave.php",
        data: {
          imgBase64: dataUrl
        }
      }).done(function(msg) {
        console.log('saved');
        // Do Any thing you want
      });
    });
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table border=1>
  <tr>
    <td>
      <video id="video" width="320" height="240" autoplay></video>
    </td>
    <td>
      <button id="snap">Capture</button>
    </td>
    <td>
      <button id="new">New</button>
    </td>
    <td>
      <canvas id="canvas" width="320" height="240"></canvas>
    </td>
    <td>
      <button id="upload">Upload</button>
    </td>
    <td>
      <input type="button" onclick=show_cam( '< device #1>') value="capture_image">
    </td>
  </tr>
  <tr>
    <td>
      <video id="video2" width="320" height="240" autoplay></video>
    </td>
    <td>
      <button id="snap2">Capture2</button>
    </td>
    <td>
      <button id="new2">New2</button>
    </td>
    <td>
      <canvas id="canvas2" width="320" height="240"></canvas>
    </td>
    <td>
      <button id="upload2">Upload2</button>
    </td>
    <td>
      <input type="button" onclick=show_cam2( '< device #1>') value="capture_image_2">
    </td>

  </tr>
</table>
<script>
  // Put event listeners into place
  //window.addEventListener("DOMContentLoaded", function() {
  function show_cam(cam_src_id) {
      $('#video2').fadeOut('slow');
      $('#canvas2').fadeOut('slow');
      $('#video').fadeIn('slow');
      $('#canvas').fadeIn('slow');
      // 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);
        };


      var constraints = {
        video: {
          optional: [{
            sourceId: cam_src_id
          }]
        }
      };
      // navigator.getUserMedia(constraints, successCallback, errorCallback);



      // Put video listeners into place
      if (navigator.getUserMedia) { // Standard
        navigator.getUserMedia(constraints, function(stream) {
          video.src = stream;
          video.play();
        }, errBack);
      } else if (navigator.webkitGetUserMedia) { // WebKit-prefixed
        navigator.webkitGetUserMedia(constraints, function(stream) {
          video.src = window.webkitURL.createObjectURL(stream);
          video.play();
        }, errBack);
      } else if (navigator.mozGetUserMedia) { // WebKit-prefixed
        navigator.mozGetUserMedia(constraints, 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);
        // Littel effects
        $('#video').fadeOut('slow');
        $('#canvas').fadeIn('slow');
        $('#snap').hide();
        $('#new').show();
        // Allso show upload button
        //$('#upload').show();
      });
      // Capture New Photo
      document.getElementById("new").addEventListener("click", function() {
        $('#video').fadeIn('slow');
        $('#canvas').fadeOut('slow');
        $('#snap').show();
        $('#new').hide();
      });
      // Upload image to sever 
      document.getElementById("upload").addEventListener("click", function() {
        var dataUrl = canvas.toDataURL();
        $.ajax({
          type: "POST",
          url: "camsave.php",
          data: {
            imgBase64: dataUrl
          }
        }).done(function(msg) {
          console.log('saved');
          // Do Any thing you want
        });
      });
    }
    //}, false);
</script>

【讨论】:

  • 您的解决方案需要用户手动选择要使用的相机。你有什么想法可以做一些事情,比如代码的逻辑可以检测相机本身,所以为了一个目的,它会连接到特定的相机,而为了另一个目的,它会连接到另一个相机来使用。无需用户选择即可自动执行?
猜你喜欢
  • 2021-08-08
  • 1970-01-01
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
相关资源
最近更新 更多