jdk1.6
jmf2.1.1
排除摄像头不可用问题
jmf studio摄像头运行正常eclipse错误如下Exception in thread "main" java.lang.NullPointerException
at net.java.paradisesdk.webcam.WebCamSwing.<init>(WebCamSwing.java:74)
at net.java.paradisesdk.webcam.WebCamSwing.main(WebCamSwing.java:101)
代码如下package net.java.paradisesdk.webcam;import javax.swing.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*;import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;import com.sun.image.codec.jpeg.*;/**
 * @author &#65533;&#65533;&#65533;
 */
public class WebCamSwing extends Applet implements ActionListener
{
    public static Player player = null;
    private CaptureDeviceInfo di = null;
    private MediaLocator ml = null;
    /**
     * &#65533;&#65533;&#65533;&#1392;&#65533;&#357;
     */
    private JButton capture = null;
    /**
     * &#65533;&#65533;&#65533;水&#357;
     */
    private JButton save = null;
    /**
     * &#1127;&#65533;&#65533;&#1127;&#65533;&#65533;
     */
    private JTextField num = null;
    private Buffer buf = null;
    private Image img = null;
    private VideoFormat vf = null;
    private BufferToImage btoi = null;
    private ImagePanel imgpanel = null;    /**
     * &#1121;&#545;&#65533;&#65533;&#65533;x,y,width,height&#65533;&#65533;&#65533;&#65533;&#65533;&#300;&#65533;&#65533;&#1461;
     */
    private int rectX;
    private int rectY;
    private int rectWidth = 150;
    private int rectHeight = 200;
    private int imgWidth = 320;
    private int imgHeight = 240;
    /**
     * &#300;&#65533;&#1009;&#65533;&#65533;&#65533;&#65533;&#316;&#65533;&#65533;&#65533;
     */
    private String fname = "test";    public WebCamSwing()
    {
        setLayout(new BorderLayout());
        setSize(320, 550);        imgpanel = new ImagePanel();
        imgpanel.addMouseMotionListener(imgpanel);
        capture = new JButton("&#65533;&#65533;&#65533;&#65533;");
        capture.addActionListener(this);
        save = new JButton("&#65533;&#65533;&#65533;&#65533;");
        save.addActionListener(this);
        num = new JTextField();        String str1 = "vfw:Logitech USB Video Camera:0";
        String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";        di = CaptureDeviceManager.getDevice(str2);
        ml = di.getLocator();
        try
        {
            player = Manager.createRealizedPlayer(ml);
            player.start();
            Component comp;
            if ((comp = player.getVisualComponent()) != null)
            {
                add(comp, BorderLayout.NORTH);
            }
            Panel panel1 = new Panel(new BorderLayout());
            panel1.add(capture, BorderLayout.NORTH);
            panel1.add(new Label("&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;1111:"), BorderLayout.WEST);
            panel1.add(num, BorderLayout.CENTER);
            panel1.add(save, BorderLayout.SOUTH);
            add(panel1, BorderLayout.CENTER);
            add(imgpanel, BorderLayout.SOUTH);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }    public static void main(String[] args)
    {
        Frame f = new Frame("&#65533;&#1459;&#65533;&#65533;&#65533;&#65533;&#1395;&#65533;&#65533;&#65533; BCU WebCam Application");
        WebCamSwing cf = new WebCamSwing();
        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                playerclose();
                System.exit(0);
            }
        });
        f.add("Center", cf);
        f.pack();
        f.setSize(new Dimension(320, 590));
        f.setVisible(true);
    }
    /**
     * &#65533;&#1585;&#65533;&#65533;&#65533;&#65533;&#65533;&#887;
     *
     */
    public static void playerclose()
    {
        player.close();
        player.deallocate();
    }
    /**
     * &#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;
     */
    public void actionPerformed(ActionEvent e)
    {
        JComponent c = (JComponent) e.getSource();
        if (c == capture)
        { // Grab a frame      
            FrameGrabbingControl fgc =
                (FrameGrabbingControl) player.getControl(
                    "javax.media.control.FrameGrabbingControl");
            buf = fgc.grabFrame(); // Convert it to an image      
            btoi = new BufferToImage((VideoFormat) buf.getFormat());
            img = btoi.createImage(buf); // show the image     
            imgpanel.setImage(img); // save image
        }
        else if (c == save)
        {
            if (img != null)
            {
                fname = !num.getText().equals("") ? num.getText() : "test";
                saveJPG(img, "Photo/" + fname + ".jpg");
            }
        }
    }
    /**
     * &#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#634;&#65533;&#65533;&#65533;&#702;&#65533;&#65533;&#428;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#1014;&#65533;&#65533;&#65533;Χ&#65533;&#65533;&#1121;&#65533;&#65533;&#1194;&#65533;&#65533;&#545;&#65533;&#306;&#65533;&#65533;&#65533;
     * @author &#65533;&#65533;&#65533;
     */

