6

Recently I've been attempting to create a photo booth by relying on Webrtc and have nearly completed all of the code except I've not been able to figure out a way to save the image after it has been captured.

This is the closest I've come so far to achieving my goal:

 <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>fotogoof</title>
    <link rel="Stylesheet" href="css/bootstrap.css"/>
    <link rel="Stylesheet" href="css/style.css"/>
    <script type="text/javascript">
            window.onload = function () {
                var img = document.getElementById('screenshot');
                var button = document.getElementById('saveImage');
                img.src = '';
                img.onload = function () {
                    button.removeAttribute('disabled');
                };
                button.onclick = function () {
                    window.location.href = img.src.replace('image/png', 'image/octet-stream');
                };
            };
    </script>
    </head>
    <div class="navbar navbar-inverse navbar-fixed-top">
    <div class="navbar-inner">
      <div class="container">
        <h2 class="brand">Html5 Photobooth</h1>
      </div>
    </div>
    </div>
    <div class="container" id="body-wrap">
    <div class="container" id="main">
      <div class="span10">
        <div id="video-container">
        <canvas width="400" height="320" class="mask"></canvas>
          <div id="counter">
          </div>
        </div>  
        <br><div id="pic-holder" class="pull-left"><img id="screenshot"></div></br>
        <input id="saveImage" type="button" value="save image" disabled="disabled"/>
      </div>
    </div>
   </div>   
    </div>

The code is essentially taking the webcam stream and feeding it into a canvas element. Pressing the space button triggers a screenshot to be taken, which then appears as an image. Because I'm unable to provide the exact source of the file that is being downloaded the button only opens the image in another tab in the browser instead of functioning properly. I would really appreciate some input as to how I can resolve this. Thanks in advance.

I've already managed to do capture the stream from the canvas in an image with this bit of code:

document.addEventListener ('keydown', function (event) {
        if(event.keyCode == 32) {
         document.querySelector('#screenshot').src = canvas.toDataURL('image/webp')
        }
})

What I want to know is how to enable the user to save the image onto their computer from there.

user2724072
  • 135
  • 1
  • 1
  • 12

4 Answers4

17

If I understand correctly you want to download (ie. provide a save dialog for the user to pick a location) the image to user's machine?'

If so you can use this snippet:

function download(canvas, filename) {

    /// create an "off-screen" anchor tag
    var lnk = document.createElement('a'),
        e;

    /// the key here is to set the download attribute of the a tag
    lnk.download = filename;

    /// convert canvas content to data-uri for link. When download
    /// attribute is set the content pointed to by link will be
    /// pushed as "download" in HTML5 capable browsers
    lnk.href = c.toDataURL();

    /// create a "fake" click-event to trigger the download
    if (document.createEvent) {

        e = document.createEvent("MouseEvents");
        e.initMouseEvent("click", true, true, window,
                         0, 0, 0, 0, 0, false, false, false,
                         false, 0, null);

        lnk.dispatchEvent(e);

    } else if (lnk.fireEvent) {

        lnk.fireEvent("onclick");
    }
}

Now all you need to do is to provide a default file name and call the function:

download(myCanvas, 'untitled.png');

If you rather than this mean save directly to user's harddisk then you can't due to security reasons.

That being said, there is always the option of using local storage such as File API or Indexed DB - but as these are sand-boxed you and the user will only have access to the "files" through the browser so perhaps not so convenient.

  • +1 Good answer. Given that canvas is a great medium for presenting both data and entertainment, it seems the browser makers really should provide a more direct canvas-to-local-disk save method--maybe something that satisfies security concerns by requiring the user to click OK in a warning dialog box. – markE Aug 28 '13 at 15:07
  • Thanks @markE. I agree, a feature like "allow this site to save to [this] location" would be great. –  Aug 28 '13 at 16:53
  • Sorry, I'm a little confused. Does the "download(myCanvas, 'untitled.png');" portion of the code go right after the "function download(canvas, filename)" portion? – user2724072 Sep 01 '13 at 18:52
  • @user2724072 function ... just defines the function and will make it available from the same scope it was defined so you can call download(..) from anywhere in your code as long as it can access the scope the function was defined int. Ie. if you put function download() in global scope you can call download() from anywhere at anytime. JavaScript parses function definitions before starting executing any code. Or put this way: Just call it when you need it as long as function is defined it does not matter if the calls come before or after. –  Sep 01 '13 at 19:07
  • I'm using [this similar code](http://muaz-khan.blogspot.com/2012/10/save-files-on-disk-using-javascript-or.html) to save a YouTube video. It works on Chrome, saving the file with the custom file name, but on Firefox 30.0, it downloads the video with the default file name "videoplayback", not the custom file name. Has anyone solved such an issue? Thank you. – frank Jun 16 '14 at 17:01
0

I don't think it's possible for IE without involving a server side script. Here's a good writeup on how to do it that way.

PEZ
  • 16,821
  • 7
  • 45
  • 66
0

I had this problem and this is the best solution without any external or additional script libraries: In Javascript tags or file create this function: We assume here that canvas is your canvas:

function download(){
        var download = document.getElementById("download");
        var image = document.getElementById("canvas").toDataURL("image/png")
                    .replace("image/png", "image/octet-stream");
        download.setAttribute("href", image);

    }

In the body part of your HTML specify the button:

<a id="download" download="image.png"><button type="button" onClick="download()">Download</button></a>

This is working and download link looks like a button.

Plcode
  • 223
  • 4
  • 8
-1

Regarding the first task, you can export the canvas content to an image with toDataUrl method supported by the canvas object.

var canvas = document.getElementById("canvas");
if (canvas.getContext) {
    var ctx = canvas.getContext("2d");                // Get the context for the canvas.
    var myImage = canvas.toDataURL("image/png");      // Get the data as an image.
}

var image = document.getElementById("screenshot");  // Get the image object.
image.src = myImage; 
Roy M J
  • 6,926
  • 7
  • 51
  • 78