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



我正在尝试为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

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

相关内容

  • 没有找到相关文章

最新更新