2012-02-09 127 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();正在拋出異常。您的列表不包含任何設備。