/Users/lyon/j4p/src/j2d/gui/CameraConfigToolbox.java
|
1 // Glenn Josefiak
2 // Fairfield University
3 // SW513
4 // Spring 2003
5
6 package j2d.gui;
7
8 // Reference:
9 // http://java.sun.com/products/java-media/jmf/2.1.1/solutions/JVidCap.html
10
11 import javax.media.*;
12 import javax.media.control.FormatControl;
13 import javax.media.format.VideoFormat;
14 import javax.media.format.YUVFormat;
15 import javax.media.protocol.CaptureDevice;
16 import javax.media.protocol.PushBufferDataSource;
17 import javax.swing.*;
18 import java.awt.*;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21 import java.util.Vector;
22
23 /**
24 * Dialog box that enables selection of camera and
25 * video format from those cameras installed on the
26 * system. Requires JMF to be installed.
27 */
28 public class CameraConfigToolbox extends JInternalFrame {
29 JComboBox cboCameraSelect;
30 JComboBox cboFormatSelect;
31 JButton btnApply;
32
33 Vector YUVdevices;
34 Format[] formats;
35
36 /**
37 * Create a new CameraConfigToolbox
38 */
39 public CameraConfigToolbox() {
40
41 Container c = getContentPane();
42 c.setLayout(new FlowLayout(FlowLayout.LEFT));
43
44 setTitle("Camera Configuration");
45
46 cboCameraSelect = new JComboBox(new Vector());
47 c.add(cboCameraSelect);
48 cboCameraSelect.addActionListener(new ActionListener() {
49 public void actionPerformed(ActionEvent e) {
50 initFormatList();
51 }
52 });
53
54 cboFormatSelect = new JComboBox(new Vector());
55 c.add(cboFormatSelect);
56
57 btnApply = new JButton("Apply");
58 c.add(btnApply);
59
60 setMinimumSize(new Dimension(100, 100));
61 setClosable(true);
62 setMaximizable(true);
63 setResizable(true);
64
65 initCameraList();
66 }
67
68 /**
69 * Populate the camera list combo box.
70 */
71 private void initCameraList() {
72 YUVdevices = CaptureDeviceManager.getDeviceList(new YUVFormat());
73 for (int i = 0; i < YUVdevices.size(); i++) {
74 cboCameraSelect.addItem(((CaptureDeviceInfo) YUVdevices.elementAt(i)).getName());
75 }
76 }
77
78 /**
79 * Populate the format list combo box.
80 */
81 private void initFormatList() {
82
83 try {
84 CaptureDeviceInfo cdi =
85 (CaptureDeviceInfo)
86 YUVdevices.elementAt(cboCameraSelect.getSelectedIndex());
87 MediaLocator ml = cdi.getLocator();
88
89 PushBufferDataSource pbds =
90 (PushBufferDataSource) Manager.createDataSource(ml);
91 pbds.connect();
92
93 FormatControl[] fcs = ((CaptureDevice) pbds).getFormatControls();
94 if (fcs.length < 1)
95 return;
96
97 FormatControl fc = fcs[0];
98 formats = fc.getSupportedFormats();
99
100 cboFormatSelect.removeAllItems();
101 for (int i = 0; i < formats.length; i++) {
102 String desc;
103 VideoFormat vf = (VideoFormat) formats[i];
104
105 desc = vf.getEncoding() + "; ";
106 desc += vf.getSize().getWidth() + "x"
107 + vf.getSize().getHeight();
108
109 cboFormatSelect.addItem(desc);
110 }
111 pbds.disconnect();
112 } catch (Exception e) {
113 // do nothing
114 }
115 }
116
117 /**
118 * Return the selected video format.
119 */
120 public VideoFormat getSelectedFormat() {
121 return (VideoFormat) formats[cboFormatSelect.getSelectedIndex()];
122 }
123
124
125
126 /**
127 * Return a handle to the Apply button on the frame.
128 */
129 public JButton getButton() {
130 return btnApply;
131 }
132 }