How to make an snapshot from a MJPEG stream in HTML

17.8k Views Asked by At

I have the following HTML web page:

<html>
<body>
<IMG SRC='http://85.46.64.155/axis-cgi/mjpg/video.cgi'>
</body>
</html>

This web page displays the feed of an IP camera streaming MJPEG data. You can try the above code here: http://jsfiddle.net/jU4aq/ (it doesn't work from IE)

My question is how I can make a snapshot of that feed. Basically I want to add a button that when the user clicks on it, a dialog will pop up and will give you the option to save the image.

4

There are 4 best solutions below

5
On

Your stream doesn't seem to be working right now but try a bit of canvas javascript, like:

<html>
<body>
<IMG id="myImage" SRC='http://85.46.64.155/axis-cgi/mjpg/video.cgi'>

<input type="button" id="save" value="Save to PNG"> 

<script type="text/javascript">
document.getElementById('save').onclick = function () {

var c = document.createElement('canvas');
var img = document.getElementById('myImage');
c.width = img.width;
c.height = img.height;
var ctx = c.getContext('2d');


ctx.drawImage(img, 0, 0);
//window.location = c.toDataURL('image/png');
window.open(c.toDataURL('image/png'))
};

</script>

</body>
</html>
0
On

Some IP cameras have a path for snapshots. For example, Vivotek's runs at "/cgi-bin/viewer/video.jpg?streamid=0".

You could setup a HTML button which triggers a JS event that will create an IMG DOMelement with that URL as "src" attribute. But you will probably need to get around cross-domain issues http://en.wikipedia.org/wiki/Same_origin_policy.

The solution I have seen the most is to use a server-side language such as php, node, python, ruby, etc, to download the snapshot and save it as a public file for your web page.

0
On

Use AXIS api to get snapshot, you can get it here: http://www.axis.com/techsup/cam_servers/dev/cam_http_api_index.php

In you case URL is "http://your.server/axis-cgi/jpg/image.cgi"

Also you can use additional parameters, such as resolution and compression

0
On

A slightly modified answer from Simon Sarris worked for me:

<img id="snapshot" src="mjpeg_stream_url" />

document.getElementById('snapshot').onclick = function () {
    const canvas = document.createElement('canvas');
    const img = document.getElementById('snapshot');
    canvas.width = img.naturalWidth;
    canvas.height = img.naturalHeight;
    const ctx = canvas.getContext('2d');
    ctx.drawImage(img, 0, 0);
    const link = document.createElement('a');
    link.download = 'snapshot.png';
    link.href = canvas.toDataURL('image/png');
    link.click();
};