tags:

views:

627

answers:

0

Hello friend,

I am using jmf to capture image from web cam. I have attached webcam and it is working with fine. I am using following code to capture image from webcam: --

    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGEncodeParam;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.AffineTransform;
    import java.awt.image.BufferedImage;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.Vector;
    import javax.media.Buffer;
    import javax.media.CannotRealizeException;
    import javax.media.CaptureDeviceInfo;
    import javax.media.CaptureDeviceManager;
    import javax.media.Manager;
    import javax.media.NoPlayerException;
    import javax.media.Player;
    import javax.media.control.FrameGrabbingControl;
    import javax.media.format.VideoFormat;
    import javax.media.util.BufferToImage;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;

public class FrameGrab extends JPanel implements ActionListener {
    private Player player = null;

    private BufferedImage buffImg = null;

    private Timer timer;

    private FrameGrabbingControl frameGrabber;

    public FrameGrab() {
     // Create capture device
     Vector devices = CaptureDeviceManager.getDeviceList(null);
     CaptureDeviceInfo cdi = null;
     for (Iterator i = devices.iterator(); i.hasNext();) {
      cdi = (CaptureDeviceInfo) i.next();
      /* Get the first Video For Windows (VFW) capture device.
       * Use the JMF registry tool in the bin directory of the JMF
       * distribution to detect available capture devices on your
       * computer.
       */
      if (cdi.getName().startsWith("vfw:"))
       break;
     }
     // start the Timer with 3s intervals
     new Timer(2000, this).start();
     try {
      player = Manager.createRealizedPlayer(cdi.getLocator());
      player.start();
     } catch (NoPlayerException e) {
      e.printStackTrace();
     } catch (CannotRealizeException e) {
      e.printStackTrace();
     } catch (IOException e) {
      e.printStackTrace();
     }
     // Grab a frame from the capture device
     frameGrabber = (FrameGrabbingControl) player
       .getControl("javax.media.control.FrameGrabbingControl");
    }

    public void paintComponent(Graphics g) {
     super.paintComponent(g);
     if (buffImg != null) {
      g.drawImage(buffImg, 0, 0, this);
     }
    }

        private void writeImagetoFile(BufferedImage img,String aFileName,int width,int height,double quality)
        {
            try{
            FileOutputStream fos = new FileOutputStream(aFileName);
            JPEGImageEncoder encoder2 =JPEGCodec.createJPEGEncoder(fos);
            JPEGEncodeParam param2 = encoder2.getDefaultJPEGEncodeParam(img);
            param2.setQuality((float) quality, true);
            encoder2.encode(img,param2);
            fos.close();
            }catch(Exception ex){}
        }
    private void grab() {
            Buffer buf=null;

            try{
                Thread.sleep(2000);
     buf = frameGrabber.grabFrame();
            }catch(Exception ex){ex.printStackTrace();}
     // Convert frame to an buffered image so it can be processed and saved
     Image img = (new BufferToImage((VideoFormat) buf.getFormat())
       .createImage(buf));
     buffImg = new BufferedImage(img.getWidth(this), img.getHeight(this),
       BufferedImage.TYPE_INT_RGB);
                writeImagetoFile(buffImg,"c:/image.jpg",100,100,100);
     Graphics2D g = buffImg.createGraphics();
     g.drawImage(img, null, null);
     g.setColor(Color.darkGray);
     g.setFont(new Font("Tahoma", Font.PLAIN, 12)
       .deriveFont(AffineTransform.getRotateInstance(1.57)));
     g.drawString((new Date()).toString(), 5, 5);
    }

    public static void createAndShowGui() {
     JFrame frame = new JFrame("Frame Grabber");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.getContentPane().add(new FrameGrab());
     frame.setSize(328, 270);
     frame.setVisible(true);
    }

    public static void main(String[] args) {
     SwingUtilities.invokeLater(new Runnable() {
      public void run() {
       createAndShowGui();
      }
     });
    }

    /*
     * (non-Javadoc)
     *
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent e) {
     grab();
     repaint();
    }
}

By running this program i am getting following exception:

java.lang.NullPointerException
        at writepdffile.FrameGrab.grab(FrameGrab.java:99)
        at writepdffile.FrameGrab.actionPerformed(FrameGrab.java:137)
        at javax.swing.Timer.fireActionPerformed(Timer.java:271)
        at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at writepdffile.FrameGrab.grab(FrameGrab.java:102)
        at writepdffile.FrameGrab.actionPerformed(FrameGrab.java:137)
        at javax.swing.Timer.fireActionPerformed(Timer.java:271)
        at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

I am not getting why it is generating such exception... Please some body help me ,i am newer with jfm.

Thanks in Advance