這篇文章主要介紹“如何使用HTML5實現(xiàn)拍照功能”,在日常操作中,相信很多人在如何使用HTML5實現(xiàn)拍照功能問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何使用HTML5實現(xiàn)拍照功能”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
創(chuàng)新互聯(lián)建站是專業(yè)的合川網(wǎng)站建設公司,合川接單;提供成都做網(wǎng)站、成都網(wǎng)站建設,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行合川網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
代碼如下:
<!--
聲明: 此div應該在允許使用webcam,網(wǎng)絡攝像頭之后動態(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>
JavaScript
只要上面的HTML元素創(chuàng)建完成,那么JavaScript部分將簡單的超乎你想象的簡單:
代碼如下:
// 設置事件監(jiān)聽,DOM內(nèi)容加載完成,和jQuery的$.ready() 效果差不多。
window.addEventListener("DOMContentLoaded", function() {
// canvas 元素將用于抓拍
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"),
// video 元素,將用于接收并播放攝像頭 的數(shù)據(jù)流
video = document.getElementById("video"),
videoObj = { "video": true },
// 一個出錯的回調(diào)函數(shù),在控制臺打印出錯信息
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);
最后,記得講您的網(wǎng)頁放到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內(nèi)容加載完成,和jQuery的$.ready() 效果差不多。
window.addEventListener("DOMContentLoaded", function() {
// canvas 元素將用于抓拍
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"),
// video 元素,將用于接收并播放攝像頭 的數(shù)據(jù)流
video = document.getElementById("video"),
videoObj = { "video": true },
// 一個出錯的回調(diào)函數(shù),在控制臺打印出錯信息
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>
<div>
<!--
聲明: 此div應該在允許使用webcam,網(wǎng)絡攝像頭之后動態(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>
</div>
</body>
</html>
到此,關于“如何使用HTML5實現(xiàn)拍照功能”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)頁標題:如何使用HTML5實現(xiàn)拍照功能
當前網(wǎng)址:http://aaarwkj.com/article30/gjodso.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供建站公司、電子商務、標簽優(yōu)化、品牌網(wǎng)站設計、自適應網(wǎng)站、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)