1 | package org.greenstone.gatherer.feedback;
|
---|
2 |
|
---|
3 | import java.awt.*;
|
---|
4 | import java.applet.Applet;
|
---|
5 | import java.awt.image.*;
|
---|
6 | import java.awt.event.WindowEvent;
|
---|
7 | import java.awt.event.WindowListener;
|
---|
8 | import java.awt.event.WindowAdapter;
|
---|
9 | import java.awt.event.*;
|
---|
10 | import java.awt.image.BufferedImage;
|
---|
11 | import java.awt.image.DataBuffer;
|
---|
12 | import java.awt.geom.GeneralPath;
|
---|
13 | import java.io.*;
|
---|
14 | import javax.imageio.*;
|
---|
15 | import javax.swing.*;
|
---|
16 | import java.awt.geom.*;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * This class can take screen shot of the whole screen size or screen shot of a specified
|
---|
20 | * rectangle only.
|
---|
21 | * @author Veronica Liesaputra
|
---|
22 | */
|
---|
23 | public class ScreenShot
|
---|
24 | {
|
---|
25 | /**
|
---|
26 | * This is the buffered image that hold the screen shot.
|
---|
27 | */
|
---|
28 | private BufferedImage img;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * The dimension of the whole screen size.
|
---|
32 | */
|
---|
33 | private final static Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Rectangle with a dimension of a whole screen size.
|
---|
37 | */
|
---|
38 | private Rectangle mRect = new Rectangle(dim);
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * The desired width of the image.
|
---|
42 | */
|
---|
43 | private int iw;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * The desired height of the image.
|
---|
47 | */
|
---|
48 | private int ih;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * BufferedImage that hold the screen shot image with the desired height and width.
|
---|
52 | */
|
---|
53 | private BufferedImage bi;
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * This is the robot that allows to take screen shot.
|
---|
57 | */
|
---|
58 | private Robot mRobot;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * This constructor will setup the robot to alow taking a screen shot.
|
---|
62 | */
|
---|
63 | public ScreenShot ()
|
---|
64 | {
|
---|
65 | try
|
---|
66 | {
|
---|
67 | mRobot = new Robot();
|
---|
68 | iw = (int) (dim.getWidth() - 60);
|
---|
69 | ih = (int) (dim.getHeight() * 0.75) - 50;
|
---|
70 | }
|
---|
71 | catch (AWTException exp) {}
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * This method will only take screen shot of the particular rectangle.
|
---|
76 | * @param rect is the specified rectangle that we want to take the screen shot of.
|
---|
77 | * @return the image taken.
|
---|
78 | */
|
---|
79 | //public byte[] getImageIcon (Rectangle rect)
|
---|
80 | public BufferedImage getImageIcon (Rectangle rect)
|
---|
81 | {
|
---|
82 | BufferedImage image;
|
---|
83 | image = null;
|
---|
84 |
|
---|
85 | //try
|
---|
86 | // {
|
---|
87 | img = mRobot.createScreenCapture(rect);
|
---|
88 |
|
---|
89 | int w,h;
|
---|
90 | boolean rescale;
|
---|
91 |
|
---|
92 | rescale = false;
|
---|
93 | w = rect.width;
|
---|
94 |
|
---|
95 | if (w >= iw)
|
---|
96 | {
|
---|
97 | w = iw - 50;
|
---|
98 | rescale = true;
|
---|
99 | }
|
---|
100 |
|
---|
101 | h = rect.height;
|
---|
102 |
|
---|
103 | if ( h >= ih)
|
---|
104 | {
|
---|
105 | h = ih - 50;
|
---|
106 | rescale = true;
|
---|
107 | }
|
---|
108 |
|
---|
109 | Graphics2D big;
|
---|
110 | big = null;
|
---|
111 |
|
---|
112 | if (rescale == true)
|
---|
113 | {
|
---|
114 | image = new BufferedImage(w,h, BufferedImage.TYPE_INT_RGB);
|
---|
115 | big = image.createGraphics();
|
---|
116 | big.drawImage(img.getScaledInstance(w,h,Image.SCALE_SMOOTH),0,0,null);
|
---|
117 | }
|
---|
118 | else
|
---|
119 | {
|
---|
120 | image = img;
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | /*ByteArrayOutputStream stream;
|
---|
125 | stream = new ByteArrayOutputStream();
|
---|
126 | ImageIO.write(image,"jpeg",stream);
|
---|
127 | byte[] bytes;
|
---|
128 | bytes = stream.toByteArray();*/
|
---|
129 |
|
---|
130 | if (big != null)
|
---|
131 | big.dispose();
|
---|
132 | //image = null;
|
---|
133 | //img = null;
|
---|
134 | ActionRecorderDialog.setSavefinish(true);
|
---|
135 | return image;
|
---|
136 | //return bytes;
|
---|
137 | // }
|
---|
138 | //catch(IOException ex) {ex.printStackTrace();}
|
---|
139 |
|
---|
140 | //return null;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * This method will only take screen shot of the particular rectangle and encode
|
---|
145 | * the image to its string representation.
|
---|
146 | * @param rect is the specified rectangle that we want to take the screen shot of.
|
---|
147 | * @return the string representation of the image taken.
|
---|
148 | */
|
---|
149 | public String getImage (Rectangle rect)
|
---|
150 | {
|
---|
151 | String txt;
|
---|
152 | txt = null;
|
---|
153 |
|
---|
154 | BufferedImage image;
|
---|
155 | image = null;
|
---|
156 |
|
---|
157 | try
|
---|
158 | {
|
---|
159 | img = mRobot.createScreenCapture(rect);
|
---|
160 |
|
---|
161 | int w,h;
|
---|
162 | boolean rescale;
|
---|
163 |
|
---|
164 | rescale = false;
|
---|
165 | w = rect.width;
|
---|
166 |
|
---|
167 | if (w >= iw)
|
---|
168 | {
|
---|
169 | w = iw - 50;
|
---|
170 | rescale = true;
|
---|
171 | }
|
---|
172 |
|
---|
173 | h = rect.height;
|
---|
174 |
|
---|
175 | if ( h >= ih)
|
---|
176 | {
|
---|
177 | h = ih - 50;
|
---|
178 | rescale = true;
|
---|
179 | }
|
---|
180 |
|
---|
181 | Graphics2D big;
|
---|
182 | big = null;
|
---|
183 |
|
---|
184 | if (rescale == true)
|
---|
185 | {
|
---|
186 | image = new BufferedImage(w,h, BufferedImage.TYPE_INT_RGB);
|
---|
187 | big = image.createGraphics();
|
---|
188 | big.drawImage(img.getScaledInstance(w,h,Image.SCALE_SMOOTH),0,0,null);
|
---|
189 | }
|
---|
190 | else
|
---|
191 | {
|
---|
192 | image = img;
|
---|
193 | }
|
---|
194 |
|
---|
195 | ByteArrayOutputStream stream;
|
---|
196 | stream = new ByteArrayOutputStream();
|
---|
197 | ImageIO.write(image,"gif",stream);
|
---|
198 | byte[] bytes;
|
---|
199 | bytes = stream.toByteArray();
|
---|
200 | txt = Base64.encodeBytes(bytes);
|
---|
201 |
|
---|
202 | bytes = null;
|
---|
203 | if (big != null)
|
---|
204 | big.dispose();
|
---|
205 | image = null;
|
---|
206 | img = null;
|
---|
207 | }
|
---|
208 | catch(IOException ex) {ex.printStackTrace();}
|
---|
209 |
|
---|
210 | ActionRecorderDialog.setSavefinish(true);
|
---|
211 |
|
---|
212 | return txt;
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * This method will return the buffered image of the whole screen size screen shot
|
---|
217 | * with the desired width and height.
|
---|
218 | * @return the buffered image with the desired height and width.
|
---|
219 | */
|
---|
220 | public BufferedImage getBuffImage()
|
---|
221 | {
|
---|
222 | return bi;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * This method will take screen shot of the whole screen and resize it to the
|
---|
227 | * desired width and height.
|
---|
228 | */
|
---|
229 | public BufferedImage captureScreen()
|
---|
230 | {
|
---|
231 | img = mRobot.createScreenCapture(mRect);
|
---|
232 |
|
---|
233 | Graphics2D big;
|
---|
234 | bi = new BufferedImage(iw,ih, BufferedImage.TYPE_INT_RGB);
|
---|
235 | big = bi.createGraphics();
|
---|
236 | big.drawImage(img.getScaledInstance(iw,ih,Image.SCALE_SMOOTH),0,0,null);
|
---|
237 | big.dispose();
|
---|
238 | big = null;
|
---|
239 |
|
---|
240 | return bi;
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * This method will return the desired width.
|
---|
245 | * @return the desired width.
|
---|
246 | */
|
---|
247 | public int getWidth()
|
---|
248 | {
|
---|
249 | return iw;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * This method will return the desired height.
|
---|
254 | * @return the desired height.
|
---|
255 | */
|
---|
256 | public int getHeight()
|
---|
257 | {
|
---|
258 | return ih;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 |
|
---|
264 |
|
---|
265 |
|
---|
266 |
|
---|
267 |
|
---|
268 |
|
---|
269 |
|
---|
270 |
|
---|
271 |
|
---|
272 |
|
---|