JMF 摄像头 视频捕捉拍照 源代码

 最近用到JMF,做一个摄像头捕捉程序,一猜,肯定网上有源码,真的有,不过也不是一下子能找到的,所以就做一个转帖,自己在代码上写了少量注释.

 

import javax.media.*; import javax.media.protocol.*; import javax.media.format.*; import javax.media.control.*; import java.util.Vector; public class CaptureUtil { /** * 得到数据源 * @param af 音频格式 * @param vf 视频格式 * @return 数据源 */ protected static DataSource getCaptureDS(AudioFormat af, VideoFormat vf) { DataSource dsVideo = null, dsAudio = null, ds = null; if (vf != null) { dsVideo = createDataSource(vf); if (dsVideo == null) { return null;// 表示没有获取到captureDs; } } if (af != null) { dsAudio = createDataSource(af); } if (dsVideo != null) { // dsVideo = new MonitorCDS(dsVideo); if (dsAudio == null) { return dsVideo; } ds = dsVideo; } else if (dsAudio != null) { return dsAudio; } else { return null; } try { //建立数据源 ds = Manager.createMergingDataSource(new DataSource[] { dsAudio, dsVideo }); } catch (IncompatibleSourceException e) { return null; } return ds; } protected static DataSource createDataSource(Format format) { //获取视频设备列表 Vector<CaptureDeviceInfo> deviceList = CaptureDeviceManager .getDeviceList(format); if (deviceList.size() < 1) { System.out.println("No Device for " + format); return null; } //得到列表中第一个视频设备 CaptureDeviceInfo cdi = deviceList.elementAt(0); //设备定位 MediaLocator ml = cdi.getLocator(); DataSource ds = null; try { //建立数据源并链接 ds = Manager.createDataSource(ml); ds.connect(); if (ds instanceof CaptureDevice) { setCaptureFormat((CaptureDevice) ds, format); } } catch (java.io.IOException e) { System.out.println("IO Exception"); return null; } catch (NoDataSourceException e) { System.out.println("No DataSource Exception"); return null; } return ds; } /** * 设置视频捕捉格式 * @param cd * @param format */ protected static void setCaptureFormat(CaptureDevice cd, Format format) { FormatControl[] formatControls = cd.getFormatControls(); if (formatControls.length < 1) { System.out.println("No Format Control"); return; } FormatControl formatControl = formatControls[0]; Format[] formats = formatControl.getSupportedFormats(); for (int i = 0; i < formats.length; i++) { if (formats[i].matches(format)) { Format f = formats[i].intersects(format); System.out.println(f); formatControl.setFormat(f); break; } } } }

 

import java.awt.Graphics; import javax.swing.JApplet; import java.awt.Panel; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.media.ProcessorModel; import javax.media.Processor; import javax.media.format.VideoFormat; import javax.media.Format; import javax.media.format.AudioFormat; import javax.media.protocol.DataSource; import java.awt.Component; import java.awt.Dimension; import javax.media.protocol.FileTypeDescriptor; import javax.media.Manager; import javax.media.control.MonitorControl; import java.awt.GridLayout; import java.io.File; import javax.imageio.ImageIO; import java.awt.Image; /** * * @author Administrator */ public class VidApplet extends JApplet implements ActionListener { public void actionPerformed(ActionEvent e) { } private Panel panel = new Panel(); private Button button = new Button(); private Processor processor = null; private Component component = null; private Component monitor = null; private DataSource ds = null; private MonitorControl mc = null; private void startMonitoring() { if (processor != null) { processor.stop(); processor.close(); } if (monitor != null) { panel.remove(monitor); monitor = null; } //音频格式 //AudioFormat af = new AudioFormat(AudioFormat.LINEAR); AudioFormat af = new AudioFormat(AudioFormat.LINEAR, 44100, 16, 2); //视频大小 Dimension size = new Dimension(160, 120); //视频格式 VideoFormat vf = new VideoFormat("RGB", size, Format.NOT_SPECIFIED, null, 15); ds = CaptureUtil.getCaptureDS(af, vf); //视频格式 FileTypeDescriptor ftd = new FileTypeDescriptor(FileTypeDescriptor.QUICKTIME); Format[] formats = null; if (af != null && vf != null) { formats = new Format[]{new AudioFormat(null), new VideoFormat(null)}; } if (af == null) { formats = new Format[]{new VideoFormat(null)}; } //处理器 ProcessorModel pm = new ProcessorModel(ds, formats, ftd); try { processor = Manager.createRealizedProcessor(pm); } catch (Exception e) { ds.disconnect(); return; } //监视器控制方式 mc = (MonitorControl) ds.getControl("javax.media.control.MonitorControl"); if (mc != null) { component = mc.getControlComponent(); panel.add(component); mc.setEnabled(true); } panel.add(button); } public void destroy() { processor = null; monitor = null; if (component != null) { panel.remove(component); component = null; } ds = null; super.destroy(); } public void init() { super.init(); button.setLabel("拍照"); button.addActionListener(this); //panel.add(button); panel.setLayout(new GridLayout()); this.add(panel); } public void start() { super.start(); startMonitoring(); } @Override public void stop() { super.stop(); if (processor != null) { processor.stop(); processor.close(); } if (monitor != null) { panel.remove(monitor); monitor = null; } ds = null; } /*public static void main(String[] args){ VidApplet applet = new VidApplet(); applet.init(); applet.start(); applet.stop(); applet.destroy(); }*/ }

 

 

所需要的JMF的包就自己导入工程就是了,这个只能算是最简单的DEMO

 

你可能感兴趣的:(exception,null,Class,import,button,applet)