HTML5拍照首先,我們看看HTML代碼結構,當然,這部分的DOM內容應該是在用戶允許使用其攝像頭事件出發(fā)后,動態(tài)加載生成的,感興趣的朋友可以了解下 演示地址: HTML5拍照演示
首先,我們看看HTML代碼結構,當然,這部分的DOM內容應該是在用戶允許使用其攝像頭事件出發(fā)后,動態(tài)加載生成的。
注意: 我們采用的是 640X480的分辨率,如果采用JS動態(tài)生成,那么您是可以靈活控制分辨率的。
代碼如下:
<!-- 聲明: 此p應該在允許使用webcam,網絡攝像頭之后動態(tài)生成 寬高: 640 *480,當然,可以動態(tài)控制啦! --> <!-- Ide all y these elements aren't created until it's confirmed that the client supports video/camera, but for the sake of illustrating the elements involved, they are created with markup (not JavaScript ) --> <video id="video" width ="640" height ="480" autoplay ></video> <button id="snap">Snap Photo</button> < canvas id="canvas" width="640" height="480"></canvas>
JavaScript
只要上面的HTML元素創(chuàng)建完成,那么JavaScript部分將簡單的超乎你想象的簡單:
代碼如下:
// 設置事件監(jiān)聽,DOM內容加載完成,和 jQuery 的$.ready() 效果差不多。 window.addEvent List ener("DOMContentLoaded", function() { // canvas 元素將用于抓拍 var canvas = document .getElementById("canvas"), context = canvas.getContext("2d"), // video 元素,將用于接收并播放攝像頭 的數據流 video = document.getElementById("video"), videoObj = { "video": true }, // 一個出錯的 回調函數 ,在控制臺打印出錯信息 errBack = function(error) { if (" object " === typeof window.console){ console.log("Video capture error: ", error.code); } }; // Put video listeners into place // 針對標準的瀏覽器 if(navigator.getUserMedia) { // Standard navigator.getUserMedia(videoObj, function(stream) { video.src = stream; video.play(); }, errBack); } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed navigator.webkitGetUserMedia(videoObj, function(stream){ video.src = window.webkitURL.createObjectURL(stream); video.play(); }, errBack); } // 對拍照 按鈕 的事件監(jiān)聽 document.getElementById("snap").addEventListener("click", function() { // 畫到畫布上 context.drawImage(video, 0, 0, 640, 480); }); }, false);
最后,記得講您的網頁放到web服務器下面,然后通過http協(xié)議來訪問哦。
另外,需要瀏覽器版本較新,并且支持HTML5的相關新特性才可以。
譯者不算稱職啦,沒有按原文來翻譯。使用的瀏覽器是chrome 28。
最后,貼上完整的代碼,比較呆板。
代碼如下:
<!DOCTYPE html> <html> <head> <title> 瀏覽器webcamera </title> <meta name="Generator" content="EditPlus"> <meta name="Author" content="renfufei@qq.com"> <meta name="Description" content="inveted by: http://davidwalsh.name/browser-camera"> <script> // 設置事件監(jiān)聽,DOM內容加載完成,和jQuery的$.ready() 效果差不多。 window.addEventListener("DOMContentLoaded", function() { // canvas 元素將用于抓拍 var canvas = document.getElementById("canvas"), context = canvas.getContext("2d"), // video 元素,將用于接收并播放攝像頭 的數據流 video = document.getElementById("video"), videoObj = { "video": true }, // 一個出錯的回調函數,在控制臺打印出錯信息 errBack = function(error) { if("object" === typeof window.console){ console.log("Video capture error: ", error.code); } }; // Put video listeners into place // 針對標準的瀏覽器 if(navigator.getUserMedia) { // Standard navigator.getUserMedia(videoObj, function(stream) { video.src = stream; video.play(); }, errBack); } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed navigator.webkitGetUserMedia(videoObj, function(stream){ video.src = window.webkitURL.createObjectURL(stream); video.play(); }, errBack); } // 對拍照按鈕的事件監(jiān)聽 document.getElementById("snap").addEventListener("click", function() { // 畫到畫布上 context.drawImage(video, 0, 0, 640, 480); }); }, false); </script> </head> <body> <p> <!-- 聲明: 此p應該在允許使用webcam,網絡攝像頭之后動態(tài)生成 寬高: 640 *480,當然,可以動態(tài)控制啦! --> <!-- Ideally these elements aren't created until it's confirmed that the client supports video/camera, but for the sake of illustrating the elements involved, they are created with markup (not JavaScript) --> <video id="video" width="640" height="480" autoplay></video> <button id="snap">Snap Photo</button> <canvas id="canvas" width="640" height="480"></canvas> </p> </body> </html>
【相關推薦】
1. Html5免費視頻教程
2. HTML5實現微信播放全屏的方法詳解
3. 關于H5新標簽的瀏覽器兼容問題的詳解
4. 整體概述如何用H5制作網頁
5. H5與傳統(tǒng)html的區(qū)別