3 * (c)2008 A虎@nyatla.jp
\r
4 * airmail@ebony.plala.or.jp
\r
7 package jp.nyatla.nyartoolkit.jmf.utils;
\r
11 import javax.media.*;
\r
12 import javax.media.protocol.*;
\r
13 import javax.media.control.*;
\r
14 import javax.media.format.*;
\r
17 import javax.media.protocol.DataSource;
\r
19 import jp.nyatla.nyartoolkit.NyARException;
\r
25 public class JmfCameraCapture
\r
27 private Dimension image_size;
\r
28 private JmfCaptureListener capture_listener;
\r
29 private DataSource jmf_data_source;
\r
30 private MonitorStream jmf_monitor_stream;
\r
31 private Processor jmf_processor;
\r
32 private VideoFormat jmf_video_format;
\r
34 private Buffer read_buf=new Buffer();
\r
35 public static final String PIXEL_FORMAT_RGB="RGB";
\r
36 public JmfCameraCapture(int i_width,int i_height,float i_rate,String i_pixcel_format)
\r
38 String encoding = i_pixcel_format;//comboEncoding.getSelectedItem();
\r
39 image_size = new Dimension(i_width,i_height);
\r
40 jmf_video_format = new VideoFormat(encoding, image_size, Format.NOT_SPECIFIED,null,i_rate);
\r
42 public Dimension getSize()
\r
46 public javax.media.Buffer readBuffer() throws NyARException
\r
48 if(jmf_monitor_stream==null){
\r
49 throw new NyARException();
\r
52 jmf_monitor_stream.read(read_buf);
\r
53 }catch(Exception e){
\r
54 throw new NyARException(e);
\r
58 public void setCaptureListener(JmfCaptureListener i_listener) throws NyARException
\r
60 if(jmf_processor!=null){
\r
61 throw new NyARException();
\r
63 capture_listener=i_listener;
\r
66 public void start() throws NyARException
\r
69 DataSource ds=getCaptureDS(jmf_video_format);
\r
70 VideoFormat[] formats=new VideoFormat[]{new VideoFormat(null)};
\r
71 ProcessorModel pm = new ProcessorModel(ds,formats,null);//, formats, ftd);
\r
72 Processor processor;
\r
74 processor = Manager.createRealizedProcessor(pm);
\r
75 } catch (Exception e){
\r
76 // Make sure the capture devices are released
\r
78 throw new NyARException(e);
\r
80 // Get the monitor control:
\r
81 // Since there are more than one MonitorControl objects
\r
82 // exported by the DataSource, we get the specific one
\r
83 // that is also the MonitorStream object.
\r
84 jmf_monitor_stream=(MonitorStream)ds.getControl("jmfsample.MonitorStream");
\r
85 jmf_monitor_stream.setCaptureListener(capture_listener);
\r
87 jmf_processor=processor;
\r
88 jmf_processor.start();
\r
92 jmf_processor.stop();
\r
93 jmf_processor.close();
\r
94 jmf_processor = null;
\r
97 protected void finalize()
\r
99 if(jmf_processor!=null){
\r
100 jmf_processor.stop();
\r
101 jmf_processor.close();
\r
102 jmf_processor = null;
\r
105 private static DataSource getCaptureDS(VideoFormat vf) {
\r
106 DataSource dsVideo = null;
\r
107 DataSource ds = null;
\r
109 // Create a capture DataSource for the video
\r
110 // If there is no video capture device, then exit with null
\r
112 dsVideo = createDataSource(vf);
\r
113 if (dsVideo == null)
\r
118 // Create the monitoring datasource wrapper
\r
119 if (dsVideo != null) {
\r
120 dsVideo = new MonitorCDS(dsVideo);
\r
124 // Merge the data sources, if both audio and video are available
\r
126 ds = Manager.createMergingDataSource(new DataSource[]{dsVideo});
\r
127 } catch (IncompatibleSourceException ise){
\r
134 private static DataSource createDataSource(Format format) {
\r
137 CaptureDeviceInfo cdi;
\r
140 // Find devices for format
\r
141 devices = CaptureDeviceManager.getDeviceList(format);
\r
142 if (devices.size() < 1) {
\r
143 System.err.println("! No Devices for " + format);
\r
146 // Pick the first device
\r
147 cdi = (CaptureDeviceInfo) devices.elementAt(0);
\r
149 ml = cdi.getLocator();
\r
152 ds = Manager.createDataSource(ml);
\r
154 if (ds instanceof CaptureDevice)
\r
156 setCaptureFormat((CaptureDevice) ds, format);
\r
158 } catch (Exception e) {
\r
159 System.err.println(e);
\r
165 private static void setCaptureFormat(CaptureDevice cdev, Format format) {
\r
166 FormatControl [] fcs = cdev.getFormatControls();
\r
167 if (fcs.length < 1){
\r
170 FormatControl fc = fcs[0];
\r
171 Format [] formats = fc.getSupportedFormats();
\r
172 for (int i = 0; i < formats.length; i++) {
\r
173 if (formats[i].matches(format)){
\r
174 format = formats[i].intersects(format);
\r
175 fc.setFormat(format);
\r