这篇文章主要介绍了js+HTML5基于过滤器从摄像头中捕获视频的方法,涉及javascript基于html5元素操作多媒体的使用技巧,需要的朋友可以参考下
本文实例讲述了js+HTML5基于过滤器从摄像头中捕获视频的方法。分享给大家供大家参考。具体如下:
index.html页面:
<p class=warning>
<h2>Native web camera streaming (getUserMedia) is not supported in this browser.</h2>
</p>
<p class=container>
<h3>Current filter is: None</h3>
<button>Click here to change video filter</button>
<p style=clear:both></p>
<p class=col>
<h2>HTML5 <video> object</h2>
<video></video>
</p>
<p class=col>
<h2>HTML5 <canvas> object</h2>
<canvas width=600 height=450></canvas>
</p>
</p>
style.css文件:
.grayscale{
-webkit-filter:grayscale(1);
-moz-filter:grayscale(1);
-o-filter:grayscale(1);
-ms-filter:grayscale(1);
filter:grayscale(1);
}
.sepia{
-webkit-filter:sepia(0.8);
-moz-filter:sepia(0.8);
-o-filter:sepia(0.8);
-ms-filter:sepia(0.8);
filter:sepia(0.8);
}
.blur{
-webkit-filter:blur(3px);
-moz-filter:blur(3px);
-o-filter:blur(3px);
-ms-filter:blur(3px);
filter:blur(3px);
}
.brightness{
-webkit-filter:brightness(0.3);
-moz-filter:brightness(0.3);
-o-filter:brightness(0.3);
-ms-filter:brightness(0.3);
filter:brightness(0.3);
}
.contrast{
-webkit-filter:contrast(0.5);
-moz-filter:contrast(0.5);
-o-filter:contrast(0.5);
-ms-filter:contrast(0.5);
filter:contrast(0.5);
}
.hue-rotate{
-webkit-filter:hue-rotate(90deg);
-moz-filter:hue-rotate(90deg);
-o-filter:hue-rotate(90deg);
-ms-filter:hue-rotate(90deg);
filter:hue-rotate(90deg);
}
.hue-rotate2{
-webkit-filter:hue-rotate(180deg);
-moz-filter:hue-rotate(180deg);
-o-filter:hue-rotate(180deg);
-ms-filter:hue-rotate(180deg);
filter:hue-rotate(180deg);
}
.hue-rotate3{
-webkit-filter:hue-rotate(270deg);
-moz-filter:hue-rotate(270deg);
-o-filter:hue-rotate(270deg);
-ms-filter:hue-rotate(270deg);
filter:hue-rotate(270deg);
}
.saturate{
-webkit-filter:saturate(10);
-moz-filter:saturate(10);
-o-filter:saturate(10);
-ms-filter:saturate(10);
filter:saturate(10);
}
.invert{
-webkit-filter:invert(1);
-moz-filter:invert(1);
-o-filter: invert(1);
-ms-filter: invert(1);
filter: invert(1);
}
script.js 文件:
// Main initialization
document.addEventListener('DOMContentLoaded', function() {
// Global variables
var video = document.querySelector('video');
var audio, audioType;
var canvas = document.querySelector('canvas');
var context = canvas.getContext('2d');
// Custom video filters
var iFilter = 0;
var filters = [
'grayscale',
'sepia',
'blur',
'brightness',
'contrast',
'hue-rotate',
'hue-rotate2',
'hue-rotate3',
'saturate',
'invert',
'none'
];
// Get the video stream from the camera with getUserMedia
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
if (navigator.getUserMedia) {
// Evoke getUserMedia function
navigator.getUserMedia({video: true, audio: true}, onSuccessCallback, onErrorCallback);
function onSuccessCallback(stream) {
// Use the stream from the camera as the source of the video element
video.src = window.URL.createObjectURL(stream) || stream;
// Autoplay
video.play();
// HTML5 Audio
audio = new Audio();
audioType = getAudioType(audio);
if (audioType) {
audio.src = 'polaroid.' + audioType;
audio.play();
}
}
// Display an error
function onErrorCallback(e) {
var expl = 'An error occurred: [Reason: ' + e.code + ']';
console.error(expl);
alert(expl);
return;
}
} else {
document.querySelector('.container').style.visibility = 'hidden';
document.querySelector('.warning').style.visibility = 'visible';
return;
}
// Draw the video stream at the canvas object
function drawVideoAtCanvas(obj, context) {
window.setInterval(function() {
context.drawImage(obj, 0, 0);
}, 60);
}
// The canPlayType() function doesn't return true or false. In recognition of how complex
// formats are, the function returns a string: 'probably', 'maybe' or an empty string.
function getAudioType(element) {
if (element.canPlayType) {
if (element.canPlayType('audio/mp4; codecs=mp4a.40.5') !== '') {
return('aac');
} else if (element.canPlayType('audio/ogg; codecs=vorbis') !== '') {
return(ogg);
}
}
return false;
}
// Add event listener for our video's Play function in order to produce video at the canvas
video.addEventListener('play', function() {
drawVideoAtCanvas(this, context);
}, false);
// Add event listener for our Button (to switch video filters)
document.querySelector('button').addEventListener('click', function() {
video.className = '';
canvas.className = '';
var effect = filters[iFilter++ % filters.length]; // Loop through the filters.
if (effect) {
video.classList.add(effect);
canvas.classList.add(effect);
document.querySelector('.container h3').innerHTML = 'Current filter is: ' + effect;
}
}, false);
}, false);
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
如何解决HTML5 虚拟键盘出现挡住输入框的问题
HTML5 Plus 实现手机APP拍照或相册选择图片上传的功能
以上就是js和HTML5基于过滤器从摄像头中捕获视频的方法的详细内容,更多请关注双恒网络其它相关文章!
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » js和HTML5基于过滤器从摄像头中捕获视频的方法
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » js和HTML5基于过滤器从摄像头中捕获视频的方法
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?