【问题标题】:Getting problem in accessing web cam访问网络摄像头时遇到问题
【发布时间】: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


【解决方案1】:

如果您大部分时间都使用内置网络摄像头,它会显示错误。您可以尝试使用外部网络摄像头。

【讨论】:

    【解决方案2】:

    您的代码有些混乱,但您确定您的 Frame 设置了边框布局吗?我已经多次经历过这个问题。尝试明确说明 this.Layout = new BorderLayout();。如果问题仍然存在,请告诉我,我会尽力提供帮助。

    【讨论】:

    • 实际上 CaptureDeviceManager.getDevice 返回 null 这就是发生此异常的原因。我不明白为什么它会给出这个例外。我在 Linux 平台上的 Eclipse 中做这个程序。你能帮我吗?
    【解决方案3】:

    我输入网络摄像头型号:E-1.3MPIR CaptureDeviceManager.getDevice() 的参数应该是什么

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-18
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多