Question

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

Was it helpful?

Solution

okay, if you are facing problem of NullPointerException, this happens when you have not configured through jmf registry. When u install jmf for windows, there is jmf registory application comes associated with it. You have to open that, select settings and select capture devices. If your device starts with vfw(video for windows) like vfw:Microsoft Image Capture(win 32):0, then commit it, that's a last step.

I think you have already set the classpath to lib folder of jmf containing jmf.jar and others jar files. Then you can normally run the program of jmf. Let me know if you come across any problems

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top