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

标签 java swing webcam jmf

我正在尝试为 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());
    }

  } 



}

最佳答案

您需要添加break在 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;

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

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

关于java - 在 javax.media.Manager.createPlayer(Manager.java :482)) 处获取 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20439728/

相关文章:

Java setBounds 不适用于 JPanel

c - Linux 上 C 的网络摄像头库?

php - 通过流网络摄像头。 Wowza 流媒体服务器

java - 从网络摄像头捕获图像

java - 在 Java 中作弊单继承?

java - 使用 Spring (Java) 的数据模型和 REST Controller 的 Multi-Tenancy 权限模型

java - 使用声明检查模式在有效负载中保留一些信息?

java - JOptionPane 元素之间的交替焦点

java - 每当我选择任何单选按钮时,它都会自动选择最后一个按钮,我该怎么办?

java - 如何从 SFTP 服务器获取文件列表?