Java程序打开摄像头 我的出现了异常
Java程序打开摄像头 我的出现了异常 以下是代码 请大侠看看 谢谢
package com.lovo.Vido.Action;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.Vector;
import javax.media.CaptureDeviceInfo;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.cdm.CaptureDeviceManager;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.AudioFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Action extends JFrame {
private static final Object South = null;
static Action frame;
private JPanel contentPane, p3, p4;
private JPanel panel;
public CaptureDeviceInfo device2;
public Player player2;
Component comV, comVC, comA;
public static Player player = null;
private CaptureDeviceInfo device = null;
private MediaLocator locator = null;
static String str1 = "vfw:Logitech USB Video Camera:0";
static String str = "vfw:Microsoft WDM Image Capture (Win32):0";
static String aa = "javaSound audio capture";// 音频
static String bb = "DirectSoundCapture";
/**
* Launch the application
*
*/
public static void main(String [] args){
EventQueue.invokeLater(new Runnable() {
/**
* Runnable 提供的run方法
*/
public void run() {
frame = new Action();
frame.setVisible(true);
try {
frame.speaker();
} catch (Exception e) {
System.out.println("出现异常1:"+e.getMessage());
}
try {
frame.jbInit();
} catch (Exception e) {
System.out.println("系统出现异常2:"+e.getMessage());
}
}
});
}
private void speaker()throws Exception{
p3 = new JPanel();
p3.setLayout(new BorderLayout(0,0));
Vector deviceList = CaptureDeviceManager.getDeviceList(new AudioFormat(AudioFormat.LINEAR,44100,16,2));
if(deviceList.size()>0){
device2 = (CaptureDeviceInfo)deviceList.firstElement();
}else{
System.out.println("抱歉找不到音频设备");
}
try{
player2 = Manager.createRealizedPlayer(device2.getLocator());
player2.start();
if((comA = player2.getControlPanelComponent())!=null){
p3.add(comA,"Center");
}
}catch(Exception e){
System.out.println("异常:"+e.getMessage());
}
contentPane.add(p3,South);
}
private void jbInit()throws Exception{
//初始化设备,str为设备驱动
p4 = new JPanel();
p4.setLayout(new BorderLayout(0,0));
p4.setOpaque(false);
device = CaptureDeviceManager.getDevice(str);
MediaLocator locatorss = new MediaLocator("vfw://0");
player = Manager.createRealizedPlayer(locatorss);
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER,new Boolean(true));
player.start();
comV=player.getVisualComponent();
FrameGrabbingControl fgc = (FrameGrabbingControl)
player.getControl("javax.media.control.FrameGrabbingControl");
if((comV)!=null){
p4.add(comV,"Center");
}
p4.setVisible(true);
contentPane.add(p4,"Center");
}
public Action(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,450,300);
contentPane = new JPanel();
contentPane.setOpaque(false);
contentPane.setBorder(new EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0,0));
}
}
异常结果:
出现异常1:null
系统出现异常2:Cannot find a Player for :vfw://0
请说一下处理结果谢谢