The first thing to determine is what streaming formats your IP camera supports. Streaming JPEG is one of the simplest, albiet crude method of streaming video from an IP camera. The foscam supports this format.
This file is a frame grabber which extracts the jpeg from the HTTP response of the IP Camera.
public BufferedImage grabBufferedImage()throwsException{ byte[] buffer =newbyte[4096];// MTU or JPG Frame Size? int n =-1; ByteArrayOutputStream baos =newByteArrayOutputStream();
StringBuffer sb =newStringBuffer(); int total =0; int c; // read http subheader while((c = input.read())!=-1){ if(c >0){
sb.append((char) c); if(c ==13){
sb.append((char) input.read());// '10'
c = input.read();
sb.append((char) c); if(c ==13){
sb.append((char) input.read());// '10' break;// done with subheader }
} } } // find embedded jpeg in stream String subheader = sb.toString();
LOG.error(subheader); int contentLength =-1; // if (boundryKey == null) // { // Yay! - server was nice and sent content length int c0 = subheader.indexOf("Content-Length: "); int c1 = subheader.indexOf('\r', c0);
c0 +=16;
contentLength =Integer.parseInt(subheader.substring(c0, c1));
LOG.info("Content-Length: "+ contentLength); // } else {
Hello Akbar
The first thing to determine is what streaming formats your IP camera supports. Streaming JPEG is one of the simplest, albiet crude method of streaming video from an IP camera. The foscam supports this format.
This file is a frame grabber which extracts the jpeg from the HTTP response of the IP Camera.
http://code.google.com/p/myrobotlab/source/browse/trunk/src/org/myrobotlab/image/IPCameraFrameGrabber.java?spec=svn283&r=283
What is your end goal, maybe there is a simpler way to get what you want done.
Greg.
Thank you for giving the
Thank you for giving the solution
My ultimate goal is to create an application monitoring with ip camera with java
I actually made robot spy using the wireless module
module using chip WIZnet NM7010A
please help!!!
can i use your code http://code.google.com/p/myrobotlab/source/search?q=ipca&origq=ipca&btnG=Search+Trunk
Certainly
You may use anything which you find helpful on your project. If you have specific questions, let me know and I might be able to assist.
Regards,
Greg.
Thanks
OK
Thanks for help
:)
is that work on netbeans?
http://code.google.com/p/myrobotlab/source/browse/trunk/src/org/myrobotlab/image/IPCameraFrameGrabber.java?spec=svn283&r=283
is that work on netbeans?
where i can download package :
- com.googlecode.javacv.FrameGrabber
- com.googlecode.javacv.cpp.opencv_core.IplImage
thanks
newbie
i am newbie...
i have done like you said
but how i create the main method????
thanks in adv
This compiles. It
package org.myrobotlab.image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class IPCameraFrameGrabber {
/*
* excellent reference - http://www.jpegcameras.com/ foscam url
* http://host/videostream.cgi?user=username&pwd=password
* http://192.168.0.59:60/videostream.cgi?user=admin&pwd=password android ip
* cam http://192.168.0.57:8080/videofeed
*/
public final static Logger LOG = Logger
.getLogger(IPCameraFrameGrabber.class.getCanonicalName());
private URL url;
private URLConnection connection;
private InputStream input;
private Map<String, List<String>> headerfields;
private String boundryKey;
public IPCameraFrameGrabber(String urlstr) {
try {
url = new URL(urlstr);
} catch (MalformedURLException e) {
LOG.error(e);
}
}
public void start() throws Exception {
LOG.error("connecting to " + url);
connection = url.openConnection();
headerfields = connection.getHeaderFields();
if (headerfields.containsKey("Content-Type")) {
List<String> ct = headerfields.get("Content-Type");
for (int i = 0; i < ct.size(); ++i) {
String key = ct.get(i);
int j = key.indexOf("boundary=");
if (j != -1) {
boundryKey = key.substring(j + 9);
}
}
}
input = connection.getInputStream();
}
public void stop() throws Exception {
// connection.
input.close();
input = null;
connection = null;
url = null;
}
public BufferedImage grabBufferedImage() throws Exception {
byte[] buffer = new byte[4096];// MTU or JPG Frame Size?
int n = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
StringBuffer sb = new StringBuffer();
int total = 0;
int c;
// read http subheader
while ((c = input.read()) != -1) {
if (c > 0) {
sb.append((char) c);
if (c == 13) {
sb.append((char) input.read());// '10'
c = input.read();
sb.append((char) c);
if (c == 13) {
sb.append((char) input.read());// '10'
break; // done with subheader
}
}
}
}
// find embedded jpeg in stream
String subheader = sb.toString();
LOG.error(subheader);
int contentLength = -1;
// if (boundryKey == null)
// {
// Yay! - server was nice and sent content length
int c0 = subheader.indexOf("Content-Length: ");
int c1 = subheader.indexOf('\r', c0);
c0 += 16;
contentLength = Integer.parseInt(subheader.substring(c0, c1));
LOG.info("Content-Length: " + contentLength);
// } else {
// }
// adaptive size - careful - don't want a 2G jpeg
if (contentLength > buffer.length) {
buffer = new byte[contentLength];
}
n = -1;
total = 0;
while ((n = input.read(buffer, 0, contentLength - total)) != -1) {
total += n;
baos.write(buffer, 0, n);
if (total == contentLength) {
break;
}
}
baos.flush();
// LOG.info("wrote " + baos.size() + "," + total);
BufferedImage bi = ImageIO.read(new ByteArrayInputStream(baos
.toByteArray()));
return bi;
}
public static void main(String[] args) {
try {
org.apache.log4j.BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.DEBUG);
IPCameraFrameGrabber camera = new IPCameraFrameGrabber(
"http://host/videostream.cgi?user=username&pwd=password");
camera.start();
int fn = 0;
while (fn < 100) {
++fn;
LOG.debug("grabbing frame " + fn);
BufferedImage frame = camera.grabBufferedImage();
LOG.debug("saving frame " + fn);
save(frame, "frame_"+fn, "jpg");
}
} catch (Exception e) {
LOG.error(e.getMessage());
}
}
public static void save(BufferedImage image, String fileName, String ext) {
File file = new File(fileName + "." + ext);
try {
ImageIO.write(image, ext, file); // ignore returned boolean
} catch(IOException e) {
System.out.println("Write error for " + file.getPath() +
": " + e.getMessage());
}
}
}
Thanks
thanks in adv