The Artima Developer Community
Sponsored Link

Java Answers Forum
grab frame with JMF

2 replies on 1 page. Most recent reply: Jul 3, 2012 12:18 PM by Keren Blau

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
unda sight

Posts: 1
Nickname: undasight
Registered: Mar, 2011

grab frame with JMF Posted: Mar 14, 2011 6:00 AM
Reply to this message Reply
Advertisement
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;
 
public class 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
        public void paint(Graphics g) {
                if (myimg != null) {
                        g.drawImage(myimg, 0, 0, this);
                }
        }
 
        public void setImage(Image img) {
                this.myimg = img;
                repaint();
        }
    }    
 
    public static void main(String[] args) throws InterruptedException, IOException {
        Frame f = new Frame("iVision");
        final eCamVison ecv = new eCamVison();
        f.addWindowListener(new WindowAdapter() {
            @Override
            public void 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());
        }
    }
 
    public void actionPerformed(ActionEvent e) {
        
    }
 
    public void 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());
        }
    }
 
    public void 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);
        }
    }
 
    public void playerclose() {        
        player.close();
        player.deallocate();
    }
 
    public void exit() {
        System.exit(0);
    }
 
}
 
 

Can you please help me resolving this exception
thanking you in advance,


nandan saha

Posts: 1
Nickname: baduin
Registered: May, 2012

Re: grab frame with JMF Posted: May 5, 2012 5:27 PM
Reply to this message Reply
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();


the fgc remains null
i.e.


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


part is not working..

Keren Blau

Posts: 11
Nickname: kerenblau8
Registered: Jul, 2012

Re: grab frame with JMF Posted: Jul 3, 2012 12:18 PM
Reply to this message Reply
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 :)

Flat View: This topic has 2 replies on 1 page
Topic: Java Forum Previous Topic   Next Topic Topic: generate a random number

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use