无法使用我的代码(html/js/css)初始化网络摄像头

cannot initialize webcam with my code (html /js/css)

本文关键字:css js 初始化 摄像头 网络 html 我的 代码      更新时间:2024-03-26

我正试图让ebcam在我的html中工作,但它不起作用。相框就在那里。但我无法让它发挥作用。网络摄像头没有打开,甚至没有弹出窗口询问我是否想打开我的wbcam。。。。

我是不是错过了什么?像插件一样让它工作?o还是我的代码有错误?

这是html:

<!doctype html>
<head>
</head>
<body>
<link href="css.css" rel="stylesheet" />
<script src="js.js"></script>
<video id="video"></video>
<button id="startbutton">Take photo</button>
<canvas id="canvas"></canvas>
<img src="http://placekitten.com/g/200/150" id="photo" alt="photo">
</body>

这是csss:

html {
    background: #111111;
    height: 100%;
    background: linear-gradient( #333, #000);
}
canvas {
    display: none;
}
video, img, #startbutton {
    display: block;
    float: left;
    border: 10px solid #fff;
    border-radius: 10px;
}
#startbutton {
    background: green;
    border: none;
    color: #fff;
    margin: 100px 20px 20px 20px;
    padding: 10px 20px;
    font-size: 20px;
}
#container {
    overflow: hidden;
    width: 880px;
    margin: 20px auto;
}/

这是js:

// JavaScript Document
(function() {
  var streaming = false,
      video        = document.querySelector('#video'),
      cover        = document.querySelector('#cover'),
      canvas       = document.querySelector('#canvas'),
      photo        = document.querySelector('#photo'),
      startbutton  = document.querySelector('#startbutton'),
      width = 200,
      height = 0;
  navigator.getMedia = ( navigator.getUserMedia || 
                         navigator.webkitGetUserMedia ||
                         navigator.mozGetUserMedia ||
                         navigator.msGetUserMedia);
  navigator.getMedia(
    { 
      video: true, 
      audio: false 
    },
    function(stream) {
      if (navigator.mozGetUserMedia) { 
        video.mozSrcObject = stream;
      } else {
        var vendorURL = window.URL || window.webkitURL;
        video.src = vendorURL ? vendorURL.createObjectURL(stream) : stream;
      }
      video.play();
    },
    function(err) {
      console.log("An error occured! " + err);
    }
  );
  video.addEventListener('canplay', function(ev){
    if (!streaming) {
      height = video.videoHeight / (video.videoWidth/width);
      video.setAttribute('width', width);
      video.setAttribute('height', height);
      canvas.setAttribute('width', width);
      canvas.setAttribute('height', height);
      streaming = true;
    }
  }, false);
  function takepicture() {
    canvas.width = width;
    canvas.height = height;
    canvas.getContext('2d').drawImage(video, 0, 0, width, height);
    var data = canvas.toDataURL('image/png');
    photo.setAttribute('src', data);
  }
  startbutton.addEventListener('click', function(ev){
      takepicture();
    ev.preventDefault();
  }, false);
})();

它确实要求访问并显示我的网络摄像头:http://fiddle.jshell.net/e4Hf9/

不知道这里发生了什么,也许你有缓存问题。尝试隐姓埋名/私人浏览您的问题页面

  1. 使用Mozilla教程仔细检查您的代码拍摄网络摄像头照片
  2. 确保您的项目已正确部署在服务器上,如apache。并通过xx.xx.xx.xx:port/your_page访问您的页面

如果直接双击打开代码文件,它将不起作用。