38 |
|
|
39 |
package javax.imageio; |
package javax.imageio; |
40 |
|
|
41 |
|
import java.awt.image.RenderedImage; |
42 |
|
import java.awt.image.BufferedImage; |
43 |
|
import java.net.URL; |
44 |
import java.io.File; |
import java.io.File; |
45 |
|
import java.io.FileInputStream; |
46 |
|
import java.io.FileOutputStream; |
47 |
|
import java.io.InputStream; |
48 |
import java.io.IOException; |
import java.io.IOException; |
49 |
|
import java.io.OutputStream; |
50 |
import java.util.ArrayList; |
import java.util.ArrayList; |
51 |
import java.util.Collections; |
import java.util.Collections; |
52 |
import java.util.Iterator; |
import java.util.Iterator; |
55 |
import javax.imageio.spi.ImageReaderSpi; |
import javax.imageio.spi.ImageReaderSpi; |
56 |
import javax.imageio.spi.ImageWriterSpi; |
import javax.imageio.spi.ImageWriterSpi; |
57 |
import javax.imageio.spi.ServiceRegistry; |
import javax.imageio.spi.ServiceRegistry; |
58 |
|
import javax.imageio.stream.ImageOutputStream; |
59 |
|
import javax.imageio.stream.ImageInputStream; |
60 |
|
import javax.imageio.stream.MemoryCacheImageInputStream; |
61 |
|
import javax.imageio.stream.MemoryCacheImageOutputStream; |
62 |
|
|
63 |
public final class ImageIO |
public final class ImageIO |
64 |
{ |
{ |
153 |
|
|
154 |
public boolean filter(Object provider) |
public boolean filter(Object provider) |
155 |
{ |
{ |
156 |
if (provider instanceof ImageReaderSpi) |
if (provider instanceof ImageWriterSpi) |
157 |
{ |
{ |
158 |
ImageReaderSpi spi = (ImageReaderSpi) provider; |
ImageWriterSpi spi = (ImageWriterSpi) provider; |
159 |
String[] formatNames = spi.getFormatNames(); |
String[] formatNames = spi.getFormatNames(); |
160 |
|
|
161 |
for (int i = formatNames.length - 1; i >= 0; --i) |
for (int i = formatNames.length - 1; i >= 0; --i) |
162 |
if (formatName.equals(formatNames[i])) |
if (formatName.equals(formatNames[i])) |
163 |
return true; |
return true; |
164 |
} |
} |
165 |
|
|
166 |
return false; |
return false; |
178 |
|
|
179 |
public boolean filter(Object provider) |
public boolean filter(Object provider) |
180 |
{ |
{ |
181 |
if (provider instanceof ImageReaderSpi) |
if (provider instanceof ImageWriterSpi) |
182 |
{ |
{ |
183 |
ImageWriterSpi spi = (ImageWriterSpi) provider; |
ImageWriterSpi spi = (ImageWriterSpi) provider; |
184 |
String[] mimetypes = spi.getMIMETypes(); |
String[] mimetypes = spi.getMIMETypes(); |
203 |
|
|
204 |
public boolean filter(Object provider) |
public boolean filter(Object provider) |
205 |
{ |
{ |
206 |
if (provider instanceof ImageReaderSpi) |
if (provider instanceof ImageWriterSpi) |
207 |
{ |
{ |
208 |
ImageWriterSpi spi = (ImageWriterSpi) provider; |
ImageWriterSpi spi = (ImageWriterSpi) provider; |
209 |
String[] suffixes = spi.getFileSuffixes(); |
String[] suffixes = spi.getFileSuffixes(); |
220 |
private static final class ImageReaderIterator implements Iterator |
private static final class ImageReaderIterator implements Iterator |
221 |
{ |
{ |
222 |
Iterator it; |
Iterator it; |
223 |
|
Object readerExtension; |
224 |
|
|
225 |
public ImageReaderIterator(Iterator it) |
public ImageReaderIterator(Iterator it, Object readerExtension) |
226 |
{ |
{ |
227 |
this.it = it; |
this.it = it; |
228 |
|
this.readerExtension = readerExtension; |
229 |
} |
} |
230 |
|
|
231 |
public boolean hasNext() |
public boolean hasNext() |
237 |
{ |
{ |
238 |
try |
try |
239 |
{ |
{ |
240 |
return ((ImageReaderSpi) it.next()).createReaderInstance(); |
return ((ImageReaderSpi) it.next()).createReaderInstance(readerExtension); |
241 |
} |
} |
242 |
catch (IOException e) |
catch (IOException e) |
243 |
{ |
{ |
254 |
private static final class ImageWriterIterator implements Iterator |
private static final class ImageWriterIterator implements Iterator |
255 |
{ |
{ |
256 |
Iterator it; |
Iterator it; |
257 |
|
Object writerExtension; |
258 |
|
|
259 |
public ImageWriterIterator(Iterator it) |
public ImageWriterIterator(Iterator it, Object writerExtension) |
260 |
{ |
{ |
261 |
this.it = it; |
this.it = it; |
262 |
|
this.writerExtension = writerExtension; |
263 |
} |
} |
264 |
|
|
265 |
public boolean hasNext() |
public boolean hasNext() |
271 |
{ |
{ |
272 |
try |
try |
273 |
{ |
{ |
274 |
return ((ImageWriterSpi) it.next()).createWriterInstance(); |
return ((ImageWriterSpi) it.next()).createWriterInstance(writerExtension); |
275 |
} |
} |
276 |
catch (IOException e) |
catch (IOException e) |
277 |
{ |
{ |
289 |
private static boolean useCache = true; |
private static boolean useCache = true; |
290 |
|
|
291 |
private static Iterator getReadersByFilter(Class type, |
private static Iterator getReadersByFilter(Class type, |
292 |
ServiceRegistry.Filter filter) |
ServiceRegistry.Filter filter, |
293 |
|
Object readerExtension) |
294 |
{ |
{ |
295 |
try |
try |
296 |
{ |
{ |
297 |
Iterator it = getRegistry().getServiceProviders(type, filter, true); |
Iterator it = getRegistry().getServiceProviders(type, filter, true); |
298 |
return new ImageReaderIterator(it); |
return new ImageReaderIterator(it, readerExtension); |
299 |
} |
} |
300 |
catch (IllegalArgumentException e) |
catch (IllegalArgumentException e) |
301 |
{ |
{ |
304 |
} |
} |
305 |
|
|
306 |
private static Iterator getWritersByFilter(Class type, |
private static Iterator getWritersByFilter(Class type, |
307 |
ServiceRegistry.Filter filter) |
ServiceRegistry.Filter filter, |
308 |
|
Object writerExtension) |
309 |
{ |
{ |
310 |
try |
try |
311 |
{ |
{ |
312 |
Iterator it = getRegistry().getServiceProviders(type, filter, true); |
Iterator it = getRegistry().getServiceProviders(type, filter, true); |
313 |
return new ImageWriterIterator(it); |
return new ImageWriterIterator(it, writerExtension); |
314 |
} |
} |
315 |
catch (IllegalArgumentException e) |
catch (IllegalArgumentException e) |
316 |
{ |
{ |
329 |
throw new IllegalArgumentException("formatName may not be null"); |
throw new IllegalArgumentException("formatName may not be null"); |
330 |
|
|
331 |
return getReadersByFilter(ImageReaderSpi.class, |
return getReadersByFilter(ImageReaderSpi.class, |
332 |
new ReaderFormatFilter(formatName)); |
new ReaderFormatFilter(formatName), |
333 |
|
formatName); |
334 |
} |
} |
335 |
|
|
336 |
public static Iterator getImageReadersByMIMEType(String MIMEType) |
public static Iterator getImageReadersByMIMEType(String MIMEType) |
339 |
throw new IllegalArgumentException("MIMEType may not be null"); |
throw new IllegalArgumentException("MIMEType may not be null"); |
340 |
|
|
341 |
return getReadersByFilter(ImageReaderSpi.class, |
return getReadersByFilter(ImageReaderSpi.class, |
342 |
new ReaderMIMETypeFilter(MIMEType)); |
new ReaderMIMETypeFilter(MIMEType), |
343 |
|
MIMEType); |
344 |
} |
} |
345 |
|
|
346 |
public static Iterator getImageReadersBySuffix(String fileSuffix) |
public static Iterator getImageReadersBySuffix(String fileSuffix) |
349 |
throw new IllegalArgumentException("formatName may not be null"); |
throw new IllegalArgumentException("formatName may not be null"); |
350 |
|
|
351 |
return getReadersByFilter(ImageReaderSpi.class, |
return getReadersByFilter(ImageReaderSpi.class, |
352 |
new ReaderSuffixFilter(fileSuffix)); |
new ReaderSuffixFilter(fileSuffix), |
353 |
|
fileSuffix); |
354 |
} |
} |
355 |
|
|
356 |
public static Iterator getImageWritersByFormatName(String formatName) |
public static Iterator getImageWritersByFormatName(String formatName) |
359 |
throw new IllegalArgumentException("formatName may not be null"); |
throw new IllegalArgumentException("formatName may not be null"); |
360 |
|
|
361 |
return getWritersByFilter(ImageWriterSpi.class, |
return getWritersByFilter(ImageWriterSpi.class, |
362 |
new WriterFormatFilter(formatName)); |
new WriterFormatFilter(formatName), |
363 |
|
formatName); |
364 |
} |
} |
365 |
|
|
366 |
public static Iterator getImageWritersByMIMEType(String MIMEType) |
public static Iterator getImageWritersByMIMEType(String MIMEType) |
369 |
throw new IllegalArgumentException("MIMEType may not be null"); |
throw new IllegalArgumentException("MIMEType may not be null"); |
370 |
|
|
371 |
return getWritersByFilter(ImageWriterSpi.class, |
return getWritersByFilter(ImageWriterSpi.class, |
372 |
new WriterMIMETypeFilter(MIMEType)); |
new WriterMIMETypeFilter(MIMEType), |
373 |
|
MIMEType); |
374 |
} |
} |
375 |
|
|
376 |
public static Iterator getImageWritersBySuffix(String fileSuffix) |
public static Iterator getImageWritersBySuffix(String fileSuffix) |
379 |
throw new IllegalArgumentException("fileSuffix may not be null"); |
throw new IllegalArgumentException("fileSuffix may not be null"); |
380 |
|
|
381 |
return getWritersByFilter(ImageWriterSpi.class, |
return getWritersByFilter(ImageWriterSpi.class, |
382 |
new WriterSuffixFilter(fileSuffix)); |
new WriterSuffixFilter(fileSuffix), |
383 |
|
fileSuffix); |
384 |
} |
} |
385 |
|
|
386 |
public static String[] getReaderFormatNames() |
public static String[] getReaderFormatNames() |
519 |
{ |
{ |
520 |
ImageIO.useCache = useCache; |
ImageIO.useCache = useCache; |
521 |
} |
} |
522 |
|
|
523 |
|
/* |
524 |
|
* "Standard" simplified entry points. |
525 |
|
*/ |
526 |
|
|
527 |
|
public static boolean write(RenderedImage im, |
528 |
|
String formatName, |
529 |
|
File output) |
530 |
|
throws IOException |
531 |
|
{ |
532 |
|
return write(im, formatName, new FileOutputStream(output)); |
533 |
|
} |
534 |
|
|
535 |
|
public static boolean write(RenderedImage im, |
536 |
|
String formatName, |
537 |
|
OutputStream output) |
538 |
|
throws IOException |
539 |
|
{ |
540 |
|
return write(im, formatName, new MemoryCacheImageOutputStream(output)); |
541 |
|
} |
542 |
|
|
543 |
|
|
544 |
|
public static boolean write(RenderedImage im, |
545 |
|
String formatName, |
546 |
|
ImageOutputStream output) |
547 |
|
throws IOException |
548 |
|
{ |
549 |
|
Iterator writers = getImageWritersByFormatName(formatName); |
550 |
|
IIOImage img = new IIOImage(im, null, null); |
551 |
|
while (writers.hasNext()) |
552 |
|
{ |
553 |
|
ImageWriter w = (ImageWriter) writers.next(); |
554 |
|
try |
555 |
|
{ |
556 |
|
w.setOutput(output); |
557 |
|
} |
558 |
|
catch (IllegalArgumentException e) |
559 |
|
{ |
560 |
|
continue; |
561 |
|
} |
562 |
|
|
563 |
|
w.write(null, img, null); |
564 |
|
output.close(); |
565 |
|
return true; |
566 |
|
} |
567 |
|
return false; |
568 |
|
} |
569 |
|
|
570 |
|
public static BufferedImage read(ImageInputStream stream) |
571 |
|
throws IOException |
572 |
|
{ |
573 |
|
Iterator providers = getRegistry().getServiceProviders(ImageReaderSpi.class, true); |
574 |
|
while (providers.hasNext()) |
575 |
|
{ |
576 |
|
ImageReaderSpi spi = (ImageReaderSpi) providers.next(); |
577 |
|
if (spi.canDecodeInput(stream)) |
578 |
|
{ |
579 |
|
ImageReader reader = spi.createReaderInstance(); |
580 |
|
reader.setInput(stream); |
581 |
|
return reader.read(0, null); |
582 |
|
} |
583 |
|
} |
584 |
|
return null; |
585 |
|
} |
586 |
|
|
587 |
|
public static BufferedImage read(URL input) |
588 |
|
throws IOException |
589 |
|
{ |
590 |
|
return read(input.openStream()); |
591 |
|
} |
592 |
|
|
593 |
|
public static BufferedImage read(InputStream input) |
594 |
|
throws IOException |
595 |
|
{ |
596 |
|
return read(new MemoryCacheImageInputStream(input)); |
597 |
|
} |
598 |
|
|
599 |
|
public static BufferedImage read(File input) |
600 |
|
throws IOException |
601 |
|
{ |
602 |
|
return read(new FileInputStream(input)); |
603 |
|
} |
604 |
|
|
605 |
} |
} |