JMF调用摄像头时CaptureDeviceManager.getDevice()方法返回nul

最近在弄JMF调用摄像头的程序,记录一些东西,可能写得很乱,主要记录一下遇到的问题和解决方案:

第一步是导入JMF的JAR包:

customizer.jar

jmf.jar

mediaplayer.jar

multiplayer.jar

1、首先给出控制摄像头的源代码,这段代码我也是在网上找的

/**  */
package com.eya.camera.test;


import java.awt.Component;
import java.awt.Rectangle;


import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.UIManager;


/**
 *
 * @author ll Develop on : 2015年3月13日
 */


public class SampCam extends JFrame {
    private static final long serialVersionUID = 1211056605389924001L;
    private static Player player = null;
    private CaptureDeviceInfo device = null;
    private MediaLocator locator = null;
    boolean proportion = true;
    String str1 = "vfw:Logitech USB Video Cam:0";
    // 这段内容是在JMF注册机中找到的
    String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
    Component component1;


    public SampCam() {
 super("摄像机");
 try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());// 与操作系统风格一致
 } catch (Exception e1) {
    e1.printStackTrace();
 } 
 try {
    jbInit();
 } catch (Exception e) {
    e.printStackTrace();
 }
    }


    public static void main(String[] args) {
 new SampCam();
    }


    private void jbInit() throws Exception {
 component1 = Box.createGlue();
 // =====================初始化设备===================//
 component1.addNotify();
 // 获取设备
 device = CaptureDeviceManager.getDevice(str2);
 locator = device.getLocator();
 try {
    player = Manager.createRealizedPlayer(locator);
    player.start();
    if ((component1 = player.getVisualComponent()) != null) {
 this.getContentPane().add(component1, "Center");
    }
 } catch (Exception e) {
    e.printStackTrace();
 }
 component1.setBounds(new Rectangle(0, 0, 250, 280));
 this.setSize(380, 300);
 this.setVisible(true);
    }
}

 
  
  
  
  



2、我的开发工具是Eclipse,在执行上述代码时,
CaptureDeviceManager.getDevice(str2)

这段代码返回Null了,理应不该返回null的,应该返回我接入的视频设备驱动器,后来网上找了资料(http://www.cnblogs.com/perhaps/archive/2010/07/10/1775053.html),大致内容差不多,确实是由于IDE的classpath下没有找到jmf.jar这个包

解决方案是在对应的工程下的classpath中加入jmf.jar的路径

3、如何在Eclipse的classpath中添加其他包的路径引用

在工作空间中找到对应的工程,下面有个.classpath文件,用打开该文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="src"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.6.0_14"/>
	<classpathentry kind="lib" path="customizer.jar"/>
	<classpathentry kind="lib" path="jmf.jar"/>
	<classpathentry kind="lib" path="mediaplayer.jar"/>
	<classpathentry kind="lib" path="multiplayer.jar"/>
	<classpathentry kind="output" path="bin"/>
</classpath>



在其中添加一行代码:<classpathentry kind="lib" path="C:\\Program Files\\JMF2.1.1e\\lib\jmf.jar" />

添加代码后变成:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="src"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.6.0_14"/>
	<classpathentry kind="lib" path="customizer.jar"/>
	<classpathentry kind="lib" path="jmf.jar"/>
	<classpathentry kind="lib" path="mediaplayer.jar"/>
	<classpathentry kind="lib" path="multiplayer.jar"/>
	<classpathentry kind="output" path="bin"/>
	<classpathentry kind="lib" path="C:\\Program Files\\JMF2.1.1e\\lib\jmf.jar" />
</classpath>



重启Eclipse后,可以正常获取设备了。

到此,程序运行不报错,但是我也不清楚到底摄像头是否开始运行了,因为没有弹出窗显示摄像头录入的信息,我还在继续研究中.....

你可能感兴趣的:(java,eclipse,JMF)