web H5摄像头-Media-Recorder-API-Demo

2021-11-08 10:15:56 浏览数 (1)

media-recorder-api

代码语言:javascript复制
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>拍照2</title>
</head>
 
<body>
    <div  style="display: block">
    <button id="take" >拍照</button>
    <button onclick="upload()" >保存照片</button>
    <button onclick="closeapp()" >退出</button>
   </div>
   <div class="voidetop" style="position: relative;display: block">
<video id="v" width="350"  style="height: 100%; padding: 0;margin:50px 0;border:10px solid #f00;background: #022"  ></video>
    <canvas id="canvas" style="display:none;"></canvas>
   </div>
    <br />
    <div class="voideimg" style="position: absolute; left: 480px; top: 100px; width: 370px; height: 370px;" >    
    <img height="350" width="350" src="http://placehold.it/640&text=Your image here ..." id="photo" alt="photo">
    </div>
    <script>
        !(function () {
            // 老的浏览器可能根本没有实现 mediaDevices,所以我们可以先设置一个空的对象
            if (navigator.mediaDevices === undefined) {
                navigator.mediaDevices = {};
            }
            if (navigator.mediaDevices.getUserMedia === undefined) {
                navigator.mediaDevices.getUserMedia = function (constraints) {
                    // 首先,如果有getUserMedia的话,就获得它
                    var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
 
                    // 一些浏览器根本没实现它 - 那么就返回一个error到promise的reject来保持一个统一的接口
                    if (!getUserMedia) {
                        return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
                    }
 
                    // 否则,为老的navigator.getUserMedia方法包裹一个Promise
                    return new Promise(function (resolve, reject) {
                        getUserMedia.call(navigator, constraints, resolve, reject);
                    });
                }
            }
            const constraints = {
                video: true,
                audio: false
            };
            let videoPlaying = false;
            let v = document.getElementById('v');
            let promise = navigator.mediaDevices.getUserMedia(constraints);
            promise.then(stream => {
                // 旧的浏览器可能没有srcObject
                if ("srcObject" in v) {
                    v.srcObject = stream;
                } else {
                    // 防止再新的浏览器里使用它,应为它已经不再支持了
                    v.src = window.URL.createObjectURL(stream);
                }
                v.onloadedmetadata = function (e) {
                    v.play();
                    videoPlaying = true;
                };
            }).catch(err => {
                console.error(err.name   ": "   err.message);
            })
            document.getElementById('take').addEventListener('click', function () {
                if (videoPlaying) {
                    let canvas = document.getElementById('canvas');
                    canvas.width = v.videoWidth;
                    canvas.height = v.videoHeight;
                    canvas.getContext('2d').drawImage(v, 0, 0);
                    let data = canvas.toDataURL('image/webp');
                    document.getElementById('photo').setAttribute('src', data);
                }
            }, false); 
            
        })();
        function upload() {
            var d = document.getElementById("photo").toDataURL("image/jpeg");
            d = d.slice(d.indexOf(",")   1);
            ajaxRequest(frmMain.hfrWebCamPic, "saveimg", ["img=" d]); 
            }
        function closeapp() {
    ajaxRequest(frmMain.hfrWebCamPic, "closeapp", []);
  }
    </script>
</body>
</html>

0 人点赞