从浏览器访问摄像头

时间:2012-08-19 06:47:39

标签: javascript camera

是否可以从浏览器访问相机(内置在Apples上)?

最佳解决方案是客户端javascript。希望避免使用Java或Flash。

9 个答案:

答案 0 :(得分:13)

HTML5规范允许访问网络摄像头,但最后我检查过,它还远未定稿,并且支持非常少的浏览器。

这是一个帮助您入门的链接: http://www.html5rocks.com/en/tutorials/getusermedia/intro/

如果你想让它跨浏览器工作,你可能不得不使用flash。

W3 draft

答案 1 :(得分:7)

截至2017年,WebKit announces support for WebRTC on Safari

现在您可以使用video和标准JavaScript WebRTC

访问它们

E.g。

var video = document.createElement('video');
video.setAttribute('playsinline', '');
video.setAttribute('autoplay', '');
video.setAttribute('muted', '');
video.style.width = '200px';
video.style.height = '200px';

/* Setting up the constraint */
var facingMode = "user"; // Can be 'user' or 'environment' to access back or front camera (NEAT!)
var constraints = {
  audio: false,
  video: {
   facingMode: facingMode
  }
};

/* Stream it to video element */
navigator.mediaDevices.getUserMedia(constraints).then(function success(stream) {
  video.srcObject = stream;
});

玩弄它。

答案 2 :(得分:5)

Danny Markov为此提供了一个非常酷的解决方案。它使用navigator.getUserMedia方法,应该可以在现代浏览器中使用。我已经使用Firefox和Chrome成功测试了它。 IE没有工作:

这是一个演示:

https://tutorialzine.github.io/pwa-photobooth/

链接到Danny Markovs描述页面:

http://tutorialzine.com/2016/09/everything-you-should-know-about-progressive-web-apps/

链接到GitHub:

https://github.com/tutorialzine/pwa-photobooth/

答案 3 :(得分:2)

答案 4 :(得分:1)

您可以使用HTML5:

<video autoplay></video>
<script>
  var onFailSoHard = function(e) {
    console.log('Reeeejected!', e);
  };

  // Not showing vendor prefixes.
  navigator.getUserMedia({video: true, audio: true}, function(localMediaStream) {
    var video = document.querySelector('video');
    video.src = window.URL.createObjectURL(localMediaStream);

    // Note: onloadedmetadata doesn't fire in Chrome when using it with getUserMedia.
    // See crbug.com/110938.
    video.onloadedmetadata = function(e) {
      // Ready to go. Do some stuff.
    };
  }, onFailSoHard);
</script>

Source

答案 5 :(得分:1)

Video Tutorial: Accessing the Camera with HTML5 & appMobi API 会对您有所帮助。

另外,您可以尝试getUserMedia方法(由Opera 12支持)

enter image description here

答案 6 :(得分:0)

    <style type="text/css">
        #container {
            margin: 0px auto;
            width: 500px;
            height: 375px;
            border: 10px #333 solid;
        }

        #videoElement {
          width: 500px;
          height: 375px;
          background-color: #777;
        }
    </style>    
<div id="container">
            <video autoplay="true" id="videoElement"></video>
        </div>
        <script type="text/javascript">
          var video = document.querySelector("#videoElement");
          navigator.getUserMedia = navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia||navigator.oGetUserMedia;
    
          if(navigator.getUserMedia) {
            navigator.getUserMedia({video:true}, handleVideo, videoError);
          }
    
          function handleVideo(stream) {
            video.srcObject=stream;
            video.play();
          }
    
          function videoError(e) {
    
          }
        </script>

答案 7 :(得分:0)

**简单的JAVASCRIPT香草**

var video = document.querySelector("#videoElement");

if (navigator.mediaDevices.getUserMedia) {
    navigator.mediaDevices.getUserMedia({ video: true })
        .then(function (stream) {
            video.srcObject = stream;
        })
        .catch(function (err0r) {
            console.log("Something went wrong!");
        });
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="stuff, to, help, search, engines, not" name="keywords">
<meta content="What this page is about." name="description">
<meta content="Display Webcam Stream" name="title">
<title>Display Webcam Stream</title>
<style>
  #container{
   align-self: center;
   margin-left: 350px;
   align-items: center;
   justify-content: center;
position: relative;
    width: 1000px;
    height: 1000px;
       background-color: black;
       padding: 3px;
  }
  #videoElement{
    transform: rotate(90deg);
   align-self: center;
   height: 50a0px;
   left: 20;
   width: 700px;
   position:absolute;
   padding: 1px;
   top: 120px;
  }
</style>
</head>
  
<body>
<div id="container">
    <video autoplay="true" id="videoElement">
    </video>
</div>
<script src="index.js">

</script>
</body>
</html>

答案 8 :(得分:0)

是的,可以从浏览器访问相机,以下是对我有用的代码

<html><head>
</head><body>
    <video src="" ></video>
    <br />
<button id='flipCamera'>Flip</button>
</body>
<script>
  var front = false;
var video = document.querySelector('video');
  document.getElementById('flipCamera').onclick = function() { front = !front; };
  var constraints = { video: { facingMode: (front? "user" : "environment"), width: 640, height: 480  } };
  navigator.mediaDevices.getUserMedia(constraints)
  .then(function(mediaStream) {
    video.srcObject = mediaStream;
    video.onloadedmetadata = function(e) {
    video.play();
};
})
.catch(function(err) { console.log(err.name + ": " + err.message); })
</script></html>