天天看點

HTML之調用攝像頭實作拍照和攝像功能

應該有很多人知道,我們的手機裡面有個功能是“抓拍入侵者”,說白了就是在解鎖應用時如果我們輸錯了密碼手機就會調用這一功能實作自動拍照。

其實在手機上還有很多我們常用的軟體都有類似于這樣的功能,比如微信掃一掃二維碼,玩圖……

感覺挺有趣的,今天,我們就來研究一下這個功能的原理,不過我們不是用Android來做,而是用HTML5和javascript來做,浏覽器支援IE9+。

布局很簡單,就是設定一個“拍照”按鈕的監聽器,調用攝像頭video,然後顯示出來畫像。(需要使用者權限)

首先,我們要允許網頁寬度自動調整,我們在網頁頭添加viewport标簽:

<meta name="viewport" content="width=device-width, initial-scale=1" />
      

還有一個重點知識,就是getUserMedia(擷取使用者多媒體)。

使用這個getUserMedia API可以通路多媒體裝置,利用該API與<video>和canvas元素,可以在浏覽器裡面捕獲許多漂亮的圖檔。

是以除了視訊,還有音頻,是以接口要變成類似{video: true,audio:false},可以設定音視訊的擷取開關。

 navigator對象包含的屬性描述了正在使用的浏覽器,可以使用這些屬性進行平台專用的配置。

如果浏覽器檢測不到攝像頭,則會提示:

HTML之調用攝像頭實作拍照和攝像功能

That\'s all right,我們直接走起看Demo吧:

<!DOCTYPE html>

<head>

<title>HTML5 GetUserMedia Demo</title>

<meta charset="utf-8">

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

</head>

<body>

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

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

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

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

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

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

<script type="text/javascript">

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

var audio, audioType;

var canvas1 = document.getElementById(\'canvas1\');

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

var canvas2 = document.getElementById(\'canvas2\');

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

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

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

var exArray = []; //儲存設備源ID

MediaStreamTrack.getSources(function (sourceInfos) {

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

var sourceInfo = sourceInfos[i];

//這裡會周遊audio,video,是以要加以區分

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

exArray.push(sourceInfo.id);

}

}

});

function getMedia() {

if (navigator.getUserMedia) {

navigator.getUserMedia({

\'video\': {

\'optional\': [{

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

}]

},

\'audio\':true

}, successFunc, errorFunc); //success是擷取成功的回調函數

}

else {

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

}

}

function successFunc(stream) {

//alert(\'Succeed to get media!\');

if (video.mozSrcObject !== undefined) {

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

video.mozSrcObject = stream;

}

else {

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

}

//video.play();

// 音頻

audio = new Audio();

audioType = getAudioType(audio);

if (audioType) {

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

audio.play();

}

}

function errorFunc(e) {

alert(\'Error!\'+e);

}

// 将視訊幀繪制到Canvas對象上,Canvas每60ms切換幀,形成肉眼視訊效果

function drawVideoAtCanvas(video,context) {

window.setInterval(function () {

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

}, 60);

}

//擷取音頻格式

function getAudioType(element) {

if (element.canPlayType) {

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

return (\'aac\');

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

return ("ogg");

}

}

return false;

}

// vedio播放時觸發,繪制vedio幀圖像到canvas

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

// drawVideoAtCanvas(video, context2);

// }, false);

//拍照

function getPhoto() {

context1.drawImage(video, 0, 0,90,120); //将video對象内指定的區域捕捉繪制到畫布上指定的區域,實作拍照。

}

//視訊

function getVedio() {

drawVideoAtCanvas(video, context2);

}

</script>

</body>