2 * PROJECT: NyARToolkit JMF utilities.
\r
3 * --------------------------------------------------------------------------------
\r
5 * Copyright (c) 2008 nyatla
\r
6 * airmail(at)ebony.plala.or.jp
\r
7 * http://nyatla.jp/nyartoolkit/
\r
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
10 * of this software and associated documentation files (the "Software"), to deal
\r
11 * in the Software without restriction, including without limitation the rights
\r
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
13 * copies of the Software, and to permit persons to whom the Software is
\r
14 * furnished to do so, subject to the following conditions:
\r
15 * The above copyright notice and this permission notice shall be included in
\r
16 * all copies or substantial portions of the Software.
\r
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
27 package jp.nyatla.nyartoolkit.jmf.utils;
\r
29 import javax.media.*;
\r
30 import javax.media.protocol.*;
\r
31 import javax.media.control.*;
\r
32 import javax.media.format.*;
\r
35 import javax.media.protocol.DataSource;
\r
37 import jp.nyatla.nyartoolkit.NyARException;
\r
41 * @deprecated このクラスは近い将来削除します。
\r
42 * JmfCaptureDeviceList/JmfCaptureDeviceを使用してください。
\r
43 * {@link #JmfCaptureDeviceList()}
\r
46 @Deprecated public class JmfCameraCapture
\r
48 private Dimension image_size;
\r
50 private JmfCaptureListener capture_listener;
\r
52 // private DataSource jmf_data_source;
\r
53 private MonitorStream jmf_monitor_stream;
\r
55 private Processor jmf_processor;
\r
57 private VideoFormat jmf_video_format;
\r
59 private Buffer read_buf = new Buffer();
\r
61 public static final String PIXEL_FORMAT_RGB = "RGB";
\r
63 public JmfCameraCapture(int i_width, int i_height, float i_rate, String i_pixcel_format)
\r
65 String encoding = i_pixcel_format;// comboEncoding.getSelectedItem();
\r
66 image_size = new Dimension(i_width, i_height);
\r
67 jmf_video_format = new VideoFormat(encoding, image_size, Format.NOT_SPECIFIED, null, i_rate);
\r
70 public Dimension getSize()
\r
75 public javax.media.Buffer readBuffer() throws NyARException
\r
77 if (jmf_monitor_stream == null) {
\r
78 throw new NyARException();
\r
81 jmf_monitor_stream.read(read_buf);
\r
82 } catch (Exception e) {
\r
83 throw new NyARException(e);
\r
88 public void setCaptureListener(JmfCaptureListener i_listener) throws NyARException
\r
90 if (jmf_processor != null)
\r
92 throw new NyARException();
\r
94 capture_listener = i_listener;
\r
98 public void start() throws NyARException
\r
101 DataSource ds = getCaptureDS(jmf_video_format);
\r
102 VideoFormat[] formats = new VideoFormat[] { new VideoFormat(null) };
\r
103 ProcessorModel pm = new ProcessorModel(ds, formats, null);// ,
\r
106 Processor processor;
\r
108 processor = Manager.createRealizedProcessor(pm);
\r
109 } catch (Exception e) {
\r
110 // Make sure the capture devices are released
\r
112 throw new NyARException(e);
\r
114 // Get the monitor control:
\r
115 // Since there are more than one MonitorControl objects
\r
116 // exported by the DataSource, we get the specific one
\r
117 // that is also the MonitorStream object.
\r
118 jmf_monitor_stream = (MonitorStream) ds.getControl("jmfsample.MonitorStream");
\r
119 jmf_monitor_stream.setCaptureListener(capture_listener);
\r
120 // jmf_data_source=ds;
\r
121 jmf_processor = processor;
\r
122 jmf_processor.start();
\r
127 jmf_processor.stop();
\r
128 jmf_processor.close();
\r
129 jmf_processor = null;
\r
133 protected void finalize()
\r
135 if (jmf_processor != null) {
\r
136 jmf_processor.stop();
\r
137 jmf_processor.close();
\r
138 jmf_processor = null;
\r
142 private static DataSource getCaptureDS(VideoFormat vf)
\r
144 DataSource dsVideo = null;
\r
145 DataSource ds = null;
\r
147 // Create a capture DataSource for the video
\r
148 // If there is no video capture device, then exit with null
\r
150 dsVideo = createDataSource(vf);
\r
151 if (dsVideo == null)
\r
155 // Create the monitoring datasource wrapper
\r
156 if (dsVideo != null) {
\r
157 dsVideo = new MonitorCDS(dsVideo);
\r
161 // Merge the data sources, if both audio and video are available
\r
163 ds = Manager.createMergingDataSource(new DataSource[] { dsVideo });
\r
164 } catch (IncompatibleSourceException ise) {
\r
171 private static DataSource createDataSource(Format format)
\r
175 CaptureDeviceInfo cdi;
\r
178 // Find devices for format
\r
179 devices = CaptureDeviceManager.getDeviceList(format);
\r
180 if (devices.size() < 1) {
\r
181 System.err.println("! No Devices for " + format);
\r
184 // Pick the first device
\r
185 cdi = (CaptureDeviceInfo) devices.elementAt(0);
\r
187 ml = cdi.getLocator();
\r
190 ds = Manager.createDataSource(ml);
\r
192 if (ds instanceof CaptureDevice) {
\r
193 setCaptureFormat((CaptureDevice) ds, format);
\r
195 } catch (Exception e) {
\r
196 System.err.println(e);
\r
202 private static void setCaptureFormat(CaptureDevice cdev, Format format)
\r
204 FormatControl[] fcs = cdev.getFormatControls();
\r
205 if (fcs.length < 1) {
\r
208 FormatControl fc = fcs[0];
\r
209 Format[] formats = fc.getSupportedFormats();
\r
210 for (int i = 0; i < formats.length; i++) {
\r
211 if (formats[i].matches(format)) {
\r
212 format = formats[i].intersects(format);
\r
213 fc.setFormat(format);
\r