解决方案 »

  1.   

    class ImagePanel extends Panel implements MouseMotionListener
        {
            private Image myimg = null;        public ImagePanel()
            {
                setLayout(null);
                setSize(imgWidth, imgHeight);
            }
            public void setImage(Image img)
            {
                this.myimg = img;
                repaint();
            }
            public void update(Graphics g)
            {
                g.clearRect(0, 0, getWidth(), getHeight());
                if (myimg != null)
                {
                    g.drawImage(myimg, 0, 0, this);
                    g.setColor(Color.RED);
                    g.drawRect(rectX, rectY, rectWidth, rectHeight);
                }
            }
            public void paint(Graphics g)
            {
                update(g);
            }        public void mouseDragged(MouseEvent e)
            {
                rectX = e.getX() - 50;
                rectY = e.getY() - 50;
                repaint();
            }        public void mouseMoved(MouseEvent e)
            {
            }
        }
        /**
         * &#65533;&#65533;&#65533;&#65533;&#892;&#65533;&#65533;
         * @param img
         * @param s
         */
        public void saveJPG(Image img, String s)
        {
            BufferedImage bi = (BufferedImage) createImage(imgWidth, imgHeight);
            /*BufferedImage bi =
                new BufferedImage(
                    img.getWidth(null),
                    img.getHeight(null),
                    BufferedImage.TYPE_INT_RGB);*/        Graphics2D g2 = bi.createGraphics();
            g2.clipRect(rectX, rectY, rectWidth, rectHeight);
            g2.drawImage(img, null, null);
            int moveX = rectX > 0 ? rectX : 0;
            int moveY = rectY > 0 ? rectY : 0;
            int cutWidth =
                rectX + rectWidth > imgWidth
                    ? rectWidth - ((rectX + rectWidth) - imgWidth)
                    : rectWidth;
            int cutHeight =
                rectY + rectHeight > imgHeight
                    ? rectHeight - ((rectY + rectHeight) - imgHeight)
                    : rectHeight;
            bi = bi.getSubimage(moveX, moveY, cutWidth, cutHeight);        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(1f, false);
            encoder.setJPEGEncodeParam(param);
            try
            {
                encoder.encode(bi);
                out.close();
            }
            catch (java.io.IOException io)
            {
                System.out.println("IOException");
            }
        }
    }
      

  2.   

    73行 di = CaptureDeviceManager.getDevice(str2);
    取设备没有取到返回null。 可能是设备名称不对
      

  3.   

    jmf studio 里面 注册参数 如下Name = vfw:Microsoft WDM Image Capture (Win32):0Locator = vfw://0Output Formats---->0. javax.media.format.RGBFormat
      RGB, 640x480, Length=921600, 24-bit, Masks=3:2:1, PixelStride=3, LineStride=1920, Flipped
    1. javax.media.format.RGBFormat
      RGB, 160x120, Length=57600, 24-bit, Masks=3:2:1, PixelStride=3, LineStride=480, Flipped
    2. javax.media.format.RGBFormat
      RGB, 176x144, Length=76032, 24-bit, Masks=3:2:1, PixelStride=3, LineStride=528, Flipped
    3. javax.media.format.RGBFormat
      RGB, 320x240, Length=230400, 24-bit, Masks=3:2:1, PixelStride=3, LineStride=960, Flipped
    4. javax.media.format.RGBFormat
      RGB, 352x288, Length=304128, 24-bit, Masks=3:2:1, PixelStride=3, LineStride=1056, Flipped他自检出来的就是这个设备阿
      

  4.   

    java,.net,asp,hacker技术讨论群14401742,诚招高手加入,共同讨论,共同发展。
      

  5.   

    java.lang.NullPointerException
    这不是空指针异常吗?
      

  6.   

    你是不是没有在JMFRegistry注册新的数据源?
      

  7.   

    String str2 = "vfw:Microsoft WDM Image Capture (Win32):0"; 这个设备的描述是不对的
    所以你捕获的设备是不存在,因为不存在所以报NullPointerException 这个异常
      

  8.   

    要在本机的JMF上注册摄像头捕捉设备!
      

  9.   

    我在这方面有一段时间的专业研究,欢迎与我取得适时在线联系,我的msn
    [email protected]
      

  10.   

    我也遇到过这样的问题,你必须先用FRegistry向JMF注册新的数据源、媒体处理器、插件、视频和音频截取设备,然后你才能够在你的程序中使用它们,怎么注册请看下面的网址:
    http://www.hackhome.com/InfoView/Article_115110_3.html
      

  11.   

    十有八九是因为导入的包太多了!
    Two actions to solve "CaptureDeviceManager.getDeviceList(null) return NULL"0. delete jmf.jar information if jmf.jar location information is java's lib folder. 
    - jmf.jar is should be imported from JMF's original folder
    (c:\java\jmf2.1.1.e\lib[java 1.5 or above] or c:\program files\java\jmf2.1.1.e\lib [ java 1.4.x] , path name is varies depends on JAVA version).
    1. delete jmf.jar in %JAVA_HOME%\lib (or c:\java\lib\jmf.jar or c:\java\lib\ext\jmf.jar ) 2. import jmf.jar located in JMF's lib folder ( in Eclipse environment) 
    - do not just copy jmf.jar to your application folder or eclipse's folder 
    because, JMF's registry file is save in JMF folder and some information is exchanged througth this registry file