从网络摄像头捕获图像

时间:2009-12-05 18:24:14

标签: java webcam jmf

我正在使用jmf从网络摄像头捕获图像。 我已经连接了摄像头,它工作得很好。 我使用以下代码从网络摄像头捕获图像: -

    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();
    }
}

通过运行此程序,我得到以下异常:

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)

我不明白为什么会产生这样的异常...... 请一些人帮我,我是jfm的新手。

先谢谢

1 个答案:

答案 0 :(得分:1)

好的,如果您遇到NullPointerException的问题,当您未通过jmf注册表进行配置时会发生这种情况。当您为Windows安装jmf时,会有与之关联的jmf注册应用程序。您必须打开它,选择设置并选择捕获设备。如果您的设备以vfw(Windows视频)开头,如vfw:Microsoft Image Capture(win 32):0,则提交它,这是最后一步。

我认为你已经将类路径设置为包含jmf.jar和其他jar文件的jmf的lib文件夹。然后你可以正常运行jmf的程序。如果您遇到任何问题,请告诉我