html5启用摄像头作用的完成编码

日期:2021-02-26 类型:科技新闻 

关键词:微信小程序页面设计,python小程序,小程序首页模板,如何建立微信小程序,怎么做微信小程序

序言

前些天,网上笔试的情况下,发现必须访问器愿意打开摄像头,觉得好像 js 启用的,因为那时候笔试,也就想不到这么多难题。今日闲来无事,看了下自身的 todo,发现有这个启用摄像头的todo,才想起😂。在网上查了1下,果真 js 有启用摄像头的 api,为此自身写1个 demo ,防止忘掉。

文章正文

启用摄像头

1共有两种完成方法,1种是应用navigator.getUserMedia(该特点早已从 Web 规范中删掉,尽管1些访问器现阶段依然适用它,但或许会在将来的某个時间终止适用,请尽可能不必应用该特点),前面1种早已从 Web 规范中删掉,仅以便向后适配而存在,第2种是应用navigator.mediaDevices.getUserMedia(强烈推荐应用),这两种方式 Safari 貌似都不适用。。。。

第1种方式navigator.getUserMedia用法详见mdn ,编码以下:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF⑻">
    <title>摄像头启用1</title>
</head>
<body>
    <video id="v"></video>
    <script>
        !(function () {
            function userMedia() {
                return navigator.getUserMedia = navigator.getUserMedia ||
                    navigator.webkitGetUserMedia ||
                    navigator.mozGetUserMedia ||
                    navigator.msGetUserMedia || null;
            }
            if (userMedia()) {
                var constraints = {
                    video: true,
                    audio: false
                };
                var media = navigator.getUserMedia(constraints, function (stream) {
                    var v = document.getElementById('v');
                    var url = window.URL || window.webkitURL;
                    v.src = url ? url.createObjectURL(stream) : stream;
                    v.play();
                }, function (error) {
                    console.log("ERROR");
                    console.log(error);
                });
            } else {
                console.log("不适用");
            }
        })();
    </script>
</body>
</html>

第2种方式navigator.mediaDevices.getUserMedia用法详见mdn。navigator.mediaDevices.getUserMedia 实际上和第1种类似,关键第2种回到是1个 Promise 目标,编码以下:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF⑻">
    <title>摄像头启用2</title>
</head>
<body>
    <video id="v"></video>
    <script>
        !(function () {
            // 老的访问器将会压根沒有完成 mediaDevices,因此大家能够先设定1个空的目标
            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;

                    // 1些访问器压根没完成它 - 那末就回到1个error到promise的reject来维持1个统1的插口
                    if (!getUserMedia) {
                        return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
                    }

                    // 不然,为老的navigator.getUserMedia方式包裹1个Promise
                    return new Promise(function (resolve, reject) {
                        getUserMedia.call(navigator, constraints, resolve, reject);
                    });
                }
            }
            const constraints = {
                video: true,
                audio: false
            };
            let promise = navigator.mediaDevices.getUserMedia(constraints);
            promise.then(stream => {
                let v = document.getElementById('v');
                // 旧的访问器将会沒有srcObject
                if ("srcObject" in v) {
                    v.srcObject = stream;
                } else {
                    // 避免再新的访问器里应用它,应为它早已已不适用了
                    v.src = window.URL.createObjectURL(stream);
                }
                v.onloadedmetadata = function (e) {
                    v.play();
                };
            }).catch(err => {
                console.error(err.name + ": " + err.message);
            })
        })();
    </script>
</body>
</html>

照相

思路是设定1个标示自变量 videoPlaying 看看是不是 video 有在 play,监视照相按钮的点一下恶性事件,假如videoPlaying 为 true ,应用1个canvas 获得 video 的宽高(默认设置 canvas 是无法显示的),随后应用 canvas 的drawImage,随后应用 canvas 的 toDataURL回到1个 data url,将这个 url,设定在1个 img 标识上便可😀

第1种方式navigator.getUserMedia完成编码:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF⑻">
    <title>照相1</title>
</head>
<body>
    <button id="take">照相</button>
    <br />
    <video id="v" style="width: 640px;height: 480px;"></video>
    <canvas id="canvas" style="display:none;"></canvas>
    <br />
    <img src="http://placehold.it/640&text=Your%20image%20here%20..." id="photo" alt="photo">
    <script>
        !(function () {
            function userMedia() {
                return navigator.getUserMedia = navigator.getUserMedia ||
                    navigator.webkitGetUserMedia ||
                    navigator.mozGetUserMedia ||
                    navigator.msGetUserMedia || null;
            }
            if (userMedia()) {
                let videoPlaying = false;
                let constraints = {
                    video: true,
                    audio: false
                };
                let video = document.getElementById('v');
                let media = navigator.getUserMedia(constraints, function (stream) {
                    let url = window.URL || window.webkitURL;
                    video.src = url ? url.createObjectURL(stream) : stream;
                    video.play();
                    videoPlaying = true;
                }, function (error) {
                    console.log("ERROR");
                    console.log(error);
                });
                document.getElementById('take').addEventListener('click', function () {
                    if (videoPlaying) {
                        let canvas = document.getElementById('canvas');
                        canvas.width = video.videoWidth;
                        canvas.height = video.videoHeight;
                        canvas.getContext('2d').drawImage(video, 0, 0);
                        let data = canvas.toDataURL('image/webp');
                        document.getElementById('photo').setAttribute('src', data);
                    }
                }, false);
            } else {
                console.log("不适用");
            }
        })();
    </script>
</body>
</html>

第2种navigator.mediaDevices.getUserMedia完成方式:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF⑻">
    <title>照相2</title>
</head>

<body>
    <button id="take">照相</button>
    <br />
    <video id="v" style="width: 640px;height: 480px;"></video>
    <canvas id="canvas" style="display:none;"></canvas>
    <br />
    <img src="http://placehold.it/640&text=Your%20image%20here%20..." id="photo" alt="photo">
    <script>
        !(function () {
            // 老的访问器将会压根沒有完成 mediaDevices,因此大家能够先设定1个空的目标
            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;

                    // 1些访问器压根没完成它 - 那末就回到1个error到promise的reject来维持1个统1的插口
                    if (!getUserMedia) {
                        return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
                    }

                    // 不然,为老的navigator.getUserMedia方式包裹1个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);
        })();
    </script>
</body>
</html>

以上便是本文的所有內容,期待对大伙儿的学习培训有一定的协助,也期待大伙儿多多适用脚本制作之家。