2 Replies - 5928 Views - Last Post: 26 June 2009 - 02:37 PM Rate Topic: -----

#1 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Java Media Framework, null pointer

Posted 24 June 2009 - 04:55 AM

Does anyone know why I might be getting a null pointer exception, specifically pointing to the last line in this code
String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
ml = di.getLocator();


I have run the JMF registry and when i do capture device i get what is stated in str2. This is my first time using the JMF so I am not very familiar with the whole process. I borrowed some code for now which is suppossed to show the webcam in a frame and allow an image to be captured. This is the full code if needed.
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 implements 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(320,550);
	
	imgpanel = new ImagePanel();
	capture = new JButton("Capture");
	capture.addActionListener(this);
	
	String str1 = "vfw:Logitech USB Video Camera: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) 
  {
	Frame 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);
  }
  
  
  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,"c:\\test.jpg");
	}
  }
  
  class ImagePanel extends 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"); 
	}
  }
  
}


cheers

Is This A Good Question/Topic? 0
  • +

Replies To: Java Media Framework, null pointer

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Java Media Framework, null pointer

Posted 26 June 2009 - 03:24 AM

Quote

specifically pointing to the last line in this code


That would most likely be due to 'di' being null, the device having not been found by the previous call
Was This Post Helpful? 0
  • +
  • -

#3 nick2price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Java Media Framework, null pointer

Posted 26 June 2009 - 02:37 PM

I have actually changed it a bit from that now and have gotten it all to work. The problem was really surprising to me. Basically, any webcam display above the resolution of 320 by 240 would show me a bright pink screen, and play a really screeching sound. This error had suppossedly been fixed by sun, but I guess not. In order to compensate for this, I am using quite a complicated resolution setting method like so
	
public boolean requestCaptureFormat(DataSource ds) {
 
		if (ds instanceof CaptureDevice) {
			FormatControl[] fcs = ((CaptureDevice) ds).getFormatControls();
			for (FormatControl fc : fcs) {
				Format[] formats = ((FormatControl) fc).getSupportedFormats();
				for (Format format : formats) {
					if ((format instanceof VideoFormat) &&
						(((VideoFormat)format).getSize().getHeight() <= 240) &&
						(((VideoFormat)format).getSize().getWidth()  <= 320)) {
							((FormatControl) fc).setFormat(format);
							return true;
					}
				}
			}
		}
		return false;
	}


It does the trick, so I cant complain. Now I need to display a red box in this display to indicate where the persons face should be, hook it up to my neural network facial recognition program, and I'm good to go. Year 3 Project finished before even starting year 3!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1