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
请说一下处理结果谢谢
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
请说一下处理结果谢谢
解决方案 »
- JAVA中ArrayList是否允许两个线程同时进行插入和删除操作?
- java初学者的疑问,好心人帮帮忙
- 用JFileChooser可以实现打开服务器的文件目录选择文件吗?
- java写的txt文件非DOS格式怎么办?
- jmf 如何设置摄像头初始化的大小??
- java应用程序打包出现问题。
- 请教一下,那里有JBULIDER的使用教材/
- 总是出现java.lang.NoSuchMethodError错误,在线等待!~!
- visual age for java连接数据库问题!
- 为什么依然是false?
- 寻求一个关于hibernate有关的抓狂的问题
- 为什么以后会学到linux开发呢?linux跟软件开发的关系是什么呢?在线等,感激不尽!!!
—— 说明出现空指针了,后面再出现其他错误就更正常了。但是:
} catch (Exception e) {
System.out.println("出现异常1:"+e.getMessage());
}这习惯非常不好,让人不知道问题位置,请修改为:
} catch (Exception e) {
System.out.println("出现异常1:"+e.getMessage());
e.printStackTrace(); // 这句话非常非常重要!
}然后看看是哪里出现空指针了。
不是仅仅这句:System.out.println("异常:"+e.getMessage());