2012-02-09 85 views
0

我希望有人能够帮助我解决一个我正在开发的应用程序的问题,该应用程序利用JMF媒体库中的Java摄像头。Java Webcam GUI应用程序

我遇到的问题是,我可以自己用这个类在这里

import java.awt.BorderLayout; 
import java.util.Vector; 

import javax.media.CaptureDeviceInfo; 
import javax.media.CaptureDeviceManager; 
import javax.media.Manager; 
import javax.media.MediaLocator; 
import javax.media.Player; 
import javax.media.control.FormatControl; 
import javax.swing.JFrame; 
import javax.swing.JButton; 

public class WebcamClass{ 

CaptureDeviceInfo cam; 
MediaLocator locator; 
Player player; 
FormatControl formatControl; 
public WebcamClass(){ 

    try{ 
        // List out available Devices to Capture Video. 
     Vector<CaptureDeviceInfo> list = CaptureDeviceManager.getDeviceList (null); 
        System.out.println(list); 
     // Iterating list 
     for(CaptureDeviceInfo temp : list){ 
      // Checking whether the current device supports VfW 
      // VfW = Video for Windows 
         if(temp.getName().startsWith("vfw:")) 
         { 
      System.out.println("Found : "+temp.getName().substring(4)); 
      // Selecting the very first device that supports VfW 
      cam = temp; 
      System.out.println("Selected : "+cam.getName().substring(4)); 
      break; 
         } 
     } 

     System.out.println("Put it on work!..."); 
     // Getting the MediaLocator for Selected device. 
     // MediaLocator describes the location of media content 
     locator = cam.getLocator(); 

     if(locator != null){ 

      // Create a Player for Media Located by MediaLocator 
      player = Manager.createRealizedPlayer(locator); 

      if(player != null){ 

       // Starting the player 
       player.start(); 

       // Creating a Frame to display Video 
            JFrame f = new JFrame(); 
       f.setTitle("Test Webcam"); 

       f.setLayout(new BorderLayout()); 
       // Adding the Visual Component to display Video captured by Player 
       // from URL provided by MediaLocator 
       f.add(player.getVisualComponent(), BorderLayout.CENTER); 
       f.pack(); 
       f.setVisible(true); 
      } 

     } 

    }catch(Exception e){ 
     System.out.println(e); 
    } 
} 

}

运行的应用程序的网络摄像头确定然而,当我把它放到我的GUI应用程序,我想运行它我不断得到“线程中的异常”AWT-EventQueue-0“java.lang.NullPointerException”当我按下按钮打开相机。

我知道它不是拿起摄像头设备,但我不明白为什么,因为它不会尝试将它嵌入到我的GUI中。

我的库文件夹中也有JMF.jar。

任何帮助将不胜感激。

回答

1

没有关于您的NullPointerException的更多信息,不可能说出是什么原因造成的问题。在异常的堆栈跟踪中,您应该识别触发异常的代码中的行。 没有更多的信息,我的是你没有ActionListener登记到JButton应该启动相机。

0

cam.getLocator();正在抛出异常。您的列表不包含任何设备。