gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / DefaultSimpleImage.java @ 2252
History | View | Annotate | Download (11.5 KB)
1 |
package org.gvsig.tools.swing.impl; |
---|---|
2 |
|
3 |
import java.awt.Dimension; |
4 |
import java.awt.Graphics2D; |
5 |
import java.awt.Image; |
6 |
import java.awt.MediaTracker; |
7 |
import java.awt.RenderingHints; |
8 |
import java.awt.geom.AffineTransform; |
9 |
import java.awt.image.BufferedImage; |
10 |
import java.io.ByteArrayInputStream; |
11 |
import java.io.ByteArrayOutputStream; |
12 |
import java.io.File; |
13 |
import java.io.FileInputStream; |
14 |
import java.io.IOException; |
15 |
import java.io.InputStream; |
16 |
import java.io.OutputStream; |
17 |
import java.net.URL; |
18 |
import java.util.Iterator; |
19 |
import javax.imageio.ImageIO; |
20 |
import javax.imageio.ImageReader; |
21 |
import javax.imageio.stream.ImageInputStream; |
22 |
import javax.swing.ImageIcon; |
23 |
import org.apache.commons.codec.DecoderException; |
24 |
import org.apache.commons.codec.binary.Base64; |
25 |
import org.apache.commons.codec.binary.Hex; |
26 |
import org.apache.commons.io.IOUtils; |
27 |
import org.gvsig.tools.swing.api.SimpleImage; |
28 |
|
29 |
/**
|
30 |
*
|
31 |
* @author jjdelcerro
|
32 |
*/
|
33 |
@SuppressWarnings("UseSpecificCatch") |
34 |
public class DefaultSimpleImage implements SimpleImage { |
35 |
|
36 |
private BufferedImage image; |
37 |
private Object source; |
38 |
private Dimension dimensions; |
39 |
|
40 |
public static BufferedImage toBufferedImage(Image img) { |
41 |
if( img == null ) { |
42 |
throw new IllegalArgumentException("null image."); |
43 |
} |
44 |
if (img instanceof BufferedImage) { |
45 |
return (BufferedImage) img; |
46 |
} |
47 |
// Create a buffered image with transparency
|
48 |
BufferedImage bimage = new BufferedImage( |
49 |
img.getWidth(null),
|
50 |
img.getHeight(null),
|
51 |
BufferedImage.TYPE_INT_ARGB
|
52 |
); |
53 |
// Draw the image on to the buffered image
|
54 |
Graphics2D bGr = bimage.createGraphics();
|
55 |
bGr.drawImage(img, 0, 0, null); |
56 |
bGr.dispose(); |
57 |
return bimage;
|
58 |
} |
59 |
|
60 |
public DefaultSimpleImage() {
|
61 |
this.image = null; |
62 |
this.dimensions = null; |
63 |
this.source = null; |
64 |
} |
65 |
|
66 |
public DefaultSimpleImage(BufferedImage image) { |
67 |
this();
|
68 |
this.image = image;
|
69 |
this.source = image;
|
70 |
} |
71 |
|
72 |
public DefaultSimpleImage(Object source) { |
73 |
this();
|
74 |
this.source = source;
|
75 |
} |
76 |
|
77 |
public void clean() { |
78 |
this.image = null; |
79 |
this.dimensions = null; |
80 |
this.source = null; |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public BufferedImage getBufferedImage() { |
85 |
if( this.image == null ) { |
86 |
Dimension dim = new Dimension(); |
87 |
this.image = this.load(source, dim); |
88 |
this.dimensions = dim;
|
89 |
} |
90 |
return this.image; |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public boolean isEmpty() { |
95 |
return this.image == null && this.source==null ; |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public void set(BufferedImage image) { |
100 |
this.image = image;
|
101 |
this.source = image;
|
102 |
} |
103 |
|
104 |
@Override
|
105 |
public void set(Object source) { |
106 |
this.image = null; |
107 |
this.source = source;
|
108 |
} |
109 |
|
110 |
private BufferedImage load(Object source, Dimension dimensions) { |
111 |
BufferedImage theImage = null; |
112 |
try {
|
113 |
if (source instanceof BufferedImage) { |
114 |
theImage = (BufferedImage) source;
|
115 |
|
116 |
} else if (source instanceof ImageIcon ) { |
117 |
ImageIcon icon = (ImageIcon)source; |
118 |
int loadStatus = icon.getImageLoadStatus();
|
119 |
if( loadStatus != MediaTracker.COMPLETE ) { |
120 |
icon.getDescription(); |
121 |
} |
122 |
theImage = toBufferedImage(icon.getImage()); |
123 |
|
124 |
} else if (source instanceof Image) { |
125 |
theImage = toBufferedImage((Image) source);
|
126 |
|
127 |
} else if (source instanceof byte[]) { |
128 |
ByteArrayInputStream is = new ByteArrayInputStream((byte[]) source); |
129 |
theImage = ImageIO.read(is);
|
130 |
|
131 |
} else if (source instanceof File) { |
132 |
theImage = ImageIO.read((File) source); |
133 |
|
134 |
} else if (source instanceof URL) { |
135 |
theImage = ImageIO.read((URL) source); |
136 |
|
137 |
} else if (source instanceof String) { |
138 |
InputStream is;
|
139 |
String s = (String) source; |
140 |
File f = new File(s); |
141 |
if (f.exists()) {
|
142 |
is = new FileInputStream(f); |
143 |
} else {
|
144 |
try {
|
145 |
URL url = new URL(s); |
146 |
is = url.openStream(); |
147 |
is.available(); // Force exception if is null
|
148 |
} catch (Exception ex) { |
149 |
try {
|
150 |
byte[] data = Hex.decodeHex(s.toCharArray()); |
151 |
is = new ByteArrayInputStream(data); |
152 |
} catch (DecoderException ex2) {
|
153 |
try {
|
154 |
byte[] data = Base64.decodeBase64(s); |
155 |
is = new ByteArrayInputStream(data); |
156 |
} catch (Exception ex3) { |
157 |
return null; |
158 |
} |
159 |
} |
160 |
} |
161 |
} |
162 |
theImage = ImageIO.read(is);
|
163 |
IOUtils.closeQuietly(is); |
164 |
} |
165 |
} catch (IOException ex) { |
166 |
return null; |
167 |
} |
168 |
if( theImage!=null && dimensions!=null ) { |
169 |
dimensions.width = theImage.getWidth(); |
170 |
dimensions.height = theImage.getHeight(); |
171 |
} |
172 |
return theImage;
|
173 |
} |
174 |
|
175 |
private Dimension loadDimensions(Object source) { |
176 |
try {
|
177 |
ImageInputStream in = ImageIO.createImageInputStream(source); |
178 |
Iterator<ImageReader> readers = ImageIO.getImageReaders(in); |
179 |
if (readers!=null && readers.hasNext()) { |
180 |
ImageReader reader = readers.next();
|
181 |
try {
|
182 |
reader.setInput(in); |
183 |
return new Dimension(reader.getWidth(0), reader.getHeight(0)); |
184 |
} finally {
|
185 |
reader.dispose(); |
186 |
} |
187 |
} |
188 |
Dimension dim = new Dimension(); |
189 |
this.image = this.load(source, dim); |
190 |
return dim;
|
191 |
} catch (IOException ex) { |
192 |
throw new RuntimeException("Can't load dimensions of the image.", ex); |
193 |
} |
194 |
} |
195 |
|
196 |
@Override
|
197 |
public int getWidth() { |
198 |
if( this.dimensions == null ) { |
199 |
this.dimensions = this.loadDimensions(source); |
200 |
} |
201 |
return this.dimensions.width; |
202 |
} |
203 |
|
204 |
@Override
|
205 |
public int getHeight() { |
206 |
if( this.dimensions == null ) { |
207 |
this.dimensions = this.loadDimensions(source); |
208 |
} |
209 |
return this.dimensions.height; |
210 |
} |
211 |
|
212 |
@Override
|
213 |
public DefaultSimpleImage resize(double factor) { |
214 |
BufferedImage theImage = this.getBufferedImage(); |
215 |
int w = theImage.getWidth();
|
216 |
int h = theImage.getHeight();
|
217 |
int width = (int) (w * factor); |
218 |
int height = (int) (h * factor); |
219 |
BufferedImage newImage = new BufferedImage(width, height, theImage.getType()); |
220 |
Graphics2D g = newImage.createGraphics();
|
221 |
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
222 |
g.drawImage(theImage, 0, 0, width, height, 0, 0, w, h, null); |
223 |
g.dispose(); |
224 |
return new DefaultSimpleImage(newImage); |
225 |
} |
226 |
|
227 |
@Override
|
228 |
public DefaultSimpleImage resize(int width, int height) { |
229 |
BufferedImage theImage = this.getBufferedImage(); |
230 |
int w = theImage.getWidth();
|
231 |
int h = theImage.getHeight();
|
232 |
BufferedImage newImage = new BufferedImage(width, height, theImage.getType()); |
233 |
Graphics2D g = newImage.createGraphics();
|
234 |
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
235 |
g.drawImage(theImage, 0, 0, width, height, 0, 0, w, h, null); |
236 |
g.dispose(); |
237 |
return new DefaultSimpleImage(newImage); |
238 |
} |
239 |
|
240 |
@Override
|
241 |
public DefaultSimpleImage rotate(int angle) { |
242 |
BufferedImage theImage = this.getBufferedImage(); |
243 |
int w = theImage.getWidth();
|
244 |
int h = theImage.getHeight();
|
245 |
BufferedImage newImage = new BufferedImage(w, h, theImage.getType()); |
246 |
Graphics2D g = newImage.createGraphics();
|
247 |
g.rotate(Math.toRadians(angle), w / 2, h / 2); |
248 |
g.drawImage(theImage, null, 0, 0); |
249 |
g.dispose(); |
250 |
return new DefaultSimpleImage(newImage); |
251 |
} |
252 |
|
253 |
@Override
|
254 |
public DefaultSimpleImage horizontalflip() {
|
255 |
BufferedImage img = this.getBufferedImage(); |
256 |
int w = img.getWidth();
|
257 |
int h = img.getHeight();
|
258 |
BufferedImage newImage = new BufferedImage(w, h, img.getType()); |
259 |
Graphics2D g = newImage.createGraphics();
|
260 |
g.drawImage(img, 0, 0, w, h, w, 0, 0, h, null); |
261 |
g.dispose(); |
262 |
return new DefaultSimpleImage(newImage); |
263 |
} |
264 |
|
265 |
@Override
|
266 |
public DefaultSimpleImage verticalflip() {
|
267 |
BufferedImage img = this.getBufferedImage(); |
268 |
int w = img.getWidth();
|
269 |
int h = img.getHeight();
|
270 |
BufferedImage newImage = new BufferedImage(w, h, img.getColorModel().getTransparency()); |
271 |
Graphics2D g = newImage.createGraphics();
|
272 |
g.drawImage(img, 0, 0, w, h, 0, h, w, 0, null); |
273 |
g.dispose(); |
274 |
return new DefaultSimpleImage(newImage); |
275 |
} |
276 |
|
277 |
@Override
|
278 |
public DefaultSimpleImage transform(AffineTransform transform, int width, int height) { |
279 |
BufferedImage img = this.getBufferedImage(); |
280 |
BufferedImage newImage = new BufferedImage(width, height, img.getColorModel().getTransparency()); |
281 |
Graphics2D g = newImage.createGraphics();
|
282 |
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
283 |
g.setTransform(transform); |
284 |
g.drawImage(img, 0, 0, null); |
285 |
g.dispose(); |
286 |
return new DefaultSimpleImage(newImage); |
287 |
} |
288 |
|
289 |
@Override
|
290 |
public DefaultSimpleImage transform(AffineTransform transform) { |
291 |
BufferedImage img = this.getBufferedImage(); |
292 |
int w = img.getWidth();
|
293 |
int h = img.getHeight();
|
294 |
BufferedImage newImage = new BufferedImage(w, h, img.getColorModel().getTransparency()); |
295 |
Graphics2D g = newImage.createGraphics();
|
296 |
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
297 |
g.setTransform(transform); |
298 |
g.drawImage(img, 0, 0, null); |
299 |
g.dispose(); |
300 |
return new DefaultSimpleImage(newImage); |
301 |
} |
302 |
|
303 |
private String bytearray_hex(byte[] data) { |
304 |
StringBuilder builder = new StringBuilder(); |
305 |
for (byte abyte : data) { |
306 |
int v = abyte & 0xff; |
307 |
builder.append(String.format("%02x", v)); |
308 |
} |
309 |
return builder.toString();
|
310 |
} |
311 |
|
312 |
@Override
|
313 |
public byte[] toBytearray() { |
314 |
return toBytearray("png"); |
315 |
} |
316 |
|
317 |
@Override
|
318 |
public byte[] toBytearray(String format) { |
319 |
try {
|
320 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
321 |
ImageIO.write( this.image, format, baos ); |
322 |
baos.flush(); |
323 |
byte[] imageInByte = baos.toByteArray(); |
324 |
baos.close(); |
325 |
return imageInByte;
|
326 |
} catch (IOException ex) { |
327 |
return null; |
328 |
} |
329 |
} |
330 |
|
331 |
@Override
|
332 |
public String toString() { |
333 |
byte[] data = this.toBytearray(); |
334 |
return bytearray_hex(data);
|
335 |
} |
336 |
|
337 |
@Override
|
338 |
public void save(File output, String formatName) throws IOException { |
339 |
ImageIO.write(this.getBufferedImage(), formatName, output); |
340 |
} |
341 |
|
342 |
@Override
|
343 |
public void save(OutputStream output, String formatName) throws IOException { |
344 |
ImageIO.write(this.getBufferedImage(), formatName, output); |
345 |
} |
346 |
|
347 |
} |