【发布时间】:2011-02-02 07:44:17
【问题描述】:
我已经用 Java 编写了代码来访问网络摄像头并保存图像... 我收到以下异常: 线程“主”java.lang.NullPointerException 中的异常 在 SwingCapture.(SwingCapture.java:40) 在 SwingCapture.main(SwingCapture.java:66)
如何移除此异常。
代码如下:
import javax.swing.*;
导入 javax.swing.event.; 导入 java.io.; 导入 javax.media.; 导入 javax.media.format。; 导入 javax.media.util.; 导入 javax.media.control.; 导入 javax.media.protocol.; 导入 java.util.; 导入 java.awt.; 导入 java.awt.image.; 导入 java.awt.event.; 导入 com.sun.image.codec.jpeg.;
公共类 SwingCapture 扩展 Panel 实现 ActionListener { 公共静态播放器播放器=空; 公共 CaptureDeviceInfo di = null; 公共媒体定位器 ml = null; 公共 JButton 捕获 = null; 公共缓冲区 buf = null; 公共图像 img = null; 公共视频格式 vf = null; 公共 BufferToImage btoi = null; public ImagePanel imgpanel = null;
公共 SwingCapture() { 设置布局(新边框布局()); setSize(320,550);
imgpanel = new ImagePanel();
capture = new JButton("Capture");
capture.addActionListener(this);
String str1 = "vfw:iNTEX IT-308 WC:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
ml = di.getLocator();
try
{
player = Manager.createRealizedPlayer(ml);
player.start();
Component comp;
if ((comp = player.getVisualComponent()) != null)
{
add(comp,BorderLayout.NORTH);
}
add(capture,BorderLayout.CENTER);
add(imgpanel,BorderLayout.SOUTH);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) { 帧 f = new Frame("SwingCapture"); SwingCapture cf = new SwingCapture();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
playerclose();
System.exit(0);}});
f.add("Center",cf);
f.pack();
f.setSize(new Dimension(320,550));
f.setVisible(true);
}
公共静态无效播放器关闭() { 播放器.close(); player.deallocate(); }
public void actionPerformed(ActionEvent e) { JComponent c = (JComponent) e.getSource();
if (c == capture)
{
// Grab a frame
FrameGrabbingControl fgc = (FrameGrabbingControl)
player.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
// Convert it to an image
btoi = new BufferToImage((VideoFormat)buf.getFormat());
img = btoi.createImage(buf);
// show the image
imgpanel.setImage(img);
// save image
saveJPG(img,"\test.jpg");
}
}
类 ImagePanel 扩展面板 { public Image myimg = null;
public ImagePanel()
{
setLayout(null);
setSize(320,240);
}
public void setImage(Image img)
{
this.myimg = img;
repaint();
}
public void paint(Graphics g)
{
if (myimg != null)
{
g.drawImage(myimg, 0, 0, this);
}
}
}
public static void saveJPG(Image img, String s) { BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = bi.createGraphics(); g2.drawImage(img, null, null);
FileOutputStream out = null;
try
{
out = new FileOutputStream(s);
}
catch (java.io.FileNotFoundException io)
{
System.out.println("File Not Found");
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(0.5f,false);
encoder.setJPEGEncodeParam(param);
try
{
encoder.encode(bi);
out.close();
}
catch (java.io.IOException io)
{
System.out.println("IOException");
}
}
}
【问题讨论】:
-
这就是
ml = di.getLocator();,前面是di = CaptureDeviceManager.getDevice(str2);。我猜CaptureDeviceManager.getDevice返回了null,可能是出于其 API 文档中列出的原因。 -
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";这是针对 MS Windows 的,我使用的是 Ubuntu Linux。我认为在 Linux 中必须替换此行。
标签: java