代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Css
】
使用 UserMedia API 访问摄像头的功能
作者:
ecalf
/ 发布于
2014/8/29
/
965
<head> <title>Camera and Video Control with HTML5 Example</title> <style> video,img { display: block; margin: 0 0 20px 0; } </style> </head> <body> <div id="page"> <div id="contentHolder"> <section class="left"> <h1>Camera and Video Control with HTML5</h1> <video id="video" autoplay=""></video> <button id="btn">Take Photo</button> <img id="photo"> <script> window.addEventListener("DOMContentLoaded", function(){ var width = 480; var photo = document.getElementById("photo"); var video = document.getElementById("video"); var canvas = document.createElement("canvas"); var context = canvas.getContext("2d"); var mediaErr = function(error) { console.log("media error: ",error); }; var mediaConf = { video: true , //寮€濮嬫憚鍍忓ご audio: true ,//寮€鍚害鍏嬮 }; //event:take photo document.getElementById("btn").addEventListener("click", function(){ context.drawImage(video, 0, 0, video.width, video.height); photo.setAttribute('src', canvas.toDataURL('image/png')); }); //event: resize video video.addEventListener('play', function(ev){ setTimeout(function(){ if(video.videoWidth){ height = video.videoHeight/(video.videoWidth/width); video.setAttribute('width', width); video.setAttribute('height', height); canvas.setAttribute('width',width); canvas.setAttribute('height',height); photo.setAttribute('width', width); photo.setAttribute('height', height); }else{ setTimeout(arguments.callee,100); } },100); }, false); // open media if(navigator.getUserMedia) { // Standard, opera navigator.getUserMedia(mediaConf, function(stream) { video.src = stream; video.play(); }, mediaErr); } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed, chrome navigator.webkitGetUserMedia(mediaConf, function(stream){ video.src = window.webkitURL.createObjectURL(stream); video.play(); }, mediaErr); }else if(navigator.mozGetUserMedia){ // firefox navigator.mozGetUserMedia(mediaConf,function(stream){ video.mozSrcObject = stream; video.play(); },mediaErr); }else{ console.log('your browser do not support UserMedia API'); } }, false); </script> </section> </div> </div></body> 跟新:加入关闭摄像头方法 <!DOCTYPE html> <html> <head> <title>Camera and Video Control with HTML5 Example</title> <style> video,img { display: block; margin: 0 0 20px 0; } </style> </head> <body> <div id="page"> <div id="contentHolder"> <!-- <div> <!-- camera--照相机;camcorder--摄像机;microphone--录音 --> <input id="uploadimg" type="file" accept="image/*" capture="camera" /> </div> --> <section class="left"> <h1>Camera and Video Control with HTML5</h1> <video id="video" autoplay ></video> <button id="btnStop" data-status='1'>stop</button> <button id="btn">Take Photo</button> <img id="photo" /> <script> document.getElementsByTagName('input')[0].addEventListener('change',function(){ var file=this.files[0]; var formatDate = function(date){ return date.getFullYear() +'-'+(date.getMonth()+1) +'-'+date.getDate() +' '+date.getHours() +':'+date.getMinutes() +':'+date.getSeconds(); }; var s = 'now: '+formatDate(new Date())+'<hr />'; for(var k in file){ if(file.hasOwnProperty(k)&&typeof(k)=='string'){ if(k=='lastModifiedDate'){ s+= k+': '+formatDate(file[k])+'<br/>'; }else{ s+= k+': '+file[k]+'<br/>'; } } } var el = document.createElement('div'); el.innerHTML = s; this.parentNode.appendChild(el); /* var reader = new FileReader(); reader.onloadend = function() { console.log('reader>>>',this); }; reader.readAsDataURL(file); */ },false); window.addEventListener("DOMContentLoaded", function(){ var width = 480; var photo = document.getElementById("photo"); var video = document.getElementById("video"); var canvas = document.createElement("canvas"); var context = canvas.getContext("2d"); var mediaErr = function(error) { console.log("media error: ",error); }; var mediaConf = { video: true , //开始摄像头 audio: false //开启麦克风 }; window.videoStream; //stop camera document.getElementById('btnStop').addEventListener('click',function(){ if(this.dataset.status==1){ videoStream.stop(); this.dataset.status=0; this.innerHTML='play'; }else{ openMedia(); this.dataset.status=1; this.innerHTML='stop'; } }); //event:take photo document.getElementById("btn").addEventListener("click", function(){ context.drawImage(video, 0, 0, video.width, video.height); photo.setAttribute('src', canvas.toDataURL('image/png')); }); //event: resize video video.addEventListener('play', function(ev){ setTimeout(function(){ if(video.videoWidth){ height = video.videoHeight/(video.videoWidth/width); video.setAttribute('width', width); video.setAttribute('height', height); canvas.setAttribute('width',width); canvas.setAttribute('height',height); photo.setAttribute('width', width); photo.setAttribute('height', height); }else{ setTimeout(arguments.callee,100); } },100); }, false); // open media function openMedia(){ if(navigator.getUserMedia) { // Standard, opera navigator.getUserMedia(mediaConf, function(stream) { videoStream = stream; video.src = stream; video.play(); }, mediaErr); } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed, chrome navigator.webkitGetUserMedia(mediaConf, function(stream){ videoStream = stream; video.src = window.webkitURL.createObjectURL(stream); video.play(); }, mediaErr); }else if(navigator.mozGetUserMedia){ // firefox navigator.mozGetUserMedia(mediaConf,function(stream){ videoStream = stream; video.mozSrcObject = stream; video.play(); },mediaErr); }else{ console.log('your browser do not support UserMedia API'); } } openMedia(); }, false); </script> </section> </div> </body> </html>
试试其它关键字
最新版本的
chrome、firefox、
opera
已经支持
同语言下
.
前端PC-移动端CSS公共样式+HTML
.
手机端页面通用样式
.
H5页面通用头部设置
.
elect默认样式美化代码兼容移动端和pc端
.
按钮效果 css
.
唤醒app
.
放大效果
.
html5+css3实现上拉和下拉刷新
.
html上传图片后,在页面显示上传的图片
.
html5实现点击弹出图片
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
ecalf
贡献的其它代码
(
12
)
.
发布网页时为js 、css 文件加上版本号
.
堆排序(2叉树)
.
希尔排序,
.
鸽巢排序,非负整数
.
桶排序,自然数,无重复值
.
基数排序(默认10进制),非负整数
.
归并排序
.
快速排序
.
选择排序
.
插入排序
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3