时间:2021-05-25
详解HTML5 使用video标签实现选择摄像头功能
1. html
// jquery reference // <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> // <input type="hidden" name="imgValue" id="imgValue" /> <button id="btnOpen1" class="btn btn-default" type="button" >Open WebCam</button> <select id="videoSource" ></select> <div id="vdoOne" style="display:none"> <video id="video" style="margin-top:15px;margin-bottom:15px;" width="300" autoplay></video> <canvas id="canvasPreview" style="margin-top:15px;" width="300" height="224"></canvas> <canvas id="canvasUpload" style="display:none;" width='300' height='224'></canvas> <button id="snap" class="btn btn-default" type="button">Snap Photo</button> </div>2. javascript
<script> //// Elements for taking the snapshot var canvasPreview = document.getElementById('canvasPreview'); var canvasUpload = document.getElementById('canvasUpload'); var contextPreview = canvasPreview.getContext('2d'); var contextUpload = canvasUpload.getContext('2d'); //#################### Video Source #######################3 var videoElement = document.querySelector('video'); var videoSelect = document.querySelector('select#videoSource'); navigator.mediaDevices.enumerateDevices() .then(gotDevices).then(getStream).catch(handleError); videoSelect.onchange = getStream; function gotDevices(deviceInfos) { for (var i = 0; i < deviceInfos.length; ++i) { var deviceInfo = deviceInfos[i]; var option = document.createElement('option'); option.value = deviceInfo.deviceId; if (deviceInfo.kind === 'videoinput') { option.text = deviceInfo.label || 'camera ' + (videoSelect.length + 1); videoSelect.appendChild(option); } else { console.log('Found ome other kind of source/device: ', deviceInfo); } } } var _streamCopy = null; function getStream() { if (_streamCopy != null) { try { _streamCopy.stop(); // if this method doesn't exist, the catch will be executed. } catch (e) { _streamCopy.getVideoTracks()[0].stop(); // then stop the first video track of the stream } } var constraints = { audio:false, video: { optional: [ { sourceId: videoSelect.value } ] } }; navigator.mediaDevices.getUserMedia(constraints).then(gotStream).catch(handleError); } function gotStream(stream) { _streamCopy = stream; // make stream available to console videoElement.srcObject = stream; } function handleError(error) { alert(error.name + ": " + error.message); } //######################## End Video Source ################# // Get access to the camera! if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { navigator.mediaDevices.getUserMedia({ video: true }).then(function(stream) { videoElement.src = window.URL.createObjectURL(stream); videoElement.play(); }); } else { document.getElementById("pnlVideo1").style.display = "none"; } //// Trigger photo take document.getElementById("snap").addEventListener("click", function() { contextPreview.drawImage(videoElement, 0, 0, 300, 224); contextUpload.drawImage(videoElement, 0, 0, 300, 224); document.getElementById("video").style.display = "none"; document.getElementById("snap").style.display = "none"; document.getElementById("canvasPreview").style.display = "block"; var image = document.getElementById("canvasUpload").toDataURL("image/jpeg"); image = image.replace('data:image/jpeg;base64,', ''); $("#imgValue").val(image); alert("image value :" + image); }); //// Trigger photo take document.getElementById("btnOpen1").addEventListener("click", function() { document.getElementById("vdoOne").style.display = "block"; document.getElementById("video").style.display = "block"; document.getElementById("snap").style.display = "block"; document.getElementById("canvasPreview").style.display = "none"; }); </script>如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
今天,GoogleChromeBeta版的更新带来了另一个HTML5特性:它现在可以无需插件便能访问计算机上的摄像头和麦克风。新功能利用HTML5的getUse
帝国CMS7.5版视频播放器更新,做视频站更方便:1、系统模型的播放器新增HTML5的video播放标签、HTML5的Audio播放标签、jwplayer播放器
做HTML5的video标签,本身我们有下载功能,但是在video区域内,点击右键可以将“视频另存为”,来下载视频,为了屏蔽&ldquo
AppleWatch系列都支持摄像头功能。AppleWatch是通过连接iPhone手机来实现拍照功能的,连接iPhone手机,无论是在锁屏状态还是打开状态,都
我们都是习惯了windowsXP在我的电脑里面硬盘符下面有一个摄像头快捷方式,通过此可快捷启动摄像头功能的使用习惯,然而最新的windows7系统却找不到摄像头