时间:2020-10-11来源:www.pcxitongcheng.com作者:电脑系统城
效果图: 整体效果:
视频加载:
拍照:
第一步:创建HTML元素
首先,我们要创建一个HTML5的文档。
?1 2 3 4 5 6 7 8 9 10 |
<!doctype html> < html > < head > < meta charset = "utf-8" > < title >无标题文档</ title > </ head > < body > </ body > </ html > |
然后在<body></body>
插入以下代码:
1 2 3 |
< video id = "video" width = "640" height = "480" autoplay></ video > < button id = "snap" >截图</ button > < canvas id = "canvas" width = "640" height = "480" ></ canvas > |
第二步:创建JavaScript
首先,要在<head></head>
里创建一个JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<script language= "javascript" > // Grab elements, create settings, etc. var video = document.getElementById( 'video' ); // Get access to the camera! if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) { // Not adding `{ audio: true }` since we only want video now navigator.mediaDevices.getUserMedia({ video: true }).then( function (stream) { //video.src = window.URL.createObjectURL(stream); video.srcObject = stream; video.play(); }); } /* Legacy code below: getUserMedia else if(navigator.getUserMedia) { // Standard navigator.getUserMedia({ video: true }, function(stream) { video.src = stream; video.play(); }, errBack); } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed navigator.webkitGetUserMedia({ video: true }, function(stream){ video.src = window.webkitURL.createObjectURL(stream); video.play(); }, errBack); } else if(navigator.mozGetUserMedia) { // Mozilla-prefixed navigator.mozGetUserMedia({ video: true }, function(stream){ video.srcObject = stream; video.play(); }, errBack); } */ </script> |
随后,要在刚才创建的HTML元素的后面插入以下代码:
?1 2 3 4 5 6 7 8 9 10 11 |
<script language= "javascript" > // Elements for taking the snapshot var canvas = document.getElementById( 'canvas' ); var context = canvas.getContext( '2d' ); var video = document.getElementById( 'video' ); // Trigger photo take document.getElementById( "snap" ).addEventListener( "click" , function () { context.drawImage(video, 0, 0, 640, 480); }); </script> |
现在,这个HTML就可以完成打开摄像头,和拍照的功能了!
到此这篇关于如何在HTML里加载摄像头的方法的文章就介绍到这了,更多相关html加载摄像头内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!
2023-03-11
vscode文本框怎么设置输入内容与边框的距离?2020-11-18
dns-prefetch是什么 前端优化:DNS预解析提升页面速度2020-10-11
解决搜索框和搜索按钮button边框不能重合的问题