首页 > web前端 > H5教程 > js和HTML5基于过滤器从摄像头中捕获视频的方法

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

不言
发布: 2018-07-02 11:56:12
原创
2253 人浏览过

这篇文章主要介绍了js+HTML5基于过滤器从摄像头中捕获视频的方法,涉及javascript基于html5元素操作多媒体的使用技巧,需要的朋友可以参考下

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

index.html页面:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

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

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(&#39;DOMContentLoaded&#39;, function() {

  // Global variables

  var video = document.querySelector(&#39;video&#39;);

  var audio, audioType;

  var canvas = document.querySelector(&#39;canvas&#39;);

  var context = canvas.getContext(&#39;2d&#39;);

  // Custom video filters

  var iFilter = 0;

  var filters = [

    &#39;grayscale&#39;,

    &#39;sepia&#39;,

    &#39;blur&#39;,

    &#39;brightness&#39;,

    &#39;contrast&#39;,

    &#39;hue-rotate&#39;,

    &#39;hue-rotate2&#39;,

    &#39;hue-rotate3&#39;,

    &#39;saturate&#39;,

    &#39;invert&#39;,

    &#39;none&#39;

  ];

  // 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 = &#39;polaroid.&#39; + audioType;

        audio.play();

      }

    }

    // Display an error

    function onErrorCallback(e) {

      var expl = &#39;An error occurred: [Reason: &#39; + e.code + &#39;]&#39;;

      console.error(expl);

      alert(expl);

      return;

    }

  } else {

    document.querySelector(&#39;.container&#39;).style.visibility = &#39;hidden&#39;;

    document.querySelector(&#39;.warning&#39;).style.visibility = &#39;visible&#39;;

    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&#39;t return true or false. In recognition of how complex

  // formats are, the function returns a string: &#39;probably&#39;, &#39;maybe&#39; or an empty string.

  function getAudioType(element) {

    if (element.canPlayType) {

      if (element.canPlayType(&#39;audio/mp4; codecs="mp4a.40.5"&#39;) !== &#39;&#39;) {

        return(&#39;aac&#39;);

      } else if (element.canPlayType(&#39;audio/ogg; codecs="vorbis"&#39;) !== &#39;&#39;) {

        return("ogg");

      }

    }

    return false;

  }

  // Add event listener for our video&#39;s Play function in order to produce video at the canvas

  video.addEventListener(&#39;play&#39;, function() {

    drawVideoAtCanvas(this, context);

  }, false);

  // Add event listener for our Button (to switch video filters)

  document.querySelector(&#39;button&#39;).addEventListener(&#39;click&#39;, function() {

    video.className = &#39;&#39;;

    canvas.className = &#39;&#39;;

    var effect = filters[iFilter++ % filters.length]; // Loop through the filters.

    if (effect) {

      video.classList.add(effect);

      canvas.classList.add(effect);

      document.querySelector(&#39;.container h3&#39;).innerHTML = &#39;Current filter is: &#39; + effect;

    }

  }, false);

}, false);

登录后复制

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

如何解决HTML5 虚拟键盘出现挡住输入框的问题

HTML5 Plus 实现手机APP拍照或相册选择图片上传的功能

以上是js和HTML5基于过滤器从摄像头中捕获视频的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
javascript - js addClass 无效
来自于 1970-01-01 08:00:00
0
0
0
php调用js并获取js的返回值问题
来自于 1970-01-01 08:00:00
0
0
0
javascript - js代码转python
来自于 1970-01-01 08:00:00
0
0
0
找不到js文件代码
来自于 1970-01-01 08:00:00
0
0
0
js高级教程
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板