Revision 2295

View differences:

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
24 24
import org.apache.commons.codec.binary.Base64;
25 25
import org.apache.commons.codec.binary.Hex;
26 26
import org.apache.commons.io.IOUtils;
27
import org.apache.commons.lang3.StringUtils;
28
import org.apache.commons.lang3.mutable.MutableObject;
27 29
import org.gvsig.tools.swing.api.SimpleImage;
28 30

  
29 31
/**
......
36 38
    private BufferedImage image;
37 39
    private Object source;
38 40
    private Dimension dimensions;
41
    private String formatName;
39 42

  
40 43
    public static BufferedImage toBufferedImage(Image img) {
41 44
        if( img == null ) {
......
61 64
        this.image = null;
62 65
        this.dimensions = null;
63 66
        this.source = null;
67
        this.formatName = null;
64 68
    }
65 69

  
66 70
    public DefaultSimpleImage(BufferedImage image) {
......
78 82
        this.image = null;
79 83
        this.dimensions = null;
80 84
        this.source = null;
85
        this.formatName = null;
81 86
    }
82 87
    
83 88
    @Override
84 89
    public BufferedImage getBufferedImage() {
85 90
        if( this.image == null ) {
86 91
          Dimension dim = new Dimension();
87
          this.image = this.load(source, dim);
92
          MutableObject<String> theFormatName = new MutableObject<>(null);
93
          this.image = this.load(source, dim, theFormatName);
88 94
          this.dimensions = dim;
95
          this.formatName = theFormatName.getValue();
89 96
        }
90 97
        return this.image;
91 98
    }
92 99

  
93 100
    @Override
101
    public String getFormatName() {
102
        return this.formatName;
103
    }
104
    
105
    @Override
94 106
    public boolean isEmpty() {
95 107
        return this.image == null && this.source==null ;
96 108
    }
......
107 119
        this.source = source;
108 120
    }
109 121

  
110
    private BufferedImage load(Object source, Dimension dimensions) {
122
    private BufferedImage load(Object source, Dimension dimensions, MutableObject<String> formatName) {
111 123
        BufferedImage theImage = null;
124
        if( formatName!=null ) {
125
            formatName.setValue(null);
126
        }
112 127
        try {
113 128
            if (source instanceof BufferedImage) {
114 129
                theImage = (BufferedImage) source;
......
127 142
            } else if (source instanceof byte[]) {
128 143
                ByteArrayInputStream is = new ByteArrayInputStream((byte[]) source);
129 144
                theImage = ImageIO.read(is);
145
                if( formatName!=null ) {
146
                    formatName.setValue(this.getFormatName(source));
147
                }
130 148
                
131 149
            } else if (source instanceof File) {
132 150
                theImage = ImageIO.read((File) source);
151
                if( formatName!=null ) {
152
                    formatName.setValue(this.getFormatName(source));
153
                }
133 154
                
134 155
            } else if (source instanceof URL) {
135 156
                theImage = ImageIO.read((URL) source);
157
                if( formatName!=null ) {
158
                    formatName.setValue(this.getFormatName(source));
159
                }
136 160
                
137 161
            } else if (source instanceof String) {
138 162
                InputStream is;
139 163
                String s = (String) source;
140 164
                File f = new File(s);
165
                byte[] data = null;
141 166
                if (f.exists()) {
142 167
                    is = new FileInputStream(f);
143 168
                } else {
......
147 172
                        is.available(); // Force exception if is null
148 173
                    } catch (Exception ex) {
149 174
                        try {
150
                            byte[] data = Hex.decodeHex(s.toCharArray());
175
                            data = Hex.decodeHex(s.toCharArray());
151 176
                            is = new ByteArrayInputStream(data);
152 177
                        } catch (DecoderException ex2) {
153 178
                            try {
154
                                byte[] data = Base64.decodeBase64(s);
179
                                data = Base64.decodeBase64(s);
155 180
                                is = new ByteArrayInputStream(data);
156 181
                            } catch (Exception ex3) {
157 182
                                return null;
......
161 186
                }
162 187
                theImage = ImageIO.read(is);
163 188
                IOUtils.closeQuietly(is);
189
                if( formatName!=null && data!=null ) {
190
                    formatName.setValue(this.getFormatName(data));
191
                }
164 192
            }
165 193
        } catch (IOException ex) {
166 194
            return null;
......
172 200
        return theImage;
173 201
    }
174 202
    
203
    private String getFormatName(Object source) {
204
      try {
205
        ImageInputStream in = ImageIO.createImageInputStream(source);
206
        Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
207
        if (readers!=null && readers.hasNext()) {
208
          ImageReader reader = readers.next();
209
          try {
210
            reader.setInput(in);
211
            return reader.getFormatName();
212
          } finally {
213
            reader.dispose();
214
          }
215
        }
216
        return null;
217
      } catch (IOException ex) {
218
        throw new RuntimeException("Can't determine format name of the image.", ex);
219
      }
220
    }
221
    
175 222
    private Dimension loadDimensions(Object source) {
176 223
      try {
177 224
        ImageInputStream in = ImageIO.createImageInputStream(source);
......
186 233
          }
187 234
        }
188 235
        Dimension dim = new Dimension();
189
        this.image = this.load(source, dim);
236
        this.image = this.load(source, dim, null);
190 237
        return dim;
191 238
      } catch (IOException ex) {
192 239
        throw new RuntimeException("Can't load dimensions of the image.", ex);
......
311 358
    
312 359
    @Override
313 360
    public byte[] toBytearray() {
314
        return toBytearray("png");
361
        return toBytearray(null);
315 362
    }
316 363
    
317 364
    @Override
318 365
    public byte[] toBytearray(String format) {
319 366
        try {
367
            if( StringUtils.isBlank(format) ) {
368
                format = this.formatName==null?"png":this.formatName;
369
            }
320 370
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
321 371
            ImageIO.write( this.getBufferedImage(), format, baos );
322 372
            baos.flush();
......
335 385
    }
336 386
    
337 387
    @Override
388
    public String toString(String format) {
389
        byte[] data = this.toBytearray(format);
390
        return bytearray_hex(data);
391
    }
392
    
393
    @Override
338 394
    public void save(File output, String formatName) throws IOException {
395
        if( StringUtils.isBlank(formatName) ) {
396
            formatName = this.formatName==null?"png":this.formatName;
397
        }
339 398
        ImageIO.write(this.getBufferedImage(), formatName, output);
340 399
    }
341 400
    
342 401
    @Override
343 402
    public void save(OutputStream output, String formatName) throws IOException {
403
        if( StringUtils.isBlank(formatName) ) {
404
            formatName = this.formatName==null?"png":this.formatName;
405
        }
344 406
        ImageIO.write(this.getBufferedImage(), formatName, output);
345 407
    }
346 408
        
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.api/src/main/java/org/gvsig/tools/swing/api/SimpleImage.java
5 5
import java.io.File;
6 6
import java.io.IOException;
7 7
import java.io.OutputStream;
8
import javax.imageio.ImageIO;
9 8

  
10 9
/**
11 10
 *
......
22 21
    @Override
23 22
    public String toString();
24 23
    
24
    public String toString(String format);
25
    
26
    public String getFormatName();
27
    
25 28
    public void set(BufferedImage image);
26 29

  
27 30
    public void set(Object source);

Also available in: Unified diff