2012-05-16 196 views
0

我想通过java拍摄网络摄像头的快照。我跟着this question到达了这example。但有从下面的线来一个空指针异常 -使用JMF从网络摄像头拍摄快照

Buffer buf = frameGrabber.grabFrame(); 
Image img = (new BufferToImage((VideoFormat) buf.getFormat()) 
       .createImage(buf)); 
     buffImg = new BufferedImage(img.getWidth(this), img.getHeight(this), 
       BufferedImage.TYPE_INT_RGB); 

通过我观察到的缓冲实际上并不包含数据的调试器。所以我去创建frameGrabber。

frameGrabber = (FrameGrabbingControl) player 
       .getControl("javax.media.control.FrameGrabbingControl"); 

这段代码有问题吗?由于JMFStudio在我的机器上正常工作,但代码无法访问它。谢谢。

+1

*“异常来自下面的行”*我计数3行。为了更快地获得更好的帮助,请发布[SSCCE](http://sscce.org/)和堆栈跟踪。 –

回答

1

我找到了解决方案。 JMF需要时间进行初始化。在这个例子中,我们必须切换一行。把

new Timer(3000, this).start(); 

低于try catch。

整个模块如下所示。

 try { 
      player = Manager.createRealizedPlayer(cdi.getLocator()); 
      player.start(); 
     } catch (NoPlayerException e) { 
      e.printStackTrace(); 
     } catch (CannotRealizeException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
      new Timer(3000, this).start(); 
     // Grab a frame from the capture device 
     frameGrabber = (FrameGrabbingControl) player 
       .getControl("javax.media.control.FrameGrabbingControl"); 
相关问题