{"id":304,"date":"2014-07-05T01:57:20","date_gmt":"2014-07-05T05:57:20","guid":{"rendered":"http:\/\/fuel-efficient-vehicles.org\/pwsdb\/?p=304"},"modified":"2016-09-12T02:35:39","modified_gmt":"2016-09-12T06:35:39","slug":"take-visitor-jpg-photo-with-html5-no-plugins","status":"publish","type":"post","link":"https:\/\/fuel-efficient-vehicles.org\/pwsdb\/?p=304","title":{"rendered":"TAKE jpg webcam PHOTO with HTML5"},"content":{"rendered":"<p>updated 2016-03-03<\/p>\n<h3 align=center> How to take-upload your visitors webcam photo <\/h3>\n<h4 align=center> How to upload a jpg photo on your website<br \/>\n                            with your visitors webcam      <\/h4>\n<p><\/p>\n<h4> html5 webcam getUserMedia examples and demo <\/h4>\n<h3> How to take a jpg webcam photo with html5  <\/h3>\n<p>One of the really cool things that has come from HTML5 is the ability to access a website visitor&#8217;s webcam and upload a snapshot through the browser &#8211; with the visitors permission.  <\/p>\n<p>      <center>  <\/p>\n<h5>  example <\/h5>\n<p><\/p>\n<div class=\"photo\"  style=\"margin-top:-10px;\">\n<figure style=\"width:340px\" >    <img decoding=\"async\"  src=\"images\/posts\/html5-webcam-2014-09-26.jpg\"  title=\"HTML-5 webcam photo\"\/><figcaption> HTML-5 webcam photo <\/figcaption><\/figure>\n<\/div>\n<p>     <\/center><\/p>\n<p> &nbsp; <\/p>\n<blockquote><p>\nRemove Adobe Flash!<br \/>\n&#8220;If you have Adobe Flash on your computer, and most of you do, you are probably being spied on and Adobe does their best not to let you know or do anything about it. Fundamentally, rich video content is the drug Adobe wants you to get hooked on, but make no mistake, one of the main purposes of Flash is apparently to secretly compromise your security. &#8230; Flash has been riddled with exploitable vulnerabilities.&#8221;  <em>[since 2009]<\/em> <\/p>\n<p>See our page <a href=\"?p=167\">Flash takes over your camera and microphone and writes permanent cookies!<\/a>\n<\/p><\/blockquote>\n<p>The getUserMedia API<br \/>\nOne bad thing about it is that each browser had to rename the API instead of supporting the API as defined &#8211; so we are stuck with having an ugly multiple &#8220;if&#8221; hunting to find the right one.<\/p>\n<hr>\n<p><b>example 1.<\/b><\/p>\n<pre>\r\n&lt;script>\r\nnavigator.getUserMedia = (navigator.getUserMedia || \r\n                          navigator.webkitGetUserMedia || \r\n                          navigator.mozGetUserMedia );\r\nif (navigator.getUserMedia) \r\n {\r\n    navigator.getUserMedia(\r\n       {\r\n          video:true,\r\n          audio:false\r\n       },        \r\n       function(stream) { \/* do-say something *\/ },\r\n       function(error) { alert('Something went wrong. (error code ' + error.code + ')');\r\n            return; }\r\n    );\r\n }\r\n else {\r\n    alert(\"Sorry, the browser you are using doesn't support the HTML5 webcam API\");\r\n    return;\r\n }\r\n&lt;\/script>\r\n<\/pre>\n<p>from  http:\/\/html5hub.com\/using-the-getusermedia-api-with-the-html5-video-and-canvas-elements\/<\/p>\n<blockquote><p>\n&#8220;Audio\/Video capture has been the &#8220;Holy Grail&#8221; of web development for a long time. For many years we&#8217;ve had to rely on browser plugins (Flash or Silverlight) to get the job done.&#8221;<\/p>\n<p>&#8220;HTML5 to the rescue. It might not be apparent, but the rise of HTML5 has brought a surge of access to device hardware. Geolocation (GPS), the Orientation API (accelerometer), WebGL (GPU), and the Web Audio API (audio hardware) are perfect examples. These features are ridiculously powerful, exposing high level JavaScript APIs that sit on top of the system&#8217;s underlying hardware capabilities.&#8221;<\/p>\n<p>&#8220;getUserMedia() has been supported since Chrome 21, Opera 18, and Firefox 17.&#8221; <em>[also Opera Mobile for Android devices]<\/em><\/p>\n<p>http:\/\/www.html5rocks.com\/en\/tutorials\/getusermedia\/intro\/\n<\/p><\/blockquote>\n<p>So far, IE and Safari don&#8217;t support getUserMedia. &#8211; almost all other browsers do.<\/p>\n<p> <img width=325 height=129 src=images\/posts\/html5-webcam-Firefox-allow-prompt.jpg><br \/>\n<br \/>\n <img width=550 height=60 src=images\/posts\/html5-webcam-Chrome-allow-prompt.png><br \/>\n<\/p>\n<p> <img width=540 height=75 src=images\/posts\/html5-webcam-android-allow.jpg><br \/>\n<\/p>\n<p>When the webcam is called the user will be asked for permission (as in the 3 images above) letting you Allow (Chrome and android) access to your camera or to &#8220;Share Selected Device&#8221;(Firefox). This is a very important privacy feature which stops websites from being able to spy on users without their knowledge. <em>[as is being done in Flash!]<\/em><\/p>\n<p> &nbsp; <\/p>\n<hr>\n<p><b> example 2. <\/b><\/p>\n<h3 align=center>  A Complete example to get a snapshot<br \/>\n                        and upload it to the server      <\/h3>\n<p>Pieced together from a variety of google searches and then experimenting till I got it to work.<\/p>\n<p>captures a picture from webcam and submits it in a form.<\/p>\n<p><b> Part 1.  a few CSS styles: <\/b><\/p>\n<pre>\r\n &lt;style>\r\n.camcontent{\r\n  display: block;\r\n  position: relative;\r\n  overflow: hidden;\r\n  height: 480px;\r\n  margin: auto;\r\n  }\r\n.cambuttons button {\r\n  border-radius: 15px;\r\n  font-size: 18px;\r\n  }\r\n.cambuttons button:hover {\r\n  cursor: pointer;\r\n  border-radius: 15px;\r\n  background: #00dd00 ;    \/* green *\/ \r\n  }\r\n &lt;\/style>\r\n<\/pre>\n<p> &nbsp; <\/p>\n<hr>\n<p> <b> Part 2.  the Javascript and HTML5: <\/b><\/p>\n<p>Note:  A small google script is used to avoid &#8220;reinventing the wheel&#8221;. <\/p>\n<pre>\r\n&lt;script src=\"\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.8.3\/jquery.min.js\">&lt;\/script>\r\n&lt;script>\r\n        \/\/ Put event listeners into place\r\n        window.addEventListener(\"DOMContentLoaded\", function() {\r\n            \/\/ Grab elements, create settings, etc.\r\n            var canvas = document.getElementById(\"canvas\"),\r\n                context = canvas.getContext(\"2d\"),\r\n                video = document.getElementById(\"video\"),\r\n                videoObj = { \"video\": true },\r\n                image_format= \"jpeg\",\r\n                jpeg_quality= 85,\r\n                errBack = function(error) {\r\n                    console.log(\"Video capture error: \", error.code); \r\n                };\r\n\r\n\r\n            \/\/ Put video listeners into place\r\n            if(navigator.getUserMedia) { \/\/ Standard\r\n                navigator.getUserMedia(videoObj, function(stream) {\r\n                    video.src = stream;\r\n                    video.play();\r\n                    $(\"#snap\").show();\r\n                }, errBack);\r\n            } else if(navigator.webkitGetUserMedia) { \/\/ WebKit-prefixed\r\n                navigator.webkitGetUserMedia(videoObj, function(stream){\r\n                    video.src = window.webkitURL.createObjectURL(stream);\r\n                    video.play();\r\n                    $(\"#snap\").show();\r\n                }, errBack);\r\n            } else if(navigator.mozGetUserMedia) { \/\/ moz-prefixed\r\n                navigator.mozGetUserMedia(videoObj, function(stream){\r\n                    video.src = window.URL.createObjectURL(stream);\r\n                    video.play();\r\n                    $(\"#snap\").show();\r\n                }, errBack);\r\n            }\r\n                  \/\/ video.play();       these 2 lines must be repeated above 3 times\r\n                  \/\/ $(\"#snap\").show();  rather than here once, to keep \"capture\" hidden\r\n                  \/\/                     until after the webcam has been activated.  \r\n\r\n            \/\/ Get-Save Snapshot - image \r\n            document.getElementById(\"snap\").addEventListener(\"click\", function() {\r\n                context.drawImage(video, 0, 0, 640, 480);\r\n                \/\/ the fade only works on firefox?\r\n                $(\"#video\").fadeOut(\"slow\");\r\n                $(\"#canvas\").fadeIn(\"slow\");\r\n                $(\"#snap\").hide();\r\n                $(\"#reset\").show();\r\n                $(\"#upload\").show();\r\n            });\r\n            \/\/ reset - clear - to Capture New Photo\r\n            document.getElementById(\"reset\").addEventListener(\"click\", function() {\r\n                $(\"#video\").fadeIn(\"slow\");\r\n                $(\"#canvas\").fadeOut(\"slow\");\r\n                $(\"#snap\").show();\r\n                $(\"#reset\").hide();\r\n                $(\"#upload\").hide();\r\n            });\r\n            \/\/ Upload image to sever \r\n            document.getElementById(\"upload\").addEventListener(\"click\", function(){\r\n                var dataUrl = canvas.toDataURL(\"image\/jpeg\", 0.85);\r\n                $(\"#uploading\").show();\r\n                $.ajax({\r\n                  type: \"POST\",\r\n                  url: \"html5-webcam-save.php\",\r\n                  data: { \r\n                     imgBase64: dataUrl,\r\n                     user: \"Joe\",      <span style=\"color:red\"> * <\/span>\r\n                     userid: 25        <span style=\"color:red\"> * <\/span>\r\n                  }\r\n                }).done(function(msg) {\r\n                  console.log(\"saved\");\r\n                  $(\"#uploading\").hide();\r\n                  $(\"#uploaded\").show();\r\n                });\r\n            });\r\n        }, false);\r\n\r\n   &lt;\/script>\r\n\r\n\r\n&lt;div class=\"camcontent\">\r\n    &lt;video id=\"video\" autoplay>&lt;\/video>\r\n    &lt;canvas id=\"canvas\" width=\"640\" height=\"480\"><\/canvas>\r\n&lt;\/div>\r\n&lt;div class=\"cambuttons\">\r\n    &lt;button id=\"snap\" style=\"display:none;\">  Capture &lt;\/button> \r\n    &lt;button id=\"reset\" style=\"display:none;\">  Reset  &lt;\/button> &nbsp; &nbsp; \r\n    &lt;button id=\"upload\" style=\"display:none;\"> Upload &lt;\/button> \r\n    &lt;br> &lt;span id=uploading style=\"display:none;\"> Uploading has begun . . .  &lt;\/span> \r\n         &lt;span id=uploaded  style=\"display:none;\"> Success, your photo has been uploaded! \r\n              &nbsp; &lt;a href=\"javascript:history.go(-1)\"> Return &lt;\/a> &lt;\/span> \r\n &lt;\/div>\r\n<\/pre>\n<p>Above, there are 2 or 3 lines which you may need to change to fit your situation:<br \/>\n<em class=smaller> (of course, you can change a lot &#8211; especially the css and html) <\/em><\/p>\n<p>1. the name of the program ajax calls and sends the image and other data to is named &#8220;html5-webcam-save.php&#8221; in this example and the example code for it is in &#8220;Part 3.&#8221; below.   Realize that the name of your program is arbitrary and if you write programs using another language (not php), that is your option also.  <\/p>\n<p>2. in addition, you can send other information along with the image.<br \/>\n  &#8220;imgBase64: dataUrl&#8221;<br \/>\nis required but the examples  &emsp;  <span style=\"color:red\"> * <\/span><br \/>\n  user: &#8220;Joe&#8221;,<br \/>\n  userid: 25<br \/>\nare not likely useful.  You will want to put in variables that identify the person. like for instance:<br \/>\n  userid: <tt> \"&lt;?php echo $USER->id ; ?>\" <\/tt><br \/>\nThis php example is one where the visitor\/user has logged in to some website program and their user-id is being stored in memory, in a variable &#8220;$USER->id&#8221;.   If it is a number, the quotes are ok but not required, if it contains characters other than numbers, the quotes are required. <\/p>\n<p> &nbsp; <\/p>\n<hr>\n<p> <b> Part 3.  the &#8220;html5-webcam-save.php&#8221;<br \/>\n&emsp; &emsp; &emsp;  to save the .jpg photo to a folder <em class=smaller> (ex: images\/) <\/em> using php: <\/b><br \/>\nBreak up a string on the commas (&#8220;explode&#8221;), convert (decode) the jpg image, name and save the image.<br \/>\n(and maybe update a user record in your database?) <\/p>\n<pre>\r\n&lt;?php\r\n\r\n$rawData = $_POST['imgBase64'];\r\n$filteredData = explode(',', $rawData);\r\n$unencoded = base64_decode($filteredData[1]);\r\n\r\n$datime = date(\"Y-m-d-H.i.s\", time() ) ; # - 3600*7\r\n\r\n$userid  = $_POST['userid'] ;\r\n\r\n\/\/ name & save the image file \r\n$fp = fopen('images\/'.$datime.'-'.$userid.'.jpg', 'w');\r\nfwrite($fp, $unencoded);\r\nfclose($fp);\r\n\r\n <em class=smaller> \/\/ next, update or insert a users record <\/em> \r\n<\/pre>\n<p> &nbsp; <\/p>\n<hr>\n<p> <b> part 4.  extra:  How to retrieve and view the collection<br \/>\n&emsp; &emsp; &emsp;  of photos saved in the folder &#8220;images&#8221; with php <\/b><\/p>\n<pre>\r\n&lt;?php\r\n\r\n    $filelist = opendir('images') ;\r\n    $photos = array();\r\n\r\n    while ($campic = readdir($filelist)) \r\n        {\r\n        if (strpos($campic, '.jpg') !== false  ) \r\n           { $photos[] = $campic; }\r\n        }\r\n    closedir($filelist);\r\n    rsort($photos);  # to display the most recent photos first\r\n\r\n    foreach ($photos AS $photo ) \r\n        { echo  ' &lt;img width=640 height=480 src=\"images\/'.$photo.'\"> \r\n                  &lt;br> '.$photo.'&lt;br> &lt;br> &lt;br>' ; }\r\n\r\n<\/pre>\n<p> &nbsp; <\/p>\n<p> &nbsp; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>updated 2016-03-03 How to take-upload your visitors webcam photo How to upload a jpg photo on your website with your visitors webcam html5 webcam getUserMedia examples and demo How to take a jpg webcam photo with html5 One of the really cool things that has come from HTML5 is the ability to access a website visitor&#8217;s webcam and upload a snapshot through the browser &#8211; with the visitors permission. example HTML-5 webcam photo &nbsp; Remove Adobe Flash! &#8220;If you have Adobe Flash on your computer, and most of you do, you are probably being spied on and Adobe does their best not to let you know or do anything about it. Fundamentally, rich video content is the drug Adobe wants you to get hooked on, but make no mistake, one of the main purposes of Flash is apparently to secretly compromise your security. &#8230; Flash has been riddled with exploitable vulnerabilities.&#8221; [since 2009] See our page Flash takes over your camera and microphone and writes permanent cookies! The getUserMedia API One bad thing about it is that each browser had to rename the API instead of supporting the API as defined &#8211; so we are stuck with having an ugly [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,3],"tags":[],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-html5","category-php"],"_links":{"self":[{"href":"https:\/\/fuel-efficient-vehicles.org\/pwsdb\/index.php?rest_route=\/wp\/v2\/posts\/304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fuel-efficient-vehicles.org\/pwsdb\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fuel-efficient-vehicles.org\/pwsdb\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fuel-efficient-vehicles.org\/pwsdb\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fuel-efficient-vehicles.org\/pwsdb\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304"}],"version-history":[{"count":0,"href":"https:\/\/fuel-efficient-vehicles.org\/pwsdb\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions"}],"wp:attachment":[{"href":"https:\/\/fuel-efficient-vehicles.org\/pwsdb\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fuel-efficient-vehicles.org\/pwsdb\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fuel-efficient-vehicles.org\/pwsdb\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}