当前位置:首页 > 编程相关 > 正文内容

Web端调用摄像头拍照

martinsun3年前 (2021-05-31)编程相关1.66 K

在网页中调用电脑摄像头拍照

下面的测试代码必须放在服务器上面,且域名必须是https 时才能正常调用摄像头!

<!doctype html>
<html lang="en">

<head>
    <title>拍照</title>
    <meta charset="utf-8">
    <style>
        .capture-info {
            color: #e6a23c;
            font-size: 14px;
        }
        .capture-video-box {
            background-color: #000;
            margin: 10px 0;
            width: 418px;
            height: 418px;
        }
        #capture-video {
            display: none;
        }
        .capture-box {
            width: 432px;
        }
        .capture-canvas-box {
            width: 86px;
            height: 86px;
            border: 1px solid #CCC;
            margin-bottom: 10px;
        }
        .button{
            display: inline-block;
            line-height: 1;
            white-space: nowrap;
            cursor: pointer;
            border: 1px solid #409eff;
            color: #fff;
            background-color: #409eff;
            -webkit-appearance: none;
            text-align: center;
            box-sizing: border-box;
            outline: none;
            margin: 0;
            transition: .1s;
            font-weight: 500;
            -moz-user-select: none;
            -webkit-user-select: none;
            -ms-user-select: none;
            padding: 12px 20px;
            font-size: 14px;
            border-radius: 4px;
        }
        .capture-open{
            position: absolute;
            top: 209px;
            left: 170px;
        }
    </style>
</head>

<body>
<div class="capture-box">
    <div class="capture-info">提示: 该功能需要使用你的电脑摄像头拍照,点击下方按钮开启</div>
    <div class="capture-video-box">
        <div class="capture-open button" id="capture-open-button" onclick="getMedia()">开启摄像头</div>
        <video id="capture-video" width="418px" height="418px" autoplay="autoplay"></video>
    </div>
    <div class="capture-canvas-box">
        <canvas id="capture-canvas" width="86px" height="86px"></canvas>
    </div>
    <div>
        <button class="button" onclick="takePhoto()">拍照</button>
        <button class="button">上传</button>
    </div>
</div>

<script>
        //获得video摄像头区域
        let video = document.getElementById("capture-video");
        var isInit = false;
        function getMedia() {
            let constraints = {
                video: { width: 418, height: 418 },
                audio: false
            };
            /*
            新的方法:H5新媒体接口 navigator.mediaDevices.getUserMedia()
            这个方法会提示用户是否允许媒体输入,(媒体输入主要包括相机,视频采集设备,屏幕共享服务,麦克风,A/D转换器等)
            返回的是一个Promise对象。
            如果用户同意使用权限,则会将 MediaStream对象作为resolve()的参数传给then()
            如果用户拒绝使用权限,或者请求的媒体资源不可用,则会将 PermissionDeniedError作为reject()的参数传给catch()
            */
            try{
                let promise = navigator.mediaDevices.getUserMedia(constraints);
                promise.then(function (MediaStream) {
                    video.style.display = 'block';
                    video.srcObject = MediaStream;
                    video.play();
                    isInit = true;
                    document.getElementById("capture-video").style.display = 'none';
                }).catch(function (PermissionDeniedError) {
                    console.log(PermissionDeniedError);
                })
            }catch (e) {
                alert('开启摄像头失败');
            }

        }
        function takePhoto() {
            if(isInit === false){
                alert('摄像头未开启,无法拍照');
                return false;
            }
            //获得Canvas对象
            let canvas = document.getElementById("capture-canvas");
            let ctx = canvas.getContext('2d');
            ctx.drawImage(video, 0, 0, 86, 86);
            var photoSrc = document.getElementById("capture-canvas").toDataURL("image/jpeg", 0.8);
            console.log(photoSrc, 12121)
        }
    </script>
</body>

</html>

版权声明:本文由MartinSun发布,内容供学习参考使用。

图文说明:部分图文来源于网络,如侵权请联系删除。

本文链接:https://www.sunyonghong.com/?id=37

分享给朋友:

相关文章

常用正则表达式汇总(持续更新中。。。)

常用正则表达式汇总(持续更新中。。。)

火车车次/^[GCDZTSPKXLY1-9]\d{1,4}$/手机机身码(IMEI)/^\d{15,17}$/必须带端口号的网址(或ip)/^((ht|f)tps?:\/\/)?[\w-]+(\....