从浏览器访问相机

问题描述 投票:0回答:10

是否可以从浏览器访问相机(苹果内置)?

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

javascript camera
10个回答
19
投票

截至 2017 年,WebKit 宣布在 Safari 上支持 WebRTC

现在您可以使用

video
和标准 javascript WebRTC 访问它们

例如

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;
});

玩一玩吧。


17
投票

HTML5 规范确实允许访问网络摄像头,但我上次检查时发现它还远未最终确定,并且浏览器支持非常非常少。

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

如果您希望它能够跨浏览器工作,您可能必须使用 Flash。

W3 草案


6
投票

丹尼·马尔科夫对此有一个非常酷的解决方案。它使用 navigator.getUserMedia 方法,应该在现代浏览器中工作。我已经用 Firefox 和 Chrome 测试成功。 IE 无法运行:

这是一个演示:

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

链接到丹尼·马尔科夫描述页面:

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

GitHub 链接:

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


5
投票

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

<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>


2
投票

您可以使用 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>

来源


2
投票
    <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>

0
投票

视频教程:使用 HTML5 和 appMobi API 访问相机将对您有所帮助。

另外,你也可以尝试

getUserMedia
方法(Opera 12支持)

enter image description here


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>


0
投票

由于 navigator.getUserMedia 已弃用并且是旧版 API 的一部分,请改用 navigator.mediaDevices.getUserMedia。事实证明它与 Firefox 和 Chrome 兼容:

function loadCamera ()
{
   let v = document.getElementById ("myvideo");
   navigator.mediaDevices.getUserMedia({video:{width:v.width, height:v.height}})
      .then((mediaStream) => {  v.srcObject = mediaStream; })
      .catch(e => { console.error(`${e.name}: ${e.message}`); });

   v.play();
}

loadCamera ();

如果视频图像在显示之前要进行一些特殊处理,例如背景替换或作为 WebGL2 纹理,则还可以选择将其加载到内存中:

function makeVideo (height, width)
{
   let video  = document.createElement ("video");
   video.height      = height;
   video.width       = width;
   video.autoplay    = true;
   video.playsInline = true;
   video.muted       = true;
   video.loop        = true;
   return video;
}
async function loadCamera (height, width)
{
   let v  = makeVideo (height, width);

   navigator.mediaDevices.getUserMedia({video:{width:width, height:height}})
      .then((mediaStream) => {  v.srcObject = mediaStream; })
      .catch(e => { console.error(`${e.name}: ${e.message}`); });

   v.play();
   return v;

}

then
处理程序中添加任何处理

loadCamera (180, 320).then(video => { document.body.appendChild(video); });
© www.soinside.com 2019 - 2024. All rights reserved.