index.html 6.5 KB
Newer Older
6
UPDATE  
64104061f23fda247c679fa8 已提交
1 2 3 4 5 6 7
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="style.css" rel="stylesheet" type="text/css" />
6
UPDATE  
62f378412b7489745d885e7f 已提交
8 9
    <script src="https://unpkg.com/vue@2.6.14/dist/vue.js"></script>
    <title>面部照片收集</title>
6
UPDATE  
64104061f23fda247c679fa8 已提交
10 11
</head>
<body>
6
UPDATE  
62f378412b7489745d885e7f 已提交
12 13 14 15 16 17 18 19 20 21 22 23
    <div id="app">
        <div class="publish">
            <div class="title center">面部照片收集</div>
            <div class="user">
                <div class="user_box">
                    <video width="251" height="361" id="video" ref="video" playsinline webkit-playsinline></video>
                    <canvas style="display: none" width="251" height="361" id="canvasCamera"></canvas>

                </div>
            </div>
            <div @click="setImage" class="submit d_btn center">确认采集</div>
        </div>
6
64104061f23fda247c679fa8 已提交
24
    </div>
6
UPDATE  
62f378412b7489745d885e7f 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    <script>
        (function (doc, win) {
            var docEl = doc.documentElement,
                resizeEvt =
                    "orientationchange" in window ? "orientationchange" : "resize",
                recalc = function () {
                    var clientWidth = docEl.clientWidth;
                    // if(clientWidth > 1024 ){
                    //   clientWidth = 375
                    // }
                    if (!clientWidth) return;
                    docEl.style.fontSize = 100 * (clientWidth / 750) + "px";
                };
            if (!doc.addEventListener) return;
            win.addEventListener(resizeEvt, recalc, false);
            recalc();
        })(document, window);
        // 新建一个Vue对象
        const vm = new Vue({
            // el是element的缩写,#app是id选择器
            el: '#app',
            // 页面使用的变量需要在data中注册(定义)
            data: {
                mediaStreamTrack: {},
                video_stream: "", // 视频stream
                imgSrc: "", // 拍照图片
                canvas: null,
                context: null,
                err: "",
                streamIns: "",
                URL: "",
            },
            created() {
            },
            mounted() {
                // 进入页面 自动调用摄像头
                this.getUserMedia(
                    {
                        //摄像头拍摄的区域
                        video: {
                            // width: { min: 640, ideal: 1920, max: 1920 },
                            // height: { min: 640, ideal: 1330 },
                            width: { min: 420, ideal: 500, max: 1920 },
                            height: { min: 420, ideal: 1920 },
                            facingMode: "user",
                        },
                        audio: false
                    },

                    this.success,

                    this.error
                );
            },
            methods: {
                success(stream) {
                    this.streamIns = stream;
                    const video = document.getElementById("video"); // webkit内核浏览器

                    this.URL = window.URL || window.webkitURL;

                    if ("srcObject" in video) {
                        video.srcObject = stream;
                    } else {
                        video.src = this.URL.createObjectURL(stream);
                    } // 苹果手机的系统弹框会阻止js的线程的继续执行 手动0.1秒之后自动执行代码

                    setTimeout(() => {
                        video.play();
                    }, 100);
                },

                error(e) {
                    console.log(e);
                    this.scanTip = "访问用户媒体失败";
                },
                getUserMedia(constrains, success, error) {
                    const supported = navigator.mediaDevices.getSupportedConstraints();
                    this.canvas = document.getElementById("canvasCamera");
                    this.context = this.canvas.getContext("2d");
                    if (navigator.mediaDevices === undefined) {
                        navigator.mediaDevices = {};
                    }
                    if (navigator.mediaDevices.getUserMedia) {
                        //最新标准API
                        navigator.mediaDevices
                            .getUserMedia(constrains)
                            .then(success)
                            .catch(error);
                    } else if (navigator.webkitGetUserMedia) {
                        //webkit内核浏览器
                        navigator.webkitGetUserMedia(constrains).then(success).catch(error);
                    } else if (navigator.mozGetUserMedia) {
                        //Firefox浏览器
                        navigator.mozGetUserMedia(constrains).then(success).catch(error);
                    } else if (navigator.getUserMedia) {
                        //旧版API
                        navigator.getUserMedia(constrains).then(success).catch(error);
                    } else {
                        this.scanTip = "你的浏览器不支持访问用户媒体设备";
                    }
                },
                // 拍照 绘制图片
                setImage() {
                    // 点击canvas画图
                    this.context.drawImage(this.$refs.video, 0, 0, 251, 361);
                    console.log("拍照", this.context.drawImage);
                    // 获取图片base64链接 canvas
                    this.canvas.style.display = "block";
                    console.log(this.canvas, "拍照 image ", this.canvas.style);
                    const image = this.canvas.toDataURL("image/png");

                    this.imgSrc = image;
                    console.log(this.imgSrc, "拍照 image ", image);
                    if (this.imgSrc) {
                        this.showPhoto = true;
                    }
                    //   this.$emit("refreshDataList", this.imgSrc);
                },
                dataURLtoFile(dataurl, filename) {
                    var arr = dataurl.split(",");
                    var mime = arr[0].match(/:(.*?);/)[1];
                    var bstr = atob(arr[1]);
                    var n = bstr.length;
                    var u8arr = new Uint8Array(n);
                    while (n--) {
                        u8arr[n] = bstr.charCodeAt(n);
                    }
                    return new File([u8arr], filename, { type: mime });
                },

            },
        })
    </script>
6
UPDATE  
64104061f23fda247c679fa8 已提交
159 160
</body>
</html>