html獲取攝像頭拍照(優先獲取后攝像頭)


<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <title> 設備管理后台</title>
</head>
<body >

        <div >
            <!-- 內容主體區域 -->

            <div >
                <video id="video" width="480" height="320" controls></video>
                </div>
            <div >
               <input type="button" id="capture" value="拍照"/>
            </div>
            <div  >
                 <canvas id="canvas" width="480" height="480"></canvas>
   </div>


        </div>

</body>
</html>

<script>
    window.onload = function () {

        //訪問用戶媒體設備的兼容方法

        function getUserMedia(constraints, success, error) {

            if (navigator.mediaDevices.getUserMedia) {

                //最新的標准API

                navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);

            } else if (navigator.webkitGetUserMedia) {

                //webkit核心瀏覽器

                navigator.webkitGetUserMedia(constraints, success, error)

            } else if (navigator.mozGetUserMedia) {

                //firfox瀏覽器

                navigator.mozGetUserMedia(constraints, success, error);

            } else if (navigator.getUserMedia) {

                //舊版API

                navigator.getUserMedia(constraints, success, error);

            }

        }

        /*

        var exArray = []; //存儲設備源ID

        MediaStreamTrack.getSources(function (sourceInfos) {

        for (var i = 0; i != sourceInfos.length; ++i) {

        var sourceInfo = sourceInfos[i];

        //這里會遍歷audio,video,所以要加以區分

        if (sourceInfo.kind === 'video') {

        exArray.push(sourceInfo.id);

        }

        }

        });

        */



        let video = document.getElementById('video');

        let canvas = document.getElementById('canvas');

        let context = canvas.getContext('2d');



        function success(stream) {

            //兼容webkit核心瀏覽器

            let CompatibleURL = window.URL || window.webkitURL;

            //將視頻流設置為video元素的源

            //   console.log(stream);



            //video.src = CompatibleURL.createObjectURL(stream);

            video.srcObject = stream;

            video.play();

        }



        function error(error) {

            console.log(`訪問用戶媒體設備失敗${error.name}, ${error.message}`);

        }



        if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {

            if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {

                console.log("enumerateDevices() not supported.");

                return;

            }



            // 列出攝像頭和麥克風

            var exArray = [];

            navigator.mediaDevices.enumerateDevices()

                .then(function (devices) {

                    devices.forEach(function (device) {

                        //   console.log(device.kind + ": " + device.label +

                        //       " id = " + device.deviceId);

                        if (device.kind == "videoinput") {

                            //   alert(device.label);

                            exArray.push(device.deviceId);

                        }

                    });



                    var mediaOpts = { video: { width: 420, height: 120 } };

                    var mediaOpts =

                    {

                        video:

                        {

                            deviceId: { exact: exArray[1] }

                        }

                    };

                    //調用用戶媒體設備, 訪問攝像頭

                    getUserMedia(mediaOpts, success, error);

                })

                .catch(function (err) {

                    console.log(err.name + ": " + err.message);

                });

        } else {

            alert('不支持訪問用戶媒體');

        }

        var img = new Image();

        document.getElementById('capture').addEventListener('click', function () {

            context.drawImage(video, 0, 0, 480, 320);

            var image_data = context.getImageData(0, 0, 480, 320);
            // var string = OCRAD(image_data.data);

            // string = _arrayBufferToBase64(image_data);

            var image = new Image();
            image.src = canvas.toDataURL("image/jpeg");
            // return image;

            //   alert(image.src);

        })

    }


</script>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM