调用本地摄像头使用HTML5 Canvas拍照并转为base64格式的图片

利用navigator对象
navigator.mediaDevices.getUserMedia({   audio:true,   video:true })
说明:
 navigator.mediaDevices.getUserMedia({
        audio: false, video: true
  }).then(function (result) {
        video.srcObject = result;
  })

h5利用canvas摄像拍照及转成base64格式,完成例子代码见下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>调用本地摄像头使用HTML5 Canvas拍照并转为base64格式的图片</title>
</head>
<body>
    <video muted  autoplay='autoplay' id='video'></video>
    <canvas id="canvas4" width="484px" height="484px" hidden></canvas>
    <button id='control'>拍照</button>

    <script>
    var video = document.getElementById('video');
        var audio, audioType;
        var canvas4 = document.getElementById('canvas4');
        var context4 = canvas4.getContext('2d');
        
        navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.mediaDevices;
        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': 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.srcObject = stream;
                // video.src = window.URL && window.URL.createObjectURL(stream) || stream;
            }
        }
        function errorFunc(e) {
            alert('Error!' + e);
        }


        // 将视频帧绘制到Canvas对象上,Canvas每60ms切换帧,形成肉眼视频效果
        function drawVideoAtCanvas(video, context) {
            window.setInterval(function () {
                context.drawImage(video, 0, 0, 800, 600);
            }, 60);
        }


        //拍照
        function getPhoto() {

            context4.drawImage(video, 0, 0, 484 ,484); //将video对象内指定的区域捕捉绘制到画布上指定的区域,实现拍照。
            var new_img = document.createElement('img');
            new_img.setAttribute('crossOrigin', 'anonymous');
            new_img.src = canvas4.toDataURL("image/jpeg");//图片转为base64格式
            new_img.setAttribute('width',300)
            new_img.setAttribute('height',300)
            console.log(canvas4.toDataURL("image/jpeg"))
            document.body.appendChild(new_img)
        }

        getMedia()
        control.addEventListener('click',function(){  
            getPhoto()
            // drawVideoAtCanvas()
        })
</script>
</body>
</html>

效果截图:

自拍照想想还是删了。。。

结束。

参考自:https://blog.csdn.net/qq_40678503/article/details/96318365

例子代码亲测可用。

posted @ 2022-02-16 17:48  仔行天下  阅读(472)  评论(0编辑  收藏  举报