Following is the java code – which gets video from D-Link DCS900 (ip-camera) & display in frame. This is pure java class. (NO JMF)
Please give ur DCS900’s ip-address on line no. 27/28
import java.net.; import com.sun.image.codec.jpeg.; import java.io.; import java.awt.; import java.awt.event.; import java.awt.image.; import javax.swing.; import java.applet.;public class AxisCamera extends Applet implements Runnable { public boolean useMJPGStream = true; String appletToLoad; Thread appletThread; //URL for Axis 2420 //public String jpgURL="http://www.easemarry.com/axis-cgi/jpg/image.cgi?resolution=352x240"; //public String mjpgURL="http://www.easemarry.com/axis-cgi/mjpg/video.cgi?resolution=352x240"; //Following 3-lines added //URL for D-Link DCS-900 public String jpgURL = "http://www.easemarry.com/IMAGE.JPG"; public String mjpgURL = "http://www.easemarry.com/video.cgi"; DataInputStream dis; private Image image=null; public Dimension imageSize = null; public boolean connected = false; private boolean initCompleted = false; HttpURLConnection huc=null; Component parent; /* Creates a new instance of AxisCamera */ public AxisCamera (Component parent_) { parent = parent_; } public void connect() { try { URL u = new URL(useMJPGStream?mjpgURL:jpgURL); huc = (HttpURLConnection) u.openConnection(); //System.out.println(huc.getContentType()); InputStream is = huc.getInputStream(); connected = true; BufferedInputStream bis = new BufferedInputStream(is); dis= new DataInputStream(bis); if(!initCompleted) initDisplay(); } catch(IOException e) { //incase no connection exists wait and try again, instead of printing the error try { huc.disconnect(); Thread.sleep(60); } catch(InterruptedException ie) { huc.disconnect();connect(); } connect(); } catch(Exception e){;} } public void initDisplay() { //setup the display if (useMJPGStream) readMJPGStream(); else { readJPG(); disconnect(); } imageSize = new Dimension(image.getWidth(this), image.getHeight(this)); //setPreferredSize(imageSize); //parent.setSize(imageSize); //parent.validate(); initCompleted = true; } public void disconnect() { try { if(connected) { dis.close(); connected = false; } } catch(Exception e){;} } public void init() { System.out.println("Starting Applet"); appletToLoad = getParameter("appletToLoad"); setBackground(Color.white); } public void paint(Graphics g) { //used to set the image on the panel if (image != null) g.drawImage(image, 0, 0, this); } /public void run() { try { connect(); readStream(); Class appletClass = Class.forName(appletToLoad); Applet realApplet = (Applet)appletClass.newInstance(); //realApplet.setStub(this); setLayout( new GridLayout(1,0)); add(realApplet); realApplet.init(); realApplet.start(); } catch (Exception e) { System.out.println( e ); } validate(); } public void start() { appletThread = new Thread(this); appletThread.start(); } public void stop() { appletThread.stop(); appletThread = null; } public void readStream() { //the basic method to continuously read the stream try { if (useMJPGStream) { while(true) { readMJPGStream(); //parent.repaint(); } } else { while(true) { connect(); readJPG(); //parent.repaint(); disconnect(); } } } catch(Exception e){;} } public void readMJPGStream() { //preprocess the mjpg stream to remove the mjpg encapsulation //Following commented on 07/08/2006 //readLine(3,dis); //discard the first 3 lines //Following added on 07/08/2006 readLine(4, dis); //discard the first 4 lines for D-Link DCS-900 readJPG(); readLine(2,dis); //discard the last two lines } public void readJPG() { //read the embedded jpeg image try { JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(dis); image = decoder.decodeAsBufferedImage(); } catch(Exception e) { e.printStackTrace();disconnect(); } } public void readLine(int n, DataInputStream dis) { //used to strip out the header lines for (int i=0; i{ readLine(dis); } } public void readLine(DataInputStream dis) { try { boolean end = false; String lineEnd = "\n"; //assumes that the end of the line is marked with this byte[] lineEndBytes = lineEnd.getBytes(); System.out.println("lineEndBytes....."+lineEndBytes); byte[] byteBuf = new byte[lineEndBytes.length]; System.out.println("byteBuf......."+byteBuf); while(!end) { //dis.read(byteBuf,0,lineEndBytes.length); String t = ""; if(byteBuf != null) { dis.read(byteBuf,0,lineEndBytes.length); t = new String(byteBuf); } //System.out.print(t); //uncomment if you want to see what the lines actually look like if(t.equals(lineEnd)) end=true; } } catch(Exception e) { e.printStackTrace(); } } public void run() { connect(); readStream(); } /*public static void main(String[] args) { JFrame jframe = new JFrame(); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); AxisCamera axPanel = new AxisCamera(); AxisCamera axPanel = new AxisCamera(jframe); new Thread(axPanel).start(); jframe.getContentPane().add(axPanel); jframe.pack(); jframe.show(); } */ }

5 Comments on "Capture Video from ip camera using JMF"
load: AxisCamera.class can’t be instantiated.
Please review your code, It doesn’t work and throw the following exception:
java.lang.InstantiationException: AxisCamera
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Hi Java people,
I rewrite the above code as the following code, It work well on Applet Viewer of Eclipse but doesn’t work on browser. Any body can help me to embed into browser. Thank you so much. Here is my code:
import java.applet.Applet;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
/**
*
* @author David E. Mireles, Ph.D.
*/
public class LiveDemo extends Applet implements Runnable {
/**
*
*/
private static final long serialVersionUID = 1L;
public boolean useMJPGStream = false;//true;
public String jpgURL=”http://10.0.0.81/axis-cgi/jpg/image.cgi?resolution=1024×768″;
public String mjpgURL=”http://10.0.0.81/axis-cgi/jpg/image.cgi?resolution=1024×768″;
DataInputStream dis;
private Image image = null;
public Dimension imageSize = null;
public boolean connected = false;
private boolean initCompleted = false;
HttpURLConnection huc = null;
Component parent;
/** Creates a new instance of Ax52Camera */
public void init()
{
new Thread(this).start();
}
public void connect(){
try{
URL u = new URL(useMJPGStream?mjpgURL:jpgURL);
huc = (HttpURLConnection)u.openConnection();
System.out.println(huc.getContentType());
InputStream is = huc.getInputStream();
System.out.println(is.toString());
connected = true;
BufferedInputStream bis = new BufferedInputStream(is);
dis= new DataInputStream(bis);
if (!initCompleted) initDisplay();
}catch(IOException e){ //incase no connection exists wait and try again, instead of printing the error
try{
huc.disconnect();
Thread.sleep(33);
}catch(InterruptedException ie){huc.disconnect();connect();}
connect();
}catch(Exception e){;}
}
public void initDisplay(){ //setup the display
if (useMJPGStream)readMJPGStream();
else {readJPG();disconnect();}
imageSize = new Dimension(image.getWidth(this), image.getHeight(this));
setPreferredSize(imageSize);
this.setSize(imageSize);
this.validate();
initCompleted = true;
}
public void disconnect(){
try{
if(connected){
dis.close();
connected = false;
}
}catch(Exception e){;}
}
public void paint(Graphics g) { //used to set the image on the panel
if (image != null)
g.drawImage(image, 0, 0, this);
}
public void readStream(){ //the basic method to continuously read the stream
try{
if (useMJPGStream){
while(true){
readMJPGStream();
this.repaint();
}
}
else{
while(true){
connect();
readJPG();
this.repaint();
disconnect();
}
}
}catch(Exception e){;}
}
public void readMJPGStream(){ //preprocess the mjpg stream to remove the mjpg encapsulation
readLine(3,dis); //discard the first 3 lines
readJPG();
readLine(2,dis); //discard the last two lines
}
public void readJPG(){ //read the embedded jpeg image
try{
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(dis);
image = decoder.decodeAsBufferedImage();
}catch(Exception e){e.printStackTrace();disconnect();}
}
public void readLine(int n, DataInputStream dis){ //used to strip out the header lines
for (int i=0; i<n;i++){
readLine(dis);
}
}
public void readLine(DataInputStream dis){
try{
boolean end = false;
String lineEnd = "\n"; //assumes that the end of the line is marked with this
byte[] lineEndBytes = lineEnd.getBytes();
byte[] byteBuf = new byte[lineEndBytes.length];
while(!end){
dis.read(byteBuf,0,lineEndBytes.length);
String t = new String(byteBuf);
//System.out.print(t); //uncomment if you want to see what the lines actually look like
if(t.equals(lineEnd)) end=true;
}
}catch(Exception e){e.printStackTrace();}
}
public void run() {
connect();
readStream();
}
}
Very nice and helping code. thanks..
Very helping code
Thank you
SVP j’ai travaille avec Dlink DCS-920,j utilise le même code mai sa marche pas malgré que le vidéo est accessible a part via l’@: “http://ip_camera/MJPEG.CGI”
N’hesite pas à m’aider svp c urgent.
Merci d’avance.