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æ¯è·åæåçåè°å½æ°
}
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>
HTML5 The Media Capture APIæä¾äºå¯¹æå头çå¯ç¼ç¨è®¿é®ï¼ç¨æ·å¯ä»¥ç´æ¥ç¨getUserMediaè·å¾æå头æä¾çè§é¢æµãä½å®é
ä¸ç¨html5è°ç¨ææºæå头åå¨å¾å¤é®é¢ï¼
1ï¼è°·æçåå¸çChromeå°äº21çæ¬åï¼ææ°å¢äºä¸ä¸ªç¨äºé«è´¨éè§é¢é³é¢é讯çgetUserMedia APIï¼è¯¥APIå
许Webåºç¨ç¨åºè®¿é®æå头å麦å
é£,å
¶ä»ææºæµè§å¨åªæoperaæ¯æhtml5è°ç¨æ¬å°æç
§åè½
2ï¼ä¸¤ä¸ªæµè§å¨åä¸æ¯æ访é®å¤ä¸ªæå头ï¼chromeä¸æ¯æ访é®åç½®æå头ï¼peraæ¯æ访é®åç½®æå头ç