Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javax.media.Manager.createPlayer(Manager.java:482)获取NullPointerException_Java_Swing_Webcam_Jmf - Fatal编程技术网

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

在javax.media.Manager.createPlayer(Manager.java:482)获取NullPointerException,java,swing,webcam,jmf,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.CaptureDevic

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

  } 



}
import javax.media.CaptureDeviceInfo;
导入javax.media.CaptureDeviceManager;
导入javax.media.ControllerAdapter;
导入javax.media.ControllerEvent;
导入javax.media.Format;
导入javax.media.Manager;
导入javax.media.NoDataSourceException;
导入javax.media.Player;
导入javax.media.RealizeCompleteEvent;
导入javax.media.control.FrameGrabbingControl;
导入javax.media.format.VideoFormat;
导入javax.media.protocol.DataSource;
导入javax.media.util.BufferToImage;
导入javax.swing.ImageIcon;
导入javax.swing.JOptionPane;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
公共类测试网络摄像机{
静态JPanel面板=新JPanel();
静态JFrame myFrame=newjframe();
静态播放器=空;
静态CaptureDeviceInfo视频设备=空;
静态VideoFormat VideoFormat=null;
已执行的公共无效操作(操作事件ae){}
公共静态void main(字符串[]argv){
//小组。
panel=新的JPanel();
panel.setLayout(新的FlowLayout());
//创建框架。
myFrame=newjframe();
myFrame.setVisible(true);
myFrame.setSize(300300);
myFrame.getContentPane().add(面板);
myFrame.addWindowListener(
新的WindowAdapter(){
公共无效窗口关闭(WindowEvent事件){
//player.close();
myFrame.dispose();
}
}
);                         
//获取所有媒体设备。
Vector deviceListVector=CaptureDeviceManager.getDeviceList(null);
//选择音频设备和格式。
对于(int x=0;x
您需要在块中添加
中断

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;
for(int y=0;y
因为如果有不止一个源作为一个设备,那么您将使用最后一个源,它可能是另一个源,并且通常网络摄像头是Java media framework中设备列表中的第一个设备

当您想使用播放器时,必须首先通过调用
player.realize()
或使用
Manager.createRealizedPlayer(源代码)
创建一个已实现的播放器(方法名称可能准确)
Player.realize
不是一个阻塞函数,意味着它将在
管理时在不同的线程上执行