Advertisement
cfjedimaster

Untitled

Mar 20th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.99 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  5.  
  6. <style scoped>
  7.     video { -webkit-transform: scaleX(1); }
  8.     p { text-align: center; }
  9. </style>
  10. <script>
  11. var faceKey = "9a348d36a65e0b8ca3aa010eeb1b395f";
  12. var faceSecret = "6065c6275fca70d187c0d78e2e94d472";
  13.  
  14. function init() {
  15.  
  16.     if (navigator.webkitGetUserMedia) {
  17.  
  18.         navigator.webkitGetUserMedia('video', gotStream, noStream);
  19.  
  20.         var video = document.getElementById('monitor');
  21.         var canvas = document.getElementById('photo');
  22.  
  23.         function gotStream(stream) {
  24.  
  25.             video.src = webkitURL.createObjectURL(stream);
  26.             video.onerror = function () {
  27.                 stream.stop();
  28.                 streamError();
  29.             };
  30.             document.getElementById('splash').hidden = true;
  31.             document.getElementById('app').hidden = false;
  32.             $("#snapshotbutton").click(snapshot);
  33.         }
  34.  
  35.         function noStream() {
  36.             document.getElementById('errorMessage').textContent = 'No camera available.';
  37.         }
  38.  
  39.         function streamError() {
  40.             document.getElementById('errorMessage').textContent = 'Camera error.';
  41.         }
  42.  
  43.         function snapshot() {
  44.            
  45. var file = document.getElementById('fileToUpload').files[0];
  46.          console.dir(file);
  47.              var formdata = new FormData();
  48.              formdata.append("api_key", faceKey);
  49.              formdata.append("api_secret", faceSecret);
  50.              formdata.append("filename", file.name);
  51.              formdata.append("file", file);
  52.          
  53.              $.ajax({
  54.                  url: 'http://api.face.com/faces/detect.json',
  55.                  data: formdata,
  56.                  cache: false,
  57.                  contentType: false,
  58.                  processData: false,
  59.                  type: 'POST',
  60.                  complete: function (data) {
  61.                      console.log(data);
  62.                  },
  63.                  success: function (data) {
  64.                      console.log(data);
  65.                  }
  66.              });           
  67.             canvas.width = video.videoWidth;
  68.             canvas.height = video.videoHeight;
  69.             canvas.getContext('2d').drawImage(video, 0, 0);
  70.             context = canvas.getContext("2d");
  71. //          imgdata = canvas.toDataURL("image/jpg");
  72.             imagedata = context.getImageData(0, 0, canvas.width,canvas.height);
  73.  
  74.                  var canvaspixelarray = imagedata.data;
  75.  
  76.  
  77.                  var canvaspixellen = canvaspixelarray.length;
  78.                  var bytearray = new Uint8Array(canvaspixellen);
  79. var s = "";
  80.                  for (var i=0;i<canvaspixellen;++i) {
  81.                       bytearray[i] = canvaspixelarray[i];
  82.                      s+= canvaspixelarray[i];
  83.                  }
  84.  
  85.  
  86.             console.log("ok");
  87.  
  88.             myformdata = new FormData();
  89.            myformdata.append("api_key", faceKey);
  90.            myformdata.append("api_secret", faceSecret);
  91.  
  92.            myformdata.append("filename","image.jpg");
  93.             myformdata.append("file",s);
  94.  
  95.             //begin face.com
  96.             /*
  97.             $.post("http://api.face.com/faces/detect.json?api_key="+faceKey+"&api_secret="+faceSecret+"&callback=", formdata,function(res, code) {
  98.                 console.dir(res);
  99.             });
  100.             */
  101.             $.ajax({
  102.                     url: "http://api.face.com/faces/detect.json",
  103.                     type: "POST",
  104.                     data: myformdata,
  105.                     cache: false,
  106.                     processData: false,
  107.                     contentType: false,
  108.                     complete: function(res) {
  109.                         console.dir(res);
  110.                    },
  111.                     success: function (res) {
  112.                         alert('wt');
  113.                     }
  114.                 });
  115.  
  116.         }
  117.  
  118.         function testme() {
  119.             console.log("no way");
  120.         }
  121.  
  122.     } else {
  123.         document.getElementById('errorMessage').textContent = 'No native camera support available.';
  124.     }
  125.  
  126. }
  127. </script>
  128. </head>
  129. <body onload="init()">
  130.  
  131.  
  132. <h1>Webkit WebRTC Video Element Demo</h1>
  133. <section id="splash">
  134.     <p id="errorMessage">Loading...</p>
  135. </section>
  136. <section id="app" hidden>
  137.     <p><video id="monitor" autoplay></video> <canvas id="photo"></canvas></p>
  138.     <p><input value="say cheese!" type=button value="&#x1F4F7;" id="snapshotbutton">
  139. </section>
  140.  
  141. <input type="file" id="fileToUpload">
  142.  
  143. <p>
  144. Credit: http://miernicki.com/cam.html
  145. </p>
  146.  
  147. </body>
  148. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement