久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      通過HTML5的getUserMedia實現(xiàn)拍照功能示例

      1. 通過getUserMedia調(diào)用設(shè)備的攝像頭(電腦、手機都可以,取決于瀏覽器對這個API的支持情況),并將資源放入video標(biāo)簽。
      2. 將video內(nèi)的視頻資源通過canvas的drawImage API放入canvas里,這個canvas是不顯示的。
      3. 將canvas的內(nèi)容轉(zhuǎn)換成base64編碼的webp格式的圖像(如果瀏覽器不支持這個格式,會fallback到png格式)放入img里,于是你就能看到你拍的照片了。

      不廢話了,上代碼:

      HTML

      <!doctype html>  <html>  <head>      <title>html5 capture test</title>      <link rel="stylesheet" href="style.css">  </head>  <body>      <video autoplay></video>      <img src="">      <canvas style="display: none;"></canvas>      <button id="capture">snapshot</button>          <script src="index.js"></script>  </body>  </html>

      JS

      var video = document.querySelector('video');  var canvas = document.querySelector('canvas');  var ctx = canvas.getContext('2d');  var localMediaStream = null;    var snapshot = function () {      if (localMediaStream) {          ctx.drawImage(video, 0, 0);          document.querySelector('img').src = canvas.toDataURL('image/webp');      }  };    var sizeCanvas = function () {      setTimeout(function () {          canvas.width = video.videoWidth;          canvas.height = video.videoHeight;          img.width = video.videoWidth;          img.height = video.videoHeight;      }, 100);  };    var btnCapture = document.getElementById('capture');  btnCapture.addEventListener('click', snapshot, false);    navigator.webkitGetUserMedia(      {video: true},      function (stream) {          video.src = window.URL.createObjectURL(stream);          localMediaStream = stream;          sizeCanvas();      },      function () {          alert('your browser does not support getUserMedia');      }  );

      幾個注意事項:

      不同瀏覽器對getUserMedia的支持情況不同,需要加上前綴,比如webkitGetUserMedia、mozGetUserMedia、msGetUserMedia,如果你想屏蔽這一問題的話,可以這樣做:

      // cross platforms  var myGetUserMedia = navigator.getUserMedia ||                    navigator.webkitGetUserMedia ||                   navigator.mozGetUserMedia ||                    navigator.msGetUserMedia;

      Chrome對file:///做了很多的限制,跨域就不說了,geolocation也不能在本地下使用,這個getUserMedia也是。

      這個sizeCanvas函數(shù)做的事情就是保證你拍到的照片的大小和攝像頭拍到的大小是一樣的,否則會出現(xiàn)拍到的照片只有攝像頭錄到的一部分畫面的情況。

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號