导出为JAR时,不支持带有JMF和Eclipse的major.minor 51.0版

导出为JAR时,不支持带有JMF和Eclipse的major.minor 51.0版,eclipse,jar,nullpointerexception,jmf,Eclipse,Jar,Nullpointerexception,Jmf,我正在编写一个简短的程序,使用Java媒体框架(JMF)从网络摄像头中提取图像 似乎我遇到了一个很多人都遇到过的问题,但却没有一个明确的解决方案。我正在使用eclipse,在那里我的程序运行良好。我添加了jmf.jar作为外部库 现在的问题是,如果我将程序导出为jar,那么在命令行上运行它时会出现以下错误: Exception in thread "main" java.lang.NullPointerException at recorder.SimpleRecorder.<in

我正在编写一个简短的程序,使用Java媒体框架(JMF)从网络摄像头中提取图像

似乎我遇到了一个很多人都遇到过的问题,但却没有一个明确的解决方案。我正在使用eclipse,在那里我的程序运行良好。我添加了jmf.jar作为外部库

现在的问题是,如果我将程序导出为jar,那么在命令行上运行它时会出现以下错误:

Exception in thread "main" java.lang.NullPointerException
    at recorder.SimpleRecorder.<init>(SimpleRecorder.java:46)
    at recorder.SimpleRecorder.main(SimpleRecorder.java:67)

}需要检查的一件事是eclipse.ini。我看到工作区安装的jre与eclipse.ini指定的vm不一致的问题。如果ini使用1.6,workspace使用1.7,那么一些eclipse插件可能会尝试使用1.6工具处理1.7字节码或源代码,从而导致各种问题

  • 编辑\eclipse.ini
  • 检查vm是否设置为预期版本

    -虚拟机 c:/java/jdk1.7.0_05/jre/bin/server/jvm.dll

package recorder;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;

import javax.media.Buffer;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.JFrame;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class SimpleRecorder extends JFrame{

public static Player player = null;
public CaptureDeviceInfo di = null;
public MediaLocator ml = null;
public Buffer buf = null;
public Image img = null;
public VideoFormat vf = null;
public BufferToImage btoi = null;

public SimpleRecorder(String title) {
    super(title);

    String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
    di = CaptureDeviceManager.getDevice(str2);

    if(di == null) System.out.println("di is null.");

    ml = di.getLocator();

    try 
    {
      player = Manager.createRealizedPlayer(ml);
      player.start();
      Component comp;

      if ((comp = player.getVisualComponent()) != null)
      {
        add(comp,BorderLayout.CENTER);
      }

    } 
    catch (Exception e) 
    {
      e.printStackTrace();
    }
}

public static void main(String[] args){
    SimpleRecorder frame = new SimpleRecorder("Simple Recorder");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setSize(335,275);
    frame.setLayout(new BorderLayout());
    frame.setVisible(true);

    frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
          playerclose();
          System.exit(0);}});

    System.out.println("Waiting for camera to get ready...");

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

    System.out.println("Start recording...");
    while(true){

        frame.recordImage();

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

private void recordImage(){

     // Grab a frame
      FrameGrabbingControl fgc = (FrameGrabbingControl)
      player.getControl("javax.media.control.FrameGrabbingControl");
      buf = fgc.grabFrame();

      // Convert it to an image
      btoi = new BufferToImage((VideoFormat)buf.getFormat());
      img = btoi.createImage(buf);

      // Get current directory
      String currentDir = new File("./Extracted_Image.jpg").getAbsolutePath();

      // save image
      saveJPG(img,currentDir);
}

public static void saveJPG(Image img, String s)
  {
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = bi.createGraphics();
    g2.drawImage(img, null, null);

    FileOutputStream out = null;
    try
    { 
      out = new FileOutputStream(s); 
    }
    catch (java.io.FileNotFoundException io)
    { 
        System.out.println("File Not Found"); 
    }

    if(out == null) System.out.println("Could not create file output stream.");

    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
    param.setQuality(0.5f,false);
    encoder.setJPEGEncodeParam(param);

    try 
    { 
      encoder.encode(bi); 
      out.close(); 
    }
    catch (java.io.IOException io) 
    {
      System.out.println("IOException"); 
    }
  }

public static void playerclose() 
  {
    player.close();
    player.deallocate();
  }