【问题标题】:getting NullPointerException at javax.media.Manager.createPlayer(Manager.java:482)在 javax.media.Manager.createPlayer(Manager.java:482) 处获取 NullPointerException
【发布时间】:2013-12-07 09:22:29
【问题描述】:

我正在尝试为 java 应用程序配置网络摄像头

代码报错

在 javax.media.Manager.createPlayer(Manager.java:482) 处获取 NullPointerException

排队

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

来源:

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());
    }

  } 



}

【问题讨论】:

标签: java swing webcam jmf


【解决方案1】:

您需要在块中添加break

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;

因为如果有多个源作为设备,那么您将使用可能是另一个源的最后一个源,并且通常网络摄像头是 Java 媒体框架中设备列表中的第一个设备

而当你想使用一个播放器时,你必须首先通过调用player.realize()或者通过使用Manager.createRealizedPlayer(source)创建一个已实现的播放器来实现它(方法名称可能是准确的)
Player.realize不是一个阻塞函数意味着它将在不同的线程上执行,而 Manager.createRealizedPlayer(source) 是一个阻塞函数,将停止执行直到玩家被实现
认识玩家后可以拨打Player.start()

【讨论】:

    猜你喜欢
    • 2015-06-17
    • 2014-10-17
    • 2017-12-23
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多