1. 程式人生 > >HTML5利用WebRTC的getUserMedia獲取攝像頭資訊模擬拍照及視訊(完整示例(轉))

HTML5利用WebRTC的getUserMedia獲取攝像頭資訊模擬拍照及視訊(完整示例(轉))

轉至https://blog.csdn.net/journey191/article/details/40744015    (如有冒犯聯絡我刪掉謝謝)

最近公司做的app涉及攝像頭,由於我們使用HTML5開發app,自然瞭解到getUserMedia介面。

關鍵是使用的問題,所以直接貼程式碼(通過網際網路蒐集資料整理)。

 

 
  1. <!DOCTYPE html>

  2. <html xmlns="http://www.w3.org/1999/xhtml">

  3. <head>

  4. <title>HTML5 GetUserMedia Demo</title>

  5. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />

  6. </head>

  7. <body>

  8. <input type="button" title="開啟攝像頭" value="開啟攝像頭" onclick="getMedia();" /><br />

  9. <video height="120px" autoplay="autoplay"></video><hr />

  10. <input type="button" title="拍照" value="拍照" onclick="getPhoto();" /><br />

  11. <canvas id="canvas1" height="120px" ></canvas><hr />

  12. <input type="button" title="視訊" value="視訊" onclick="getVedio();" /><br />

  13. <canvas id="canvas2" height="120px"></canvas>

  14.  
  15. <script type="text/javascript">

  16. var video = document.querySelector('video');

  17. var audio, audioType;

  18.  
  19. var canvas1 = document.getElementById('canvas1');

  20. var context1 = canvas1.getContext('2d');

  21.  
  22. var canvas2 = document.getElementById('canvas2');

  23. var context2 = canvas2.getContext('2d');

  24.  
  25. navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;

  26. window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;

  27.  
  28. var exArray = []; //儲存裝置源ID

  29. MediaStreamTrack.getSources(function (sourceInfos) {

  30. for (var i = 0; i != sourceInfos.length; ++i) {

  31. var sourceInfo = sourceInfos[i];

  32. //這裡會遍歷audio,video,所以要加以區分

  33. if (sourceInfo.kind === 'video') {

  34. exArray.push(sourceInfo.id);

  35. }

  36. }

  37. });

  38.  
  39. function getMedia() {

  40. if (navigator.getUserMedia) {

  41. navigator.getUserMedia({

  42. 'video': {

  43. 'optional': [{

  44. 'sourceId': exArray[1] //0為前置攝像頭,1為後置

  45. }]

  46. },

  47. 'audio':true

  48. }, successFunc, errorFunc); //success是獲取成功的回撥函式

  49. }

  50. else {

  51. alert('Native device media streaming (getUserMedia) not supported in this browser.');

  52. }

  53. }

  54.  
  55. function successFunc(stream) {

  56. //alert('Succeed to get media!');

  57. if (video.mozSrcObject !== undefined) {

  58. //Firefox中,video.mozSrcObject最初為null,而不是未定義的,我們可以靠這個來檢測Firefox的支援

  59. video.mozSrcObject = stream;

  60. }

  61. else {

  62. video.src = window.URL && window.URL.createObjectURL(stream) || stream;

  63. }

  64.  
  65. //video.play();

  66.  
  67. // 音訊

  68. audio = new Audio();

  69. audioType = getAudioType(audio);

  70. if (audioType) {

  71. audio.src = 'polaroid.' + audioType;

  72. audio.play();

  73. }

  74. }

  75. function errorFunc(e) {

  76. alert('Error!'+e);

  77. }

  78.  
  79.  
  80. // 將視訊幀繪製到Canvas物件上,Canvas每60ms切換幀,形成肉眼視訊效果

  81. function drawVideoAtCanvas(video,context) {

  82. window.setInterval(function () {

  83. context.drawImage(video, 0, 0,90,120);

  84. }, 60);

  85. }

  86.  
  87. //獲取音訊格式

  88. function getAudioType(element) {

  89. if (element.canPlayType) {

  90. if (element.canPlayType('audio/mp4; codecs="mp4a.40.5"') !== '') {

  91. return ('aac');

  92. } else if (element.canPlayType('audio/ogg; codecs="vorbis"') !== '') {

  93. return ("ogg");

  94. }

  95. }

  96. return false;

  97. }

  98.  
  99. // vedio播放時觸發,繪製vedio幀影象到canvas

  100. // video.addEventListener('play', function () {

  101. // drawVideoAtCanvas(video, context2);

  102. // }, false);

  103.  
  104. //拍照

  105. function getPhoto() {

  106. context1.drawImage(video, 0, 0,90,120); //將video物件內指定的區域捕捉繪製到畫布上指定的區域,實現拍照。

  107. }

  108.  
  109. //視訊

  110. function getVedio() {

  111. drawVideoAtCanvas(video, context2);

  112. }

  113.  
  114. </script>

  115. </body>

  116. </html>


在安卓的Chrome(版本Beta 39.0.2171.44)上測試的效果截圖:

 

 

現在遇到一個問題:如何實現視訊錄製效果,也就是以視訊檔案儲存到本地?

將Canvas獲取的所有的幀影象組織起來,通過演算法轉換?

如果高見,望賜教!