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