Java 网络摄像头 GUI 应用程序
我希望有人能够帮助我解决我正在开发的应用程序遇到的问题,该应用程序使用带有 JMF 媒体库的 java 网络摄像头。
我遇到的问题是我可以在应用程序中使用此类自行运行网络摄像头
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。
任何帮助将不胜感激。
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.
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.
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.
I have the JMF.jar in my libraries folder as well.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有有关
NullPointerException
的更多信息,就不可能说出导致问题的原因。在异常的堆栈跟踪中,您应该标识您编写的代码中触发异常的行。如果没有更多信息,我的猜测是您没有注册到应该启动相机的 JButton 的 ActionListener 。
Without more info on your
NullPointerException
it is impossible to say what is causing the problem. 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 theJButton
that should start the camera.cam.getLocator();
抛出异常。您的列表中未填充任何设备。cam.getLocator();
is throwing the exception. Your list is not populating with any devices.