getting NullPointerException at javax.media.Manager.createPlayer(Manager.java:482)

522 views Asked by At

I'm trying to configure a webcam for a java application

code give error

getting NullPointerException at javax.media.Manager.createPlayer(Manager.java:482)

in line

videoDataSource = Manager.createDataSource(videoDevice.getLocator());

Source:

import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.ControllerAdapter;
import javax.media.ControllerEvent;
import javax.media.Format;
import javax.media.Manager;
import javax.media.NoDataSourceException;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.protocol.DataSource;
import javax.media.util.BufferToImage;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class TestWebCam {

  static JPanel             panel       = new JPanel();
  static JFrame             myFrame     = new JFrame();
  static Player             player      = null;

  static CaptureDeviceInfo  videoDevice = null;
  static VideoFormat        videoFormat = null;

  public void actionPerformed(ActionEvent ae) { }

  public static void main(String[] argv) {

    //PANEL.           
    panel = new JPanel();
    panel.setLayout(new FlowLayout());      

    //CREATE FRAME.
    myFrame = new JFrame();
    myFrame.setVisible(true);
    myFrame.setSize(300,300);
    myFrame.getContentPane().add(panel);     
    myFrame.addWindowListener(

      new WindowAdapter(){
        public void windowClosing(WindowEvent event){  
     //     player.close();
          myFrame.dispose();
        }

      }

    );                         

    //GET ALL MEDIA DEVICES.
    Vector deviceListVector = CaptureDeviceManager.getDeviceList(null);

    //CHOOSE AUDIO DEVICES & FORMAT.
    for (int x = 0; x < deviceListVector.size(); x++)    {

      CaptureDeviceInfo device         = (CaptureDeviceInfo) deviceListVector.elementAt(x);
      String            deviceName     = device.getName();           
      Format            deviceFormat[] = device.getFormats();


      for (int y = 0; y < deviceFormat.length; y++)      {                      
        if (videoDevice == null && deviceFormat[y] instanceof VideoFormat) {
          videoFormat = (VideoFormat) deviceFormat[y];
          if(videoFormat.toString().indexOf("640")!=-1) {
            videoDevice = device;
            System.out.println(videoFormat);
          }
        }

      }

    }       

    //VIDEO DATA SOURCE.
    DataSource videoDataSource = null;
    try {
        videoDataSource = Manager.createDataSource(videoDevice.getLocator());

    } catch (Exception e) {
        // TODO Auto-generated catch block
        JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
    }

//    DeviceInfo.setFormat(videoDataSource, videoFormat);



    //CREATE PLAYER.
    try {
        player = Manager.createPlayer(videoDataSource);
    } catch (NoPlayerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    player.addControllerListener(

        new ControllerAdapter(){

          public void controllerUpdate(ControllerEvent event){  

            if (event instanceof RealizeCompleteEvent) {

              panel.add(player.getVisualComponent());

              panel.add(player.getControlPanelComponent());

              myFrame.validate();

            }

          }

        }

    );        

    player.start();     



    //GRAB IMAGE.

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

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

    Buffer               buf  = fgc.grabFrame();

    BufferToImage        btoi = new BufferToImage((VideoFormat)buf.getFormat());    

    Image                img  = btoi.createImage(buf);

    saveImagetoFile(img,"Dots.jpg");

    panel.add(new JLabel(new ImageIcon(img)));  //Expand window to see the image.

    panel.validate();

  } 



  static public void saveImagetoFile(Image img, String fileName)  {

    int           w = img.getWidth(null);

    int           h = img.getHeight(null);

    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

    Graphics2D    g2 = bi.createGraphics();

                  g2.drawImage(img, 0, 0, null);

                  g2.dispose();

    String fileType = fileName.substring(fileName.indexOf('.')+1);

    try {
        ImageIO.write(bi, fileType, new File(fileName));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        JOptionPane.showMessageDialog(null, e.getMessage());
    }

  } 



}
1

There are 1 answers

0
AudioBubble On

you need to add break In block :

for (int y = 0; y < deviceFormat.length; y++)      {                      
        if (videoDevice == null && deviceFormat[y] instanceof VideoFormat) {
          videoFormat = (VideoFormat) deviceFormat[y];
          if(videoFormat.toString().indexOf("640")!=-1) {
            videoDevice = device;
            System.out.println(videoFormat);  
            break;  
          }  
        }  
if (videoDevice!=null)   
break;

Because if there is a more than a one source as a device then you will use the last which could be another source , and usually the webcam is the first device in the device list in Java media framework

And when you want to use a player you must realize it first by either calling player.realize() or by creating a realized player by using Manager.createRealizedPlayer(source) (the method names might be in accurate)
Player.realize is not a blocking function meaing it will be executed on a different thread while Manager.createRealizedPlayer(source) is a blocking function and will halt execution until the player is realized
After realizing a player you can call Player.start()