3 * (c)2008 A虎@nyatla.jp
\r
4 * airmail@ebony.plala.or.jp
\r
7 package jp.nyatla.nyartoolkit.jmf.utils;
\r
9 import javax.media.*;
\r
10 import javax.media.protocol.*;
\r
11 import javax.media.control.*;
\r
12 import javax.media.format.*;
\r
15 import javax.media.protocol.DataSource;
\r
17 import jp.nyatla.nyartoolkit.core.NyARException;
\r
19 public class JmfCameraCapture
\r
21 private Dimension image_size;
\r
23 private JmfCaptureListener capture_listener;
\r
25 // private DataSource jmf_data_source;
\r
26 private MonitorStream jmf_monitor_stream;
\r
28 private Processor jmf_processor;
\r
30 private VideoFormat jmf_video_format;
\r
32 private Buffer read_buf = new Buffer();
\r
34 public static final String PIXEL_FORMAT_RGB = "RGB";
\r
36 public JmfCameraCapture(int i_width, int i_height, float i_rate,
\r
37 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,
\r
41 Format.NOT_SPECIFIED, null, i_rate);
\r
44 public Dimension getSize() {
\r
48 public javax.media.Buffer readBuffer() throws NyARException {
\r
49 if (jmf_monitor_stream == null) {
\r
50 throw new NyARException();
\r
53 jmf_monitor_stream.read(read_buf);
\r
54 } catch (Exception e) {
\r
55 throw new NyARException(e);
\r
60 public void setCaptureListener(JmfCaptureListener i_listener)
\r
61 throws NyARException {
\r
62 if (jmf_processor != null) {
\r
63 throw new NyARException();
\r
65 capture_listener = i_listener;
\r
69 public void start() throws NyARException {
\r
71 DataSource ds = getCaptureDS(jmf_video_format);
\r
72 VideoFormat[] formats = new VideoFormat[] { new VideoFormat(null) };
\r
73 ProcessorModel pm = new ProcessorModel(ds, formats, null);// ,
\r
76 Processor processor;
\r
78 processor = Manager.createRealizedProcessor(pm);
\r
79 } catch (Exception e) {
\r
80 // Make sure the capture devices are released
\r
82 throw new NyARException(e);
\r
84 // Get the monitor control:
\r
85 // Since there are more than one MonitorControl objects
\r
86 // exported by the DataSource, we get the specific one
\r
87 // that is also the MonitorStream object.
\r
88 jmf_monitor_stream = (MonitorStream) ds
\r
89 .getControl("jmfsample.MonitorStream");
\r
90 jmf_monitor_stream.setCaptureListener(capture_listener);
\r
91 // jmf_data_source=ds;
\r
92 jmf_processor = processor;
\r
93 jmf_processor.start();
\r
96 public void stop() {
\r
97 jmf_processor.stop();
\r
98 jmf_processor.close();
\r
99 jmf_processor = null;
\r
103 protected void finalize() {
\r
104 if (jmf_processor != null) {
\r
105 jmf_processor.stop();
\r
106 jmf_processor.close();
\r
107 jmf_processor = null;
\r
111 private static DataSource getCaptureDS(VideoFormat vf) {
\r
112 DataSource dsVideo = null;
\r
113 DataSource ds = null;
\r
115 // Create a capture DataSource for the video
\r
116 // If there is no video capture device, then exit with null
\r
118 dsVideo = createDataSource(vf);
\r
119 if (dsVideo == null)
\r
123 // Create the monitoring datasource wrapper
\r
124 if (dsVideo != null) {
\r
125 dsVideo = new MonitorCDS(dsVideo);
\r
129 // Merge the data sources, if both audio and video are available
\r
131 ds = Manager.createMergingDataSource(new DataSource[] { dsVideo });
\r
132 } catch (IncompatibleSourceException ise) {
\r
139 private static DataSource createDataSource(Format format)
\r
143 CaptureDeviceInfo cdi;
\r
146 // Find devices for format
\r
147 devices = CaptureDeviceManager.getDeviceList(format);
\r
148 if (devices.size() < 1) {
\r
149 System.err.println("! No Devices for " + format);
\r
152 // Pick the first device
\r
153 cdi = (CaptureDeviceInfo) devices.elementAt(0);
\r
155 ml = cdi.getLocator();
\r
158 ds = Manager.createDataSource(ml);
\r
160 if (ds instanceof CaptureDevice) {
\r
161 setCaptureFormat((CaptureDevice) ds, format);
\r
163 } catch (Exception e) {
\r
164 System.err.println(e);
\r
170 private static void setCaptureFormat(CaptureDevice cdev, Format format)
\r
172 FormatControl[] fcs = cdev.getFormatControls();
\r
173 if (fcs.length < 1) {
\r
176 FormatControl fc = fcs[0];
\r
177 Format[] formats = fc.getSupportedFormats();
\r
178 for (int i = 0; i < formats.length; i++) {
\r
179 if (formats[i].matches(format)) {
\r
180 format = formats[i].intersects(format);
\r
181 fc.setFormat(format);
\r