IP Camera connection in JAVA and taking jpeg snapshot every 40 sec

4,132 views
Skip to first unread message

Adnan Liaqat

unread,
Dec 29, 2011, 6:11:01 AM12/29/11
to Marvin Developers
Hello ! Dear,

I am trying to connect IP Camera in JAVA and tacking a JPEG snapshot
after 40sec and store in Hard Drive. I have no idea with this please
help me ................. thanks

Following Code without any showing result:
**************************************************
public class IPCameraConnection extends Applet implements Runnable
{
public boolean useMJPGStream = true;
String appletToLoad;
Thread appletThread;

public String mjpgURL ="http://192.168.1.10";

DataInputStream dis;
private Image image=null;
public Dimension imageSize = null;
public boolean connected = false;
private boolean initCompleted = false;
HttpURLConnection huc=null;
Component parent;

public IPCameraConnection (Component par)
{
parent = par;
}


public void connect()
{
Socket socket = null;
HttpURLConnection httpConn= null;
try
{
URL u = new URL(mjpgURL);

huc = (HttpURLConnection) u.openConnection();
String encoding = new
sun.misc.BASE64Encoder().encode("admin:admin".getBytes());
huc.setRequestProperty ("Authorization", "Basic " +
encoding);
System.out.println("con open complete");
//System.out.println(huc.getContentType());
InputStream is = huc.getInputStream();
System.out.println("input stream got");
connected = true;
//BufferedInputStream bis = new BufferedInputStream(is);
dis= new DataInputStream(is);

if(!initCompleted)
System.out.println("!init complete");
initDisplay();
}
catch(IOException e)
{
//incase no connection exists wait and try again,
instead of printing the error
try
{
System.out.println("Exception :
"+e.getMessage());
System.out.println("no con");
huc.disconnect();
Thread.sleep(60);
}
catch(InterruptedException ie)
{
System.out.println("no con excp");
huc.disconnect();
connect();
}
connect();
}
catch(Exception e){;}
}
public void initDisplay()
{
//setup the display
if (useMJPGStream)
{
System.out.println("mpg-------------------------------");
readMJPGStream();
}
else
{
System.out.println("jpg");
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);
else
System.out.println("empty frame");
}
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 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();
System.out.println("lineEndBytes....."+lineEndBytes);
byte[] byteBuf = new byte[lineEndBytes.length];
System.out.println("byteBuf......." + byteBuf);
byte[] bf = new byte[1024];
StringBuffer sb = new StringBuffer();
while(!end)
{
//dis.read(byteBuf,0,lineEndBytes.length);
//String t = "";
int i = dis.read();
sb.append((char)i);
if( i == -1)
end = true;
//if(byteBuf != null)
//{
//dis.read(byteBuf,0,lineEndBytes.length);

//t = dis.readLine();
//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();
// }

private void readJPG()
{
try
{
JPEGImageDecoder decoder =
JPEGCodec.createJPEGDecoder(dis);
image = decoder.decodeAsBufferedImage();
}
catch(Exception e)
{
e.printStackTrace();disconnect();
}
}

dinorb

unread,
Jan 1, 2012, 1:36:15 PM1/1/12
to Marvin Developers
Maybe my code can help you to get on the right track. I use
MarvinFramework and works amaizingly well.
Here's my code:
[CODE]
/**
Marvin Project <2007-2009>

Initial version by:

Danilo Rosetto Munoz
Fabio Andrijauskas
Gabriel Ambrosio Archanjo

site: http://marvinproject.sourceforge.net

GPL
Copyright (C) <2007>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along
with this program; if not, write to the Free Software Foundation,
Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import marvin.gui.MarvinImagePanel;
import marvin.image.MarvinImage;
import marvin.image.MarvinImageMask;
import marvin.io.MarvinImageIO;
import marvin.plugin.MarvinImagePlugin;
import marvin.video.MarvinVideoManager;

@SuppressWarnings("serial")
public class Cam extends JFrame implements Runnable{

// GUI
private JPanel panelButton,
panelCenter;

private JButton buttonPlayStop, close;

private MarvinVideoManager videoManager;
private MarvinImagePanel videoPanel;

private MarvinImage imageIn;
private MarvinImage imageOut;
private MarvinImage imageLastFrame;
private MarvinImageMask imageMask;

private Thread thread;

private MarvinImagePlugin pluginImage;

private int cameraWidth,
cameraHeight;



public Cam(){

videoPanel = new MarvinImagePanel();
videoManager = new MarvinVideoManager(videoPanel);
videoManager.connect();
cameraWidth = videoManager.getCameraWidth();
cameraHeight = videoManager.getCameraHeight();

imageLastFrame = new MarvinImage(cameraWidth,cameraHeight);
imageMask = MarvinImageMask.NULL_MASK;

loadGUI();
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);


thread = new Thread(this);
thread.start();
}

private void loadGUI(){
setTitle("Video Sample - Filters");

// Buttons
ButtonHandler l_handler = new ButtonHandler();
buttonPlayStop = new JButton("Snapshot");
close = new JButton("Close");
buttonPlayStop.addActionListener(l_handler);
close.addActionListener(l_handler);


// Panels
panelButton = new JPanel();
panelButton.add(buttonPlayStop);
panelButton.add(close);


panelCenter = new JPanel(new BorderLayout());
panelCenter.add(videoPanel, BorderLayout.NORTH);
panelCenter.add(panelButton, BorderLayout.SOUTH);


Container l_container = getContentPane();
l_container.setLayout(new BorderLayout());
l_container.add(panelCenter, BorderLayout.CENTER);

setSize(cameraWidth,cameraHeight+65);
setResizable(false);
setVisible(true);
}


public void run(){
while(true){

imageIn = videoManager.getCapturedImage();
imageOut = videoManager.getResultImage();

if(pluginImage == null){
MarvinImage.copyColorArray(imageIn, imageOut);
}
if(pluginImage != null){
pluginImage.process(imageIn, imageOut, null, imageMask, false);
}

MarvinImage.copyColorArray(imageIn, imageLastFrame);
videoManager.updatePanel();
}

}
public void snapshot(){
MarvinImage snapshot = videoManager.getResultImage();
snapshot.update();
MarvinImageIO.saveImage(snapshot, "./snapshot.JPG");
}

private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent a_event){
if(a_event.getSource() == buttonPlayStop){
snapshot();
} else if(a_event.getSource() == close){
dispose();
videoManager.disconnect();
}
}
}

}
[/CODE]
This takes snapshoots. To take a snapshoot every 40 sec you just have
to add maybe 2 more Lines of code. I hope this can help you.

Adnan Liaqat

unread,
Jan 2, 2012, 4:08:12 AM1/2/12
to marvin-...@googlegroups.com

Thank you for the time you spent helping me over the email. I realize you are busy, and I appreciate your spending so much time helping me untangle my IP Camera code with your.

thanks for this anticipation, Can you add me on skype (
adnaniiui) for normal conversation and hello hy , if you don't feel free

again thanks  

------
With Kind Regard,

Adnan Liaqat 
adnan...@gmail.com
Skype:: adnaniiui

                           " ~ Education is not preparation for life ; 
                                           ~  Education is itself life"   


--
You received this message because you are subscribed to the Google Groups "Marvin Developers" group.
To post to this group, send email to marvin-...@googlegroups.com.
To unsubscribe from this group, send email to marvin-projec...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/marvin-project?hl=en.




--

Reply all
Reply to author
Forward
0 new messages