1. 程式人生 > >H5技術調取本地攝像頭

H5技術調取本地攝像頭

<style>
#video {
width:640px;
height:480px;
}
#picture {
width:100%;
}
</style>

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

<div><button id='picture'>相機</button></div>  
<canvas id="canvas" width="640" height="480"></canvas>  
<script type="text/javascript">  
var video = document.getElementById("video");  
var context = canvas.getContext("2d");  
var errocb = function () {  
    console.log('sth wrong!');  

if (navigator.getUserMedia) { // 標準的API  
    navigator.getUserMedia({ "video": true }, function (stream) {  
        video.src = stream;  
        video.play();  
    }, errocb);  
} else if (navigator.webkitGetUserMedia) { // WebKit 核心的API  
    navigator.webkitGetUserMedia({ "video": true }, function (stream) {  
        video.src = window.webkitURL.createObjectURL(stream);  
        video.play();  
    }, errocb);  
}  
  
document.getElementById("picture").addEventListener("click", function () {  
    context.drawImage(video, 0, 0, 640, 480);  
});  
</script>