js+HTML5基于过滤器从摄像头中捕获视频的方法

   本文实例讲述了js+HTML5基于过滤器从摄像头中捕获视频的方法。分享给大家供大家参考。具体如下:

  index.html页面:

  ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

<div class="warning">
<h2>Native web camera streaming (getUserMedia) is not supported in this browser.</h2>
</div>
<div class="container">
<h3>Current filter is: None</h3>
<button>Click here to change video filter</button>
<div style="clear:both"></div>
<div class="col">
<h2>HTML5 <video> object</h2>
<video></video>
</div>
<div class="col">
<h2>HTML5 <canvas> object</h2>
<canvas width="600" height="450"></canvas>
</div>
</div>

  style.css文件:

  ?

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

.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 文件:

  ?

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

// 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);

  希望本文所述对大家的javascript程序设计有所帮助。

时间: 2024-08-07 05:53:29

js+HTML5基于过滤器从摄像头中捕获视频的方法的相关文章

js+HTML5基于过滤器从摄像头中捕获视频的方法_javascript技巧

本文实例讲述了js+HTML5基于过滤器从摄像头中捕获视频的方法.分享给大家供大家参考.具体如下: index.html页面: <div class="warning"> <h2>Native web camera streaming (getUserMedia) is not supported in this browser.</h2> </div> <div class="container"> &l

在PPT中插入视频的方法

  在制作PPT时为了更加生动地表示所要表达的主题,常常需要借助视频来实现,下面我们介绍在PowerPoint 2007中插入视频的两种方法. 直接插入法 这是最简单方法.用该法插入的视频,在演示界面中仅显示视频画面,和插入图片十分类似.可以说,这是一种无缝插入,效果相当不错,但同时局限性也很大.首先,该法仅支持插入AVI.MPEG和WMV等Windows Media格式视频,而像RMVB等其它格式均不支持,不能不令人遗憾.其次,用该法插入的视频,演示时只能实现暂停和播放控制,若想自由选择播放时

.net中捕获摄像头视频的方式及对比(How to Capture Camera Video via .Net)

随着Windows操作系统的不断演变,用于捕获视频的API接口也在进化,微软提 供了VFW.DirectShow和MediaFoundation这三代接口.其中VFW早已被DirectShow 取代,而最新的MediaFoundation被Windows Vista和Windows 7所支持.可惜的是 ,上述接口基于COM技术且灵活性很大,在.net中并不方便直接使用. .net封装 老外有很多活雷锋,他们奉献了不少的开源项目,DirectShow.net是对 DirectShow的封装,而Me

html5中,如何能够在video中的视频加载完成后运行js代码

问题描述 html5中,如何能够在video中的视频加载完成后运行js代码 window.onload时间并没有在视频加载后,找了好久都找不到答案,求大神回答下 解决方案 https://www.douban.com/note/158621500/

JS中捕获console.log()输出的方法_javascript技巧

本文实例讲述了JS中捕获console.log()输出的方法.分享给大家供大家参考.具体分析如下: 我们知道console.log()可以将信息输出到debugger中供开发者查看.但如果我们想要在JS中获取console.log()的输出结果呢?其实不难,先将原本的console.log保存起来,然后替换成另外一个实现即可.代码如下: var lastLog; console.oldLog = console.log; console.log = function(str) { console

Laravel 中使用 Vue.js 实现基于 Ajax 的表单提交错误验证操作

本教程基于Laravel 5.4 开始之前首先准备好开发环境,我们假设你已经安装好 Laravel,至于 Vue 的引入,请参考官方文档. 做好上述准备工作后就可以开始我们的开发了,本教程中我们将演示文章发布页面的表单 验证 . 首先在 routes/web.php 中新增两条路由规则: Route::get('post/create', 'PostController@create'); Route::post('post/save', 'PostController@save'); 然后在项

浏览器中捕获和分析javascript错误捕获经

对于javascript的出错,例如xxx undefined,SyntaxError等,我们是再熟悉不过的了,本文我们来讨论关于javascript出错如何捕获. 我们team将出现错误的javascript代码取名为badjs,也有一个开源的badjs项目,用于捕获和分析js错误,并提供了一些基础的报表数据分析. 捕获错误一般有两种方式:     使用window.onerror()捕获全局的js错误信息    使用try{...}catch(e){...}包裹需要执行的代码,获取error

MSComm控件在基于单文档中的应用

本文配套源码下载 MSComm 作为一个串行通讯控件为程序员串口通讯编程节省了很多时间.在基于对话框的应用中加入一个MSComm控件非常简单.只需进行以下操作即可: 打开"Project->Add To Project->Components and Controls->Registered Activex Controls",然后选择控件:Microsoft Communication Control,version 6.0插入到当前的工程中.这样就将类 CMSCo

js跨域请求的5中解决方式

这篇文章主要介绍了js跨域请求的5中解决方式的相关资料,需要的朋友可以参考下     跨域请求数据解决方案主要有如下解决方法: ? 1 2 3 4 5 JSONP方式 表单POST方式 服务器代理 Html5的XDomainRequest Flash request 分开说明: 一.JSONP: 直观的理解: 就是在客户端动态注册一个函数 function a(data),然后将函数名传到服务器,服务器返回一个a({/*json*/})到客户端运行,这样就调用客户端的 function a(da