how to set time

hi,

i am wrote the code for displaying media from webcam .In the code i am using capture button, click this one frame is captured and save in to local system,but i want automatically save the frame for every requied time.i am sending code

pls help me

[code]

import javax.swing.*;

import javax.swing.event.*;

import java.io.*;

import javax.media.*;

import javax.media.format.*;

import javax.media.util.*;

import javax.media.control.*;

import javax.media.protocol.*;

import java.util.*;

import java.awt.*;

import java.awt.image.*;

import java.awt.event.*;

import com.sun.image.codec.jpeg.*;

class ting1 extends JFrame implements ControllerListener,ActionListener

{

//Variables for gui

public JButton play=new JButton("Play");

public JButton stop=new JButton("Stop");

public JButton cap = new JButton("cap");

public JPanel p1=new JPanel();

public JPanel p2=new JPanel();

public JPanel p3=new JPanel();

Container c;

//Variables for videocapturing

boolean stateTransitionOK = true;

Object waitSync = new Object();

Processor p;

public BufferToImage btoi = null;

//public int i=0;

public Buffer buf = null;

public Image img = null;

public VideoFormat vf = null;

///Variables required to store the movie

DataSink sink;

//try{

//System.out.println("how to storing");

MediaLocator dest = new MediaLocator("C:\new.jpg");

//System.out.println("ok");

//}

//catch(Exception e){

//e.printStackTrace();

//}

///methods

public ting1()

{

super("Video Capture Demo");

setupvideocapture();

setupgui();

}

public void setupgui()

{

c.add(play);

c.add(stop);

c.add(cap);

addWindowListener(new WindowHandler());

stop.addActionListener(this);

play.addActionListener(this);

cap.addActionListener(this);

setSize(350,350);

setVisible(true);

show();

}

public void actionPerformed(ActionEvent ae)

{

String str;

str=ae.getActionCommand();

if(str=="Play")

{

try

{

//Before starting capturing store the movie to the HDD

p.start();

sink = Manager.createDataSink(p.getDataOutput(), dest);

sink.open();

sink.start();

}

catch(Exception e)

{

}

}

else if(str=="Stop")

{

try

{

// sink.stop();

p.stop();

}

catch(Exception e)

{

}

}

else if(str=="cap")

{

JComponent c = (JComponent) ae.getSource();

if (c == cap)

{

// Grab a frame

FrameGrabbingControl fgc = (FrameGrabbingControl)

p.getControl("javax.media.control.FrameGrabbingControl");

buf = fgc.grabFrame();

// Convert it to an image

btoi = new BufferToImage((VideoFormat)buf.getFormat());

img = btoi.createImage(buf);

// save image

// saveJPG(img,"c:\\test"+i+".jpg");

saveJPG(img,"c:\\test.jpg");

//i++;

}

}

}

public void setupvideocapture()

{

c=getContentPane();

c.setLayout(new FlowLayout());

try

{

p = Manager.createProcessor(new MediaLocator("vfw:Microsoft WDM Image Capture (Win32):0"));

p.addControllerListener(this);

p.configure();

waitForState(p.Configured);

p.setContentDescriptor(null);

p.prefetch();

waitForState(p.Prefetched);

c.add(p.getVisualComponent());

///Intializing the saving procedure

}

catch(Exception e)

{

e.printStackTrace();

}

}

public void controllerUpdate(ControllerEvent evt)

{

if (evt instanceof ConfigureCompleteEvent || evt instanceof RealizeCompleteEvent || evt instanceof PrefetchCompleteEvent)

{

synchronized (waitSync)

{

stateTransitionOK = true;

waitSync.notifyAll();

}

}

else if (evt instanceof ResourceUnavailableEvent)

{

synchronized (waitSync)

{

System.out.println("ResourceUnavailable");

stateTransitionOK = false;

waitSync.notifyAll();

}

}

else if (evt instanceof EndOfMediaEvent)

{

p.close();

System.exit(0);

}

}

boolean waitForState(int state)

{

synchronized (waitSync)

{

try

{

while (p.getState() != state && stateTransitionOK)

waitSync.wait();

}

catch (Exception e) {}

}

return stateTransitionOK;

}

//Entry point

public static void main(String args[])

{

ting1 t=new ting1();

}

public static void saveJPG(Image img, String s)

{

BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null),

BufferedImage.TYPE_INT_RGB);

Graphics2D g2 = bi.createGraphics();

g2.drawImage(img, null, null);

FileOutputStream out = null;

try

{

out = new FileOutputStream(s);

}

catch (java.io.FileNotFoundException io)

{

System.out.println("File Not Found");

}

JPEGImageEncoder encoder =

JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param =

encoder.getDefaultJPEGEncodeParam(bi);

param.setQuality(0.5f,false);

encoder.setJPEGEncodeParam(param);

try

{

encoder.encode(bi);

out.close();

}

catch (java.io.IOException io)

{

System.out.println("IOException");

}

}

}

class WindowHandler extends WindowAdapter

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

}

/code]

[5903 byte] By [togore_pinjalaa] at [2007-10-3 3:35:48]
# 1
Multiple Post http://forum.java.sun.com/thread.jspa?threadID=762718&tstart=0
cotton.ma at 2007-7-14 21:30:37 > top of Java-index,Java Essentials,Java Programming...