navigator.getUserMedia调用摄像头拍照

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>摄像头拍照</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <input type="file" accept="image/*" capture="camera">
    <input type="file" accept="video/*" capture="camcorder">
    <input type="file" accept="audio/*" capture="microphone">

    <video id='video'></video>
    <canvas id="canvas"></canvas>
    <img id="image">

    <script>
        var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia
        if (getUserMedia) {
            navigator.getUserMedia({ 
                    audio: true, 
                    video: { width: 90, height: 120 } 
                },
                function(stream) {
                    var video = document.querySelector('video')
                    video.src = window.URL.createObjectURL(stream)
                    video.onloadedmetadata = function(e) {
                    video.play()
                    }
                },
                function(err) {
                    console.log(err)
                }
            )
        }
        document.getElementById('video').onclick = function () {
            var canvas = document.getElementById("canvas")            
            var context = canvas.getContext("2d")            
            var video = document.getElementById("video")
            context.drawImage(video, 0, 0, 90, 120)
            document.getElementById('image').src = canvas.toDataURL("image/png")
        }
    </script>
</body>
</html>

  

posted @ 2018-07-31 18:06  鱿鱼须须  阅读(3396)  评论(0编辑  收藏  举报