ip camera streaming

Asked

Viewed 1,582 times

3

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

dcs-932l - d-link

I need some hint, research or example. tried with Videouri believe that not

  • Search the manufacturer’s website for the camera documentation. There should be described the protocol used. Then implement the protocol or look for a library that implements it. Mount your application.

1 answer

6

There is a MJPEG stream acquisition processing library of 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 of use:

You will need to know the camera address as well as the login/username.

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

In the example below the following data are used:

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

You should change that data to your own.

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

Project Note also available for Android, see same links.

  • 2

    Curious, someone voted the negative without disclosing what is wrong with this answer so that it can be rectified and thus provide decent content to the community! Congratulations on the hat "Anonymous Votes"!

  • Holy shit, it fell like a glove to me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.