ip camera streaming

3

I need to make an app to connect to an ip camera the camera I have is

  

dcs-932l - d-link

I need some tips, what to research or example. I tried with VideoURI I believe not from

    
asked by anonymous 20.12.2014 / 17:52

1 answer

6

There is an MJPEG stream acquisition processing library for IP cameras made in Java for this purpose:

ipcapture

package ipcapture;
import java.net.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import processing.core.*;

public class IPCapture extends PImage implements Runnable {
  private PApplet parent;
  private String urlString, user, pass;
  private byte[] curFrame;
  private boolean frameAvailable;
  private Thread streamReader;
  private HttpURLConnection conn;
  private BufferedInputStream httpIn;
  private ByteArrayOutputStream jpgOut;

  public final static String VERSION = "0.1.0";

  public IPCapture(PApplet parent, String urlString, String user, String pass) {
    super();
    this.parent = parent;
    parent.registerDispose(this);
    this.urlString = urlString;
    this.user = user;
    this.pass = pass;
    this.curFrame = new byte[0];
    this.frameAvailable = false;
    this.streamReader = new Thread(this, "HTTP Stream reader");
  }

  public boolean isAvailable() {
    return frameAvailable;
  }

  public void start() {
    streamReader.start();
  }

  public void stop() {
    try {
      jpgOut.close();
      httpIn.close();
    }
    catch (IOException e) {
      System.err.println("Error closing streams: " + e.getMessage());
    }
    conn.disconnect();
  }

  public void dispose() {
    stop();
  }

  public void run() {
    URL url;
    Base64Encoder base64 = new Base64Encoder();

    try {
      url = new URL(urlString);
    }
    catch (MalformedURLException e) {
      System.err.println("Invalid URL");
      return;
    }

    try {
      conn = (HttpURLConnection)url.openConnection();
      conn.setRequestProperty("Authorization", "Basic " + base64.encode(user + ":" + pass));
      httpIn = new BufferedInputStream(conn.getInputStream(), 8192);
    }
    catch (IOException e) {
      System.err.println("Unable to connect: " + e.getMessage());
      return;
    }

    int prev = 0;
    int cur = 0;

    try {
      while (httpIn != null && (cur = httpIn.read()) >= 0) {
        if (prev == 0xFF && cur == 0xD8) {
          jpgOut = new ByteArrayOutputStream(8192);
          jpgOut.write((byte)prev);
        }
        if (jpgOut != null) {
          jpgOut.write((byte)cur);
        }
        if (prev == 0xFF && cur == 0xD9) {
          synchronized(curFrame) {
            curFrame = jpgOut.toByteArray();
          }
          frameAvailable = true;
          jpgOut.close();
        }
        prev = cur;
      }
    }
    catch (IOException e) {
      System.err.println("I/O Error: " + e.getMessage());
    }
  }

  public void read() {
    try {
      ByteArrayInputStream jpgIn = new ByteArrayInputStream(curFrame);
      BufferedImage bufImg = ImageIO.read(jpgIn);
      jpgIn.close();
      int w = bufImg.getWidth();
      int h = bufImg.getHeight();
      if (w != this.width || h != this.height) {
        this.resize(bufImg.getWidth(),bufImg.getHeight());
      }
      bufImg.getRGB(0, 0, w, h, this.pixels, 0, w);
      this.updatePixels();
      frameAvailable = false;
    }
    catch (IOException e) {
      System.err.println("Error acquiring the frame: " + e.getMessage());
    }
  }
}

Example usage:

You will need to know the address of the camera, as well as the user / password to access it.

The code works, but may require adjustments for your particular camera.

The following data is used in the example below:

  • Address: http://212.219.113.227/axis-cgi/mjpg/video.cgi
  • User: username
  • Password: password

You should change this data for yours.

import ipcapture.*;

IPCapture cam;

void setup() {
  size(640,480);

  // Método 1
  cam = new IPCapture(this, "http://212.219.113.227/axis-cgi/mjpg/video.cgi", "", "");
  cam.start();

  // Método 2     
  // cam = new IPCapture(this);
  // cam.start("url", "username", "password");
}

void draw() {
  if (cam.isAvailable()) {
    cam.read();
    image(cam,0,0);
  }
}

void keyPressed() {
  if (key == ' ') {
    if (cam.isAlive()) cam.stop();
    else cam.start();
  }
}

Useful information

Note Project also available for Android, see same links.

    
21.12.2014 / 00:45