访问网络摄像头时遇到问题

时间:2010-03-30 03:58:26

标签: java

我用Java编写代码来访问网络摄像头,并保存图像...... 我得到以下例外: 线程“main”java.lang.NullPointerException中的异常  在SwingCapture。(SwingCapture.java:40)  在SwingCapture.main(SwingCapture.java:66)

如何删除此例外。

这是代码:

import javax.swing.*;

import javax.swing.event。; import java.io。; import javax.media。; import javax.media.format。; import javax.media.util。; import javax.media.control。; import javax.media.protocol。; import java.util。; import java.awt。; import java.awt.image。; import java.awt.event。; import com.sun.image.codec.jpeg。;

public class SwingCapture extends Panel实现了ActionListener {   public static Player player = null;   public CaptureDeviceInfo di = null;   public MediaLocator ml = null;   public JButton capture = null;   public Buffer buf = null;   public Image img = null;   public VideoFormat vf = null;   public BufferToImage btoi = null;   public ImagePanel imgpanel = null;

public SwingCapture()   {     setLayout(new BorderLayout());     的setSize(320550);

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 =新帧(“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);

}

public static void playerclose()   {     player.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扩展了Panel   {     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"); 
}

}

}

3 个答案:

答案 0 :(得分:0)

您的代码有些混乱,但是您确定指定您的Frame有一个borderlayout设置吗?我多次经历过这个问题。尝试明确说明this.Layout = new BorderLayout();.如果问题仍然存在,请告诉我并尽力帮助。

答案 1 :(得分:0)

如果您在大多数情况下使用内置网络摄像头,则会显示错误。您可以尝试使用外部网络摄像头。

答案 2 :(得分:0)

我有输入网络相机型号:E-1.3MPIR应该是CaptureDeviceManager.getDevice()的参数