/Users/lyon/j4p/src/video/ScreenCapture.java

1    package video; 
2     
3    import futils.Futil; 
4    import futils.WordPrintMerge; 
5    import gui.In; 
6    import gui.dialogs.ProgressDialog; 
7    import gui.run.RunJob; 
8    import ip.color.Octree; 
9    import ip.gif.gifAnimation.Gif89Encoder; 
10   import ip.gif.neuquantAnimation.AnimatedGifEncoder; 
11   import j2d.ImageUtils; 
12   import j2d.ShortImageBean; 
13   import utils.SystemUtils; 
14    
15   import javax.imageio.ImageIO; 
16   import java.awt.*; 
17   import java.awt.image.BufferedImage; 
18   import java.io.*; 
19   import java.util.Vector; 
20    
21    
22   /** 
23    * DocJava, Inc. http://www.docjava.com Programmer: dlyon Date: Nov 23, 
24    * 2004 Time: 1:12:41 PM 
25    */ 
26   public class ScreenCapture { 
27       Vector v = new Vector(); 
28       RunJob rj = null; 
29       Octree ot = null; 
30    
31       public static String getStackTrace(Exception e){ 
32           ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
33           PrintStream ps = new PrintStream(baos); 
34           e.printStackTrace(ps); 
35           try { 
36               baos.close(); 
37           } catch (IOException e1) { 
38               e1.printStackTrace(); 
39    
40           } 
41           return new String(baos.toByteArray()); 
42       } 
43    
44       public static void main(String args[]) { 
45           int d = In.getInt("enter delay before screen capture"); 
46           File f = Futil.getReadFileDir("select a file in an output directory"); 
47           String format = ImageUtils.getImageIoFormat(); 
48           int i = 10; 
49           Vector fileList = new Vector(); 
50           do { 
51               int i1 = i; 
52               Rectangle rect = 
53                       In.getRectangle("select" + 
54                       " a an area to" + 
55                       " capture in "+d+" ms"); 
56               sleep(d); 
57    
58               try { 
59                   final File outputFile = new File(f.getAbsoluteFile() 
60                                           +SystemUtils.getDirectorySeparator() 
61                                           +"img"+i1+"."+ format); 
62                   fileList.addElement(outputFile); 
63                   grabAndSaveAnImage( 
64                           format, 
65                           rect, 
66                           outputFile); 
67               } catch (AWTException e) { 
68                   e.printStackTrace(); 
69    
70    
71    
72               } catch (IOException e) { 
73                   e.printStackTrace(); 
74    
75               } 
76               i1++; 
77               i = i1; 
78    
79           } while (In.getBoolean("ready to go again?")); 
80           if (In.getBoolean("want to synthesize a word print merge?")){ 
81               try { 
82                   wordPrintMerge(fileList); 
83               } catch (IOException e) { 
84                   e.printStackTrace(); 
85    
86               } 
87           } 
88    
89       } 
90    
91       private static void wordPrintMerge(Vector fileList) 
92               throws IOException { 
93           File fl[] = new File[fileList.size()]; 
94           fileList.copyInto(fl); 
95           WordPrintMerge.wordPrintMerge(fl); 
96       } 
97    
98       private static void sleep(int d) { 
99           try { 
100              Thread.sleep(d); 
101          } catch (InterruptedException e) { 
102              e.printStackTrace(); 
103   
104          } 
105      } 
106   
107      public static void grabAndSaveAnImage(String format) { 
108          try { 
109   
110              grabAndSaveAnImage(format, In.getRectangle("select a capture area"), 
111                      Futil.getWriteFile("select a PNG output file")); 
112              In.message("saved"); 
113          } catch (AWTException e) { 
114              e.printStackTrace(); 
115   
116          } catch (IOException e) { 
117              e.printStackTrace(); 
118   
119          } 
120      } 
121   
122      public static void grabAndSaveGifAnimation() { 
123          ScreenCapture p = new ScreenCapture(); 
124          System.out.println("Recording"); 
125          int k = In.getInt("enter the number of frames to capture"); 
126          p.record(k);    // number of seconds per frame 
127          System.out.println("images saved"); 
128      } 
129      public static void grabAndSaveAnImage(String format, 
130                                            Rectangle rect, 
131                                            File outputFile) 
132              throws AWTException, IOException { 
133          // create screen shot 
134          Robot robot = new Robot(); 
135          BufferedImage image = robot.createScreenCapture(rect); 
136          if (format.equals("tiff")) 
137              ImageUtils.saveAsTiff(image,outputFile); 
138              else 
139          ImageIO.write(image, format, outputFile); 
140   
141      } 
142   
143   
144      private void record(int k) { 
145          for (int i = 0; i < k; i++) 
146              captureWholeScreenToBuffer(); 
147          saveNeuquantImages(); 
148      } 
149   
150      public void snapAndSaveOneFrame() { 
151          captureWholeScreenToBuffer(); 
152          saveScreen(); 
153      } 
154   
155      private void saveScreen() { 
156          Futil.setSwing(false); 
157   
158          File f = Futil.getWriteFile("select and output file (gif)"); 
159          ImageUtils.saveAsGif(makeGray(ImageUtils.getImage( 
160                  (BufferedImage) v.elementAt(0))), 
161                  f); 
162   
163      } 
164   
165      /** 
166       * @param img 
167       * @return a gray image 
168       */ 
169      private Image makeGray(Image img) { 
170          ShortImageBean sib = new ShortImageBean(img); 
171          sib.gray(); 
172          return sib.getImage(); 
173      } 
174   
175      /** 
176       * reduce the number of colors in an image sequence. 
177       * 
178       * @param NumberOfColors 
179       * @return an array of color reduced images. 
180       */ 
181      private Image[] getColorReducedImages(int NumberOfColors) { 
182          ot = new Octree(); 
183          final int n = v.size(); 
184          Image imgs[] = new Image[n]; 
185          System.out.println("begin color reduction process"); 
186          ProgressDialog pd = new ProgressDialog(); 
187          int incr = 200 / n; 
188          pd.setSize(100, 100); 
189          pd.show(); 
190          for (int i1 = 0; i1 < n; i1++) { 
191              pd.incrementValue(incr); 
192              ShortImageBean sib1 = new ShortImageBean( 
193                      ImageUtils.getImage((BufferedImage) v.elementAt(i1))); 
194              if (i1 == 0) { 
195                  ot.octreeQuantization(sib1.getR(), 
196                          sib1.getB(), 
197                          sib1.getG(), NumberOfColors); 
198                  continue; 
199              } 
200              ot.addImagesSeen(sib1.getR(), sib1.getB(), sib1.getG()); 
201   
202          } 
203          System.out.println("done with lut"); 
204          for (int i = 0; i < n; i++) { 
205              ShortImageBean sib = new ShortImageBean( 
206                      ImageUtils.getImage((BufferedImage) v.elementAt(i))); 
207              ot.reMap(sib.getR(), sib.getG(), sib.getB()); 
208              sib.swapGreenAndBlue(); 
209              imgs[i] = sib.getImage(); 
210              pd.incrementValue(incr); 
211          } 
212          System.out.println("colors reduced in image sequence"); 
213          return imgs; 
214   
215      } 
216   
217   
218      private void saveNeuquantImages()  { 
219          File f = Futil.getWriteFile("select gif output file"); 
220          AnimatedGifEncoder age = new AnimatedGifEncoder(); 
221          System.out.println("v.size="+v.size()); 
222          age.setDelay(1000); 
223          In.message("writing worked=" + age.start(f.toString())); 
224          final int n = v.size(); 
225          for (int i=0; i < n; i++) { 
226             boolean b= age.addFrame((BufferedImage)v.elementAt(i)); 
227              if (b==false) In.message("problem adding image"); 
228          } 
229   
230          age.finish(); 
231      } 
232      private void saveImages() throws IOException { 
233          File f = Futil.getWriteFile("select gif output file"); 
234          int speed = 2; //fps 
235          //Image img [] = getImages(); 
236          Image img[] = getColorReducedImages(240); 
237          int n = img.length; 
238          if (n == 0) return; 
239          FileOutputStream fos = 
240                  Futil.getFileOutputStream(f); 
241          Gif89Encoder ge = new Gif89Encoder(); 
242          for (int i = 1; i < n; i++) { 
243              ge.addFrame(img[i]); 
244              ge.setUniformDelay(Math.round(100 / speed)); 
245              System.out.println("processed frame:" + i); 
246          } 
247          System.out.println("done!"); 
248          ge.setLoopCount(true ? 
249                  0 : 
250                  1); 
251          ge.encode(fos); 
252          fos.close(); 
253      } 
254   
255       
256   
257      public void captureWholeScreenToBuffer() { 
258          v.addElement(captureWholeScreen()); 
259      } 
260   
261      private static BufferedImage captureWholeScreen() { 
262          try { 
263              return ImageUtils.captureWholeScreen(); 
264          } catch (AWTException e) { 
265              e.printStackTrace(); 
266          } 
267          return null; 
268      } 
269   
270  }