+++ /dev/null
-/**\r
- * JMFお手軽キャプチャクラス\r
- * (c)2008 A虎@nyatla.jp\r
- * airmail@ebony.plala.or.jp\r
- * http://nyatla.jp/\r
- */\r
-package jp.nyatla.nyartoolkit.jmf.utils;\r
-\r
-\r
-\r
-import javax.media.*;\r
-import javax.media.protocol.*;\r
-import javax.media.control.*;\r
-import javax.media.format.*;\r
-import java.awt.*;\r
-import java.util.*;\r
-import javax.media.protocol.DataSource;\r
-\r
-import jp.nyatla.nyartoolkit.NyARException;\r
-\r
-\r
-\r
-\r
-\r
-public class JmfCameraCapture\r
-{\r
- private Dimension image_size;\r
- private JmfCaptureListener capture_listener;\r
- private DataSource jmf_data_source;\r
- private MonitorStream jmf_monitor_stream;\r
- private Processor jmf_processor;\r
- private VideoFormat jmf_video_format;\r
- \r
- private Buffer read_buf=new Buffer();\r
- public static final String PIXEL_FORMAT_RGB="RGB";\r
- public JmfCameraCapture(int i_width,int i_height,float i_rate,String i_pixcel_format)\r
- {\r
- String encoding = i_pixcel_format;//comboEncoding.getSelectedItem();\r
- image_size = new Dimension(i_width,i_height);\r
- jmf_video_format = new VideoFormat(encoding, image_size, Format.NOT_SPECIFIED,null,i_rate);\r
- }\r
- public Dimension getSize()\r
- {\r
- return image_size;\r
- }\r
- public javax.media.Buffer readBuffer() throws NyARException\r
- {\r
- if(jmf_monitor_stream==null){\r
- throw new NyARException();\r
- }\r
- try{\r
- jmf_monitor_stream.read(read_buf);\r
- }catch(Exception e){\r
- throw new NyARException(e);\r
- }\r
- return read_buf;\r
- }\r
- public void setCaptureListener(JmfCaptureListener i_listener) throws NyARException\r
- {\r
- if(jmf_processor!=null){\r
- throw new NyARException();\r
- }\r
- capture_listener=i_listener;\r
- \r
- }\r
- public void start() throws NyARException\r
- {\r
- \r
- DataSource ds=getCaptureDS(jmf_video_format);\r
- VideoFormat[] formats=new VideoFormat[]{new VideoFormat(null)};\r
- ProcessorModel pm = new ProcessorModel(ds,formats,null);//, formats, ftd);\r
- Processor processor;\r
- try {\r
- processor = Manager.createRealizedProcessor(pm);\r
- } catch (Exception e){\r
- // Make sure the capture devices are released\r
- ds.disconnect();\r
- throw new NyARException(e);\r
- }\r
- // Get the monitor control:\r
- // Since there are more than one MonitorControl objects\r
- // exported by the DataSource, we get the specific one\r
- // that is also the MonitorStream object.\r
- jmf_monitor_stream=(MonitorStream)ds.getControl("jmfsample.MonitorStream");\r
- jmf_monitor_stream.setCaptureListener(capture_listener);\r
- jmf_data_source=ds;\r
- jmf_processor=processor;\r
- jmf_processor.start();\r
- }\r
- public void stop()\r
- {\r
- jmf_processor.stop();\r
- jmf_processor.close();\r
- jmf_processor = null;\r
- \r
- }\r
- protected void finalize()\r
- {\r
- if(jmf_processor!=null){\r
- jmf_processor.stop();\r
- jmf_processor.close();\r
- jmf_processor = null;\r
- }\r
- }\r
- private static DataSource getCaptureDS(VideoFormat vf) {\r
- DataSource dsVideo = null;\r
- DataSource ds = null;\r
-\r
- // Create a capture DataSource for the video\r
- // If there is no video capture device, then exit with null\r
- if (vf != null) {\r
- dsVideo = createDataSource(vf);\r
- if (dsVideo == null)\r
- return null;\r
- }\r
-\r
-\r
- // Create the monitoring datasource wrapper\r
- if (dsVideo != null) {\r
- dsVideo = new MonitorCDS(dsVideo);\r
- return dsVideo;\r
- }\r
-\r
- // Merge the data sources, if both audio and video are available\r
- try {\r
- ds = Manager.createMergingDataSource(new DataSource[]{dsVideo});\r
- } catch (IncompatibleSourceException ise){\r
- return null;\r
- }\r
-\r
- return ds;\r
- }\r
-\r
- private static DataSource createDataSource(Format format) {\r
- DataSource ds;\r
- Vector devices;\r
- CaptureDeviceInfo cdi;\r
- MediaLocator ml;\r
-\r
- // Find devices for format\r
- devices = CaptureDeviceManager.getDeviceList(format);\r
- if (devices.size() < 1) {\r
- System.err.println("! No Devices for " + format);\r
- return null;\r
- }\r
- // Pick the first device\r
- cdi = (CaptureDeviceInfo) devices.elementAt(0);\r
-\r
- ml = cdi.getLocator();\r
-\r
- try {\r
- ds = Manager.createDataSource(ml);\r
- ds.connect();\r
- if (ds instanceof CaptureDevice)\r
- {\r
- setCaptureFormat((CaptureDevice) ds, format);\r
- }\r
- } catch (Exception e) {\r
- System.err.println(e);\r
- return null;\r
- }\r
- return ds;\r
- }\r
-\r
- private static void setCaptureFormat(CaptureDevice cdev, Format format) {\r
- FormatControl [] fcs = cdev.getFormatControls();\r
- if (fcs.length < 1){\r
- return;\r
- }\r
- FormatControl fc = fcs[0];\r
- Format [] formats = fc.getSupportedFormats();\r
- for (int i = 0; i < formats.length; i++) {\r
- if (formats[i].matches(format)){\r
- format = formats[i].intersects(format);\r
- fc.setFormat(format);\r
- break;\r
- }\r
- }\r
- }\r
-}
\ No newline at end of file