js 调用 摄像头(只有没有安全限制的情况下才可使用)

<template>
    <div>
        <!--video用于显示媒体设备的视频流,自动播放-->
        <video ref="video" id="video" autoplay style="width: 480px;height: 320px"></video>
        <!--拍照按钮-->
        <div>
            {{msg}}
        </div>
        <ul>
            <li v-for="(v,i) in uuuu" :key="i">
                {{v}}
            </li>
        </ul>
        <div>
            <button id="capture" @click="captureF">拍照</button>
        </div>
        <!--描绘video截图-->
        <canvas ref="canvas" id="canvas" width="480" height="320"></canvas>
    </div>
</template>

<script>
var video;
var canvas;
var context;
export default {
    data() {
        return {
            msg: "",
            uuuu:[]
        };
    },
    mounted() {
        this.msg =1;
        console.log(this.$refs.video)
        this.$nextTick(() => {
             this.msg =1;
            video = document.getElementById("video")||this.$refs.video;
            canvas = document.getElementById("canvas")||this.$refs.canvas;
            console.log("video",video);
            console.log("canvas",canvas);
            context = canvas.getContext("2d");
            this.msg ="context"+context;
            if (
                navigator.mediaDevices.getUserMedia ||
                navigator.getUserMedia ||
                navigator.webkitGetUserMedia ||
                navigator.mozGetUserMedia
            ) {
                //调用用户媒体设备,访问摄像头
                this.getUserMedia(
                    {
                        video: { width: 480, height: 320 }
                    },
                    this.success,
                    this.error
                );
            } else {
                alert("你的浏览器不支持访问用户媒体设备");
            }
        });
    },
    methods: {
        captureF() {
            this.msg = "captureF+点击";
            this.msg = context;
            canvas.getContext("2d").drawImage(video, 0, 0, 480, 320);
        },
        getUserMedia(constrains, success, error) {
            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浏览器
                navagator
                    .mozGetUserMedia(constrains)
                    .then(success)
                    .catch(error);
            } else if (navigator.getUserMedia) {
                //旧版API
                navigator
                    .getUserMedia(constrains)
                    .then(success)
                    .catch(error);
            }
        },
        success(stream) {
            //兼容webkit内核浏览器
            var CompatibleURL = window.URL || window.webkitURL;
            //将视频流设置为video元素的源
            video.src = CompatibleURL.createObjectURL(stream);
            //播放视频
            video.play();
            this.msg = "success";
        },
        error(error) {
            (this.msg = "访问用户媒体设备失败:"), error.name, error.message;
            console.log("访问用户媒体设备失败:", error.name, error.message);
        }
    },watch:{
        msg(o,n){
          console.log(o);
          this.uuuu.push(o);
        }
    }
};
</script>

<style>
#video{
    background-color: red;
}
</style>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值