Thursday, June 25, 2015

getUserMedia Advance Features of HTML5 WebCam Kit

  1. Start and Stop Web Cam using getUserMedia HTML5 feature.

  2. <html>
  3. <head>
  4.  <meta charset="utf-8">
  5.  <title>JS Bin</title>
  6.  <script>
  7.     navigator.getUserMedia = ( navigator.getUserMedia ||
  8.                                navigator.webkitGetUserMedia ||
  9.                                navigator.mozGetUserMedia ||
  10.                                navigator.msGetUserMedia);
  11. var webcamStream;
  12.  
  13.  function startWebCam() {
  14.     if (navigator.getUserMedia) {
  15.         navigator.getUserMedia (
  16.             // constraints
  17.             {
  18.                 video: true,
  19.                 audio: false
  20.             },
  21.  
  22.             // successCallback
  23.             function(localMediaStream) {
  24.                 var video = document.querySelector('video');
  25.                 video.src = window.URL.createObjectURL(localMediaStream);
  26.                 webcamStream = localmediaStream;
  27.             },
  28.  
  29.             // errorCallback
  30.             function(err) {
  31.                 console.log("The following error occurred: " + err);
  32.             }
  33.         );
  34.     } else {
  35.         console.log("getUserMedia not supported");
  36.     }
  37. }
  38. function stopWebcam() {
  39.     webcamStream.stop();
  40. }
  41.  </script>
  42. </head>
  43. <body >
  44.      <video width=200 height=200 id="video" controls autoplay></video>
  45.      <button onclick="startWebcam();">Start Webcam</button>
  46.      <button onclick="stopWebcam();">Stop Webcam</button>
  47. </body>
  48. </html>

Web Cam Enable Application example

  1. <html>
  2. <head>
  3.  <meta charset="utf-8">
  4.  <title>JS Bin</title>
  5.  <script>
  6.     navigator.getUserMedia = ( navigator.getUserMedia ||
  7.                                navigator.webkitGetUserMedia ||
  8.                                navigator.mozGetUserMedia ||
  9.                                navigator.msGetUserMedia); 
  10.     if (navigator.getUserMedia) {
  11.         navigator.getUserMedia (
  12.             // constraints
  13.             {
  14.                 video: true,
  15.                 audio: false
  16.             },
  17.  
  18.             // successCallback
  19.             function(localMediaStream) {
  20.                 var video = document.querySelector('video');
  21.                 video.src = window.URL.createObjectURL(localMediaStream);
  22.             },
  23.  
  24.             // errorCallback
  25.             function(err) {
  26.                 console.log("The following error occured: " + err);
  27.             }
  28.         );
  29.     } else {
  30.         console.log("getUserMedia not supported");
  31.     }
  32.  </script>
  33. </head>
  34. <body >
  35.      <video width=200 height=200 id="video" controls autoplay></video>
  36. </body>
  37. </html>