當前位置:編程學習大全網 - 源碼下載 - 能不能通過js代碼打開攝像頭

能不能通過js代碼打開攝像頭

html5中的video這個標簽是引入視頻的,通過navigator.getUserMedia去獲取攝像頭的視頻流,所以要在事件裏用關閉的代碼都不能執行關閉攝像頭,只有關閉網頁,攝像頭才關閉。

以下為html5打開攝像頭代碼:

<!DOCTYPE html>

<html>

<head>

<meta content="text/html; charset=UTF-8" http-equiv="content-type">

<title>Smart Home - Camera</title>

<link href="css/main.css" rel="stylesheet" type="text/css">

<script src="js/jq.js"></script>

<script type="text/javascript">

/*

*/

function init(t){

accessLocalWebCam("camera_box");

}

// Normalizes window.URL

window.URL = window.URL || window.webkitURL || window.msURL || window.oURL;

// Normalizes navigator.getUserMedia

navigator.getUserMedia = navigator.getUserMedia ||?

navigator.webkitGetUserMedia|| navigator.mozGetUserMedia ||?

navigator.msGetUserMedia;

function isChromiumVersionLower() {

var ua = navigator.userAgent;

var testChromium = ua.match(/AppleWebKit\/.* Chrome\/([\d.]+).* Safari\//);

return (testChromium && (parseInt(testChromium[1].split('.')[0]) < 19));

}

function successsCallback(stream) {

document.getElementById('camera_errbox').style.display = 'none';

document.getElementById('camera_box').src = (window.URL?

&& window.URL.createObjectURL) ?

window.URL.createObjectURL(stream) : stream;

}

function errorCallback(err) {

}

function accessLocalWebCam(id) {

try {

// Tries it with spec syntax

navigator.getUserMedia({ video: true }, successsCallback, errorCallback);

} catch (err) {

// Tries it with old spec of string syntax

navigator.getUserMedia('video', successsCallback, errorCallback);

}

}

</script>

<style type="text/css">

#camera_errbox{

width:320px; height:auto; border:1px solid #333333; padding:10px;

color:#fff; text-align:left;margin:20px auto;

font-size:14px;

}

#camera_errbox b{

padding-bottom:15px;

}

</style>

</head>

<body onLoad="init(this)" oncontextmenu="return false" onselectstart="return false">

<div>

<div id="mainbox">

<div id="bt_goback"></div>

<div></div><div id="t_iconbox"?

class="icon_12"></div><div id="t_text">

<div id="el_title">Camera</div>

<div id="el_descr"></div>

</div>

<div></div>

<div><span?

class="sp_title_text">Camera</span><div class="sp_oc?

sp_oc_1"></div></div>

<dl id="el_actionbox" style="text-align:center;">

<video id="camera_box" autoplay="" src=""></video>

<div id="camera_errbox">

<b>請點擊“允許”按鈕,授權網頁訪問您的攝像頭!</b>

<div>若您並未看到任何授權提示,則表示您的瀏覽器不支持Media Capture或您的機器沒有連接攝像頭設備。</div>

</div>

</dl>

</div>

</div>

</body>

</html>

-——代碼結束

  • 上一篇:怎麽查看Android sdk中的源碼demo,apidemo跟Android的版本有關系...
  • 下一篇:網站在線考試答題系統怎麽做?
  • copyright 2024編程學習大全網