Hi i am about to develop a simple java API that grab a frame (an image) from my video capture device (web cam). my API runs very well when i implement the grab frame method in a jbutton action performed. Once i move this method to be run directly without any action triggering check it fails: Exception in thread "main" java.lang.NullPointerException
package vision;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
publicclass eCamVison extends Panel implements ActionListener {
public Player player;
public CaptureDeviceInfo di;
public MediaLocator ml;
public Buffer buf;
public Image img;
public VideoFormat vf;
public BufferToImage btoi;
public BufferedImage bi;
public FrameGrabbingControl fgc;
public Vector videoCapDevList = null;
public ImagePanel imgpanel;
class ImagePanel extends Panel {
public Image myimg;
public ImagePanel() {
setLayout(null);
setSize(320, 240);
}
@Override
publicvoid paint(Graphics g) {
if (myimg != null) {
g.drawImage(myimg, 0, 0, this);
}
}
publicvoid setImage(Image img) {
this.myimg = img;
repaint();
}
}
publicstaticvoid main(String[] args) throws InterruptedException, IOException {
Frame f = new Frame("iVision");
final eCamVison ecv = new eCamVison();
f.addWindowListener(new WindowAdapter() {
@Override
publicvoid windowClosing(WindowEvent e) {
ecv.playerclose();
System.exit(0);
}
});
f.dispose();
f.add("Center", ecv);
f.pack();
f.setVisible(true);
ecv.capture();
ecv.playerclose();
ecv.exit();
}
public eCamVison() {
setLayout(new BorderLayout());
imgpanel = new ImagePanel();
try {
vf = new VideoFormat(VideoFormat.YUV);
videoCapDevList = CaptureDeviceManager.getDeviceList(vf);
if (videoCapDevList.size() > 0) {
CaptureDeviceInfo deviceInfo = (CaptureDeviceInfo) videoCapDevList.elementAt(0);
ml = deviceInfo.getLocator();
deviceInfo = CaptureDeviceManager.getDevice(""
+ "vfw:Microsoft WDM Image Capture (Win32):0");
//CaptureDeviceManager.commit();
System.out.println("video / Device name is: " +
deviceInfo.getName());
System.out.println("video / Device supported formats are: " +
deviceInfo.getFormats());
player = Manager.createRealizedPlayer(deviceInfo.getLocator());
player.start();
Component comp = player.getVisualComponent();
if (comp != null) {
add(comp, BorderLayout.NORTH);
}
add(imgpanel, BorderLayout.SOUTH);
}
}
catch (Exception e) {
System.out.println("capture device not found");
System.err.println(e.getMessage());
}
}
publicvoid actionPerformed(ActionEvent e) {
}
publicvoid savePNG(Image img, String s) throws IOException {
bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, null, null);
FileOutputStream out;
try {
out = new FileOutputStream(s);
boolean write = ImageIO.write((RenderedImage) img, "png", out);
try {
out.close();
}
catch (java.io.IOException io) {
System.out.println("IOException");
System.err.println(io.getMessage());
}
}
catch (java.io.FileNotFoundException fnf) {
System.out.println("File Not Found");
System.err.println(fnf.getMessage());
}
}
publicvoid capture() throws IOException, InterruptedException {
// Grab a frame
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);
try {
// save image
savePNG(img, "test.png");
} catch (IOException ex) {
Logger.getLogger(eCamVison.class.getName()).log(Level.SEVERE, null, ex);
}
}
publicvoid playerclose() {
player.close();
player.deallocate();
}
publicvoid exit() {
System.exit(0);
}
}
Can you please help me resolving this exception thanking you in advance,
hey i am stuck in this same place too but so far i find fgc = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl"); buf = fgc.grabFrame();
Hi, from looking at your code, you have several places where your variable = null. You should backtrack in your code and re-check to see why you have variables with values that are not valid, and then change the code so it contains valid values in all variables. Hope this helps you resolve this issue. As for nandan saha’s post – if you can add a more comprehensive example of your code maybe I could help with some advice. Cheers, Keren :)