JMF捕获摄像头数据存入本地磁盘(java 多媒体)

首先需要说明的是JMF是个什么东西?这个简单问下百度,在百度里输入JMF后看百度百科就很清楚了,我就不啰嗦了,啰嗦也没人家说的好。哈哈

接下来就说它的安装了,我下载的是2.1.1e版本的,在windows安装直接默认就行了。下载去官网(google里输入JMF)。

下来就说正事了,功能就说用电脑本机上的摄像头采集数据然后存放成视频文件就ok了

好了看代码

先是启动类

 

public class MainClass {

	/**
	 * 程序运行入口
	 * @param args
	 */
	public static void main(String[] args) {
		MainFrame frmMain = new MainFrame();
		try {
			//设置主窗体的样式
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
			SwingUtilities.updateComponentTreeUI(frmMain);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		//设置窗口位于屏幕中央
		frmMain.setLocationRelativeTo(null);
		frmMain.setVisible(true);
	}

}

  再就是主窗体了,都有注释,就不再多做解释了

 

public class MainFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private CaptureDeviceInfo captureDevice;
	private Component visualComponent;
	private Processor processor;
	private Player player;
	private DataSink fileWriter;
	private FormatControl formatControl;
	private JPanel pnlVideo = new JPanel(new BorderLayout());
	JPanel contentPane;
	JPanel pnlControl = new JPanel();
	JButton btnStart = new JButton("开始采集");
	JButton btnStop = new JButton("停止采集");
	JFileChooser fileChooser = new JFileChooser();
	
	public MainFrame()
	{
		super("视频采集软件");
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
			SwingUtilities.updateComponentTreeUI(this);
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}
		contentPane = (JPanel) this.getContentPane();
		//获得视频设备信息
		this.captureDevice = getCaptureDeviceInfo();
		//
		try {
			//获得数据源
			DataSource source =	Manager.createDataSource(captureDevice.getLocator());
			//克隆数据源
			DataSource cloneableSource = Manager.createCloneableDataSource(source);
			//创建player
			player = Manager.createRealizedPlayer(((SourceCloneable)cloneableSource).createClone());
			//获取默认格式,我这里是RGB
			formatControl = (FormatControl) player.getControl("javax.media.control.FormatControl");
			Format defaultFormat = formatControl.getFormat();
			//设置输出文件的格式,这里是avi
			FileTypeDescriptor outputType = new FileTypeDescriptor(FileTypeDescriptor.MSVIDEO);
			ProcessorModel processorModel = new ProcessorModel(cloneableSource, new Format[]{ defaultFormat }, outputType);
			//创建一个处理器
			processor = Manager.createRealizedProcessor(processorModel);
		} catch (Exception e) {
			processException(e);
		}
		//展示图形的swing组件
		visualComponent = player.getVisualComponent();
		if (visualComponent!=null) {
			pnlVideo.add(visualComponent);
			contentPane.add(pnlVideo);
		}
		//
		btnStop.setEnabled(false);
		pnlControl.add(btnStart);
		pnlControl.add(btnStop);
		contentPane.add("South", pnlControl);
		//点击开始事件
		btnStart.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				start();
			}
		});
		btnStop.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				stop();
			}
		});
		//
		pack();
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	/**
	 * 点击开始采集
	 */
	private void start() {
		String locatorString = getLocatorString();
		if(locatorString==null)
		{
			return;
		}
		//开始采集不可用
		btnStart.setEnabled(false);
		//从处理器中拿到数据源
		DataSource source = processor.getDataOutput();
		MediaLocator dest = new MediaLocator( locatorString );
		try {
			//本地文件传输
			fileWriter = Manager.createDataSink(source, dest);
			fileWriter.open();
			fileWriter.start();
		} catch (Exception e) {
			processException(e);
		}
		processor.start();
		player.start();
		btnStop.setEnabled(true);
	}

	private void stop() {
		btnStop.setEnabled(false);
		//停止捕获
		player.stop();
		processor.stop();
		try {
			fileWriter.stop();
			fileWriter.close();
		} catch (IOException e) {
			processException(e);
		}
		btnStart.setEnabled(true);
	}
	
	private void processException(Exception e)
	{
		e.printStackTrace();
		JOptionPane.showMessageDialog(this, e.toString(), "错误", JOptionPane.ERROR_MESSAGE);
		System.exit(0);
	}
	/**
	 * 构建输出路径的URL
	 * @return
	 */
	private String getLocatorString()
	{

		if( JFileChooser.APPROVE_OPTION != fileChooser.showSaveDialog(this))
		{
			return null;
		}
		File file = fileChooser.getSelectedFile();
		if (file==null) {
			return null;
		}
		String locatorString = file.getAbsolutePath();
		if( !locatorString.endsWith(".avi" ))
		{
			locatorString += ".avi";
		}
		locatorString = "file://" + locatorString;
		return locatorString;
	}
	/**
	 * 获得截取视频的设备信息
	 * @return 设备信息
	 */
	private CaptureDeviceInfo getCaptureDeviceInfo()
	{
		//定义视频格式
		Format videoFormat = new VideoFormat(VideoFormat.YUV);
		@SuppressWarnings("unchecked")
		//获得满足条件的视频设备列表
		Vector<CaptureDeviceInfo> deviceList = CaptureDeviceManager.getDeviceList(videoFormat);
		if(deviceList.size()<1)
		{
			JOptionPane.showMessageDialog(this, "未检测到视频输入设备。", "错误", JOptionPane.ERROR_MESSAGE);
			System.exit(0);
		}
		String[] deviceNames = new String[deviceList.size()];
		for (int i = 0; i < deviceList.size(); i ++ ) {			
			deviceNames[i] = deviceList.get(i).getName();			
		}
		String deviceName = (String) JOptionPane.showInputDialog(this, "请选择视频输入设备", "请选择", JOptionPane.QUESTION_MESSAGE, null, deviceNames, deviceNames[0]);
		if (deviceName==null) {
			System.exit(0);
		}
		//
		CaptureDeviceInfo captureDevice;
		for (int i = 0; i < deviceList.size(); i ++ ) {	
			captureDevice = deviceList.get(i);
			if( deviceName.equals(captureDevice.getName()) )
			{
				return captureDevice;
			}
		}
		return null;
	}
}

 ok,经过测试OK,以作记录!最后将程序和JMF安装包附上。

你可能感兴趣的:(java,swing,Google,百度,sun)