/Users/lyon/j4p/src/j2d/gui/VideoCaptureChildFrame.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 gui.run.RunButton; 
12    
13   import javax.media.Buffer; 
14   import javax.media.control.FormatControl; 
15   import javax.media.format.VideoFormat; 
16   import javax.media.protocol.*; 
17   import javax.media.util.BufferToImage; 
18   import javax.swing.*; 
19   import javax.swing.event.InternalFrameAdapter; 
20   import javax.swing.event.InternalFrameEvent; 
21   import java.awt.*; 
22   import java.io.IOException; 
23    
24   /** 
25    * MDI child frame for displaying a video preview 
26    */ 
27   public class VideoCaptureChildFrame 
28           extends JInternalFrame 
29           implements BufferTransferHandler { 
30    
31       private ImagePanel ipnDisplay; 
32    
33       private VideoFormat vfFormat; 
34       private PushBufferDataSource pbds; 
35       private Buffer cbuffer; 
36    
37       /** 
38        * Construct a new VideoCaptureChildFrame 
39        */ 
40       public VideoCaptureChildFrame(PushBufferDataSource pbds, VideoFormat vf) { 
41    
42           this.vfFormat = vf; 
43           this.pbds = pbds; 
44    
45           setTitle("Video Monitor"); 
46    
47           ipnDisplay = new ImagePanel(); 
48    
49           Container c = getContentPane(); 
50           c.setLayout(new BorderLayout()); 
51           c.add(ipnDisplay, BorderLayout.CENTER); 
52    
53    
54           JPanel x = new JPanel(); 
55           x.setLayout(new FlowLayout(FlowLayout.RIGHT)); 
56           x.add(new RunButton("Grab Frame"){ 
57               public void run() { 
58    
59               } 
60           }); 
61           c.add(x, BorderLayout.SOUTH); 
62    
63           setMinimumSize(new Dimension(100, 100)); 
64           setClosable(true); 
65           setMaximizable(false); 
66           setResizable(false); 
67    
68   // Make sure that the camera data source is closed 
69   // when the frame is closed. 
70    
71           addInternalFrameListener(new InternalFrameAdapter() { 
72               public void internalFrameClosing(InternalFrameEvent e) { 
73                   stopVideo(); 
74               } 
75           }); 
76       } 
77    
78       /** 
79        * Begin monitoring video 
80        */ 
81       public void startVideo() { 
82    
83           // Adjust the side of the window to match the 
84           // video size 
85           ipnDisplay.setPreferredSize(vfFormat.getSize()); 
86           pack(); 
87    
88           try { 
89               // Initialize the transfer buffer 
90               cbuffer = new Buffer(); 
91    
92               // Open the data source and set its format 
93               //pbds.connect(); 
94    
95               FormatControl[] fcs = ((CaptureDevice) pbds).getFormatControls(); 
96               FormatControl fc = fcs[0]; 
97               fc.setFormat(vfFormat); 
98    
99               // Set up the monitor for the stream 
100              SourceStream ss = pbds.getStreams()[0]; 
101              ((PushBufferStream) ss).setTransferHandler(this); 
102   
103              // Start streaming. 
104              pbds.start(); 
105   
106          } catch (Exception e) { 
107              JOptionPane.showMessageDialog(this, 
108                      "Error starting video monitor." + "\n" + 
109                      e.getMessage()); 
110          } 
111      } 
112   
113      /** 
114       * Stop monitoring video 
115       */ 
116      private void stopVideo() { 
117          try { 
118              // Stop streaming and disconnect from data source. 
119              pbds.stop(); 
120              //pbds.disconnect(); 
121          } catch (Exception e) { 
122              JOptionPane.showMessageDialog(this, 
123                      "Error stopping video monitor."); 
124          } 
125      } 
126   
127      /** 
128       * Implementation of BufferTransferHandler 
129       */ 
130      public void transferData(PushBufferStream stream) { 
131          // Get the data from the original source stream 
132          try { 
133              stream.read(cbuffer); 
134          } catch (IOException ioe) { 
135              return; 
136          } 
137   
138          // Display data 
139   
140          BufferToImage bti = 
141                  new BufferToImage((VideoFormat) cbuffer.getFormat()); 
142          Image im = bti.createImage(cbuffer); 
143          ipnDisplay.setImage(im); 
144      } 
145   
146      /** 
147       * Obtain a handle to the displayed image. 
148       */ 
149      public Image getImage() { 
150          return ipnDisplay.getImage(); 
151      } 
152  }