html5éè¦ä½¿ç¨æ¥å£chrome30+ for android å·²ç»å®ç°äºå©ç¨webcamï¼è°ç¨ææºåç½®æå头ï¼ä»£ç å¦ä¸ï¼
<!DOCTYPE html>
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<title>HTML5 GetUserMedia Demo</title>
<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æ¯è·åæåç<a href="
https://www.baidu.com/s?wd=%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d-rHc3rjckuyf4nH0YPHPW0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnWDYnHndPjm3PH6vPWb1rjcvr0" target="_blank" class="baidu-highlight">åè°å½æ°</a>
}
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>
</html>