Alessio Caiazza is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

nolith / CaptureMJPEG http://capturemjpeg.lilik.it/

CaptureMJPEG, a processing library for Motion JPEG stream. CaptureMJPEG is released under the LGPLv3 license.

Clone this repository (size: 3.3 MB): HTTPS / SSH
hg clone https://bitbucket.org/nolith/capturemjpeg
hg clone ssh://hg@bitbucket.org/nolith/capturemjpeg
hg clone https://bitbucket.org/nolith/capturemjpeg/wiki

Examples

Here you can find some examples of usage of the CaptureMJPEG library.
If you don't know what it is, see Getting Started.

Basic usage

If you use the generic URL parser, you have to specify the full path of your video camera, including "http://".

import it.lilik.capturemjpeg.*;

private CaptureMJPEG capture;
private PImage next_img = null;

void setup() {
  size(400, 300);
  background(0);
  capture = new CaptureMJPEG (this,
                              "http://mynetworkcamera.foo/image?speed=20",
                              "user",
                              "password");
  // or this if you don't need auth
  // capture = new CaptureMJPEG(this, "http://mynetworkcamera.foo/image?speed=20");

  capture.startCapture();
  frameRate(20);
}

void draw() {
  if (next_img != null) {
    image(next_img, 0, 0);
  }
}

void captureMJPEGEvent(PImage img) {
  next_img = img;
}

Usage of vendor-specific URL builders

Note that when using vendor-specific URL builder, you must provide only the host name to the constructor (without "http://").

import it.lilik.capturemjpeg.*;

private CaptureMJPEG capture;
private PImage next_img = null;

void setup() {
  size(400, 300);
  background(0);
  capture = new CaptureMJPEG (this,
                              new SonyURL("mynetworkcamera.foo")
                              .setFPS(20).getURL(),
                            //new AxisURL("mynetworkcamera.foo")
                            //.setDesiredFPS(20).getURL(),
                              "user",
                              "password");
  capture.startCapture();
  frameRate(20);
}

void draw() {
  if (next_img != null) {
    image(next_img, 0, 0);
  }
}

void captureMJPEGEvent(PImage img) {
  next_img = img;
}

Usage of the buffer facility

In the previous examples, you can see that we're defining a captureMJPEGEvent () function.
This function is called from within CaptureMJPEG every time a new image is ready to be displayed.
You may prefer to avoid defining this function and choose yourself when to display the images.
CaptureMJPEG has an internal circular buffer to store ready images for this purpose.

import it.lilik.capturemjpeg.*;

private CaptureMJPEG capture;
private PImage next_img = null;

void setup() {
  size (400,300);
  capture = new CaptureMJPEG (this,
                              "http://mynetworkcamera.foo/image?speed=20",
                              "user",
                              "password");
  capture.startCapture();
  frameRate(20);
}
            
void draw() {
  next_img = capture.getImage();
  if (next_img != null) {
    image(next_img, 0, 0);
  }
}           

void stop()
{
  capture.dispose();
}

Adaptive frame size

We also provide a way to dynamically resize the applet according to the image size.
This is especially useful when you want to build a simple viewer without caring of the original image size.

import it.lilik.capturemjpeg.*;

private CaptureMJPEG capture;
private PImage next_img = null;

void setup() {
  frame.setResizable (true);
  background(0);
  capture = new CaptureMJPEG (this,
                              "http://mynetworkcamera.foo/image?speed=20",
                              "user",
                              "password");
  capture.setAdaptFrameSize(true);
  capture.startCapture();
  frameRate(20);
}

void draw() {
  if (next_img != null) {
    image(next_img, 0, 0);
  }
}

void captureMJPEGEvent(PImage img) {
  next_img = img;
}

This revision is from 2009-06-06 09:36