简体   繁体   English

Java Webcam GUI应用程序

[英]Java Webcam GUI Application

I was hoping someone would be able to help me with a problem I've been having with an application I'm developing that makes use of a webcam in java with JMF media library. 我希望有人能够帮助我解决我正在开发的应用程序所遇到的问题,该应用程序使用带有JMF媒体库的Java网络摄像头。

The problem I am having is I can run the webcam ok in an application by itself with this class here 我遇到的问题是我可以在这里通过此类自己在应用程序中运行网络摄像头

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

} }

However when I put it into my GUI application where I would like to run it from I keep getting "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" when i press the button to turn the camera on. 但是,当我将其放入要从中运行它的GUI应用程序中时,当我按此按钮打开相机电源时,会不断收到“线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException”。

I know it isn't picking up the webcam device but i can't understand why as it does when i'm not trying to embed it in my GUI. 我知道它没有拿起网络摄像头设备,但是当我不尝试将其嵌入GUI时,我不明白为什么会这样做。

I have the JMF.jar in my libraries folder as well. 我的库文件夹中也有JMF.jar。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Without more info on your NullPointerException it is impossible to say what is causing the problem. 没有有关NullPointerException更多信息,就无法说出导致问题的原因。 In the stack trace for the exception, you should identify the line in the code you wrote that triggers the exception. 在异常的堆栈跟踪中,您应该在编写的代码中标识触发异常的行。 Without any more information, my guess is you don't have an ActionListener registered to the JButton that should start the camera. 如果没有更多信息,我您是没有向JButton注册应该启动相机的ActionListener的。

cam.getLocator(); is throwing the exception. 引发异常。 Your list is not populating with any devices. 您的列表中没有任何设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM