Advertisement
AyrA

Simple HTML5 Camera Photo Capture

Aug 24th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.03 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>Test</title>
  5.     </head>
  6.     <body>
  7.         <video autoplay></video><br />
  8.         <input type="button" value="snapshot" /><br />
  9.         <canvas></canvas>
  10.         <script type="text/javascript">
  11.             (function () {
  12.                 //Video only
  13.                 var constraints = {
  14.                     video: true
  15.                 };
  16.                 //Video element
  17.                 var video = document.querySelector('video');
  18.                 //Get camera
  19.                 navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
  20.                     //assign stream to video element on success
  21.                     video.srcObject = stream;
  22.                 });
  23.                 //Button click
  24.                 document.querySelector("[type=button]").addEventListener("click", function () {
  25.                     //Get empty canvas element
  26.                     var canvas = document.querySelector("canvas");
  27.                     //Set to video dimensions
  28.                     canvas.width = video.clientWidth;
  29.                     canvas.height = video.clientHeight;
  30.                     //Grab snapshot and render it
  31.                     var ctx = canvas.getContext("2d");
  32.                     ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
  33.                 });
  34.             })();
  35.         </script>
  36.     </body>
  37. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement