📄 imageio.java
字号:
* written * * @return false if no registered writer supports the given format, * true otherwise * * @exception IllegalArgumentException if any argument is null * @exception IOException if a writing error occurs */ public static boolean write(RenderedImage im, String formatName, ImageOutputStream output) throws IOException { if (im == null || formatName == null || output == null) throw new IllegalArgumentException ("null argument"); Iterator writers = getImageWritersByFormatName(formatName); IIOImage img = new IIOImage(im, null, null); while (writers.hasNext()) { ImageWriter w = (ImageWriter) writers.next(); try { w.setOutput(output); } catch (IllegalArgumentException e) { continue; } w.write(null, img, null); output.close(); return true; } return false; } /** * Create a buffered image from an image input stream. An image * reader that supports the given image data is automatically * selected from the collection of registered readers. If no * registered reader can handle the input format, null is returned. * * @param stream the image input stream from which to read image * data * * @return a new buffered image created from the given image data, * or null * * @exception IllegalArgumentException if stream is null * @exception IOException if a reading error occurs */ public static BufferedImage read(ImageInputStream stream) throws IOException { if (stream == null) throw new IllegalArgumentException("null argument"); Iterator providers = getRegistry().getServiceProviders(ImageReaderSpi.class, true); while (providers.hasNext()) { ImageReaderSpi spi = (ImageReaderSpi) providers.next(); if (spi.canDecodeInput(stream)) { ImageReader reader = spi.createReaderInstance(); reader.setInput(stream); return reader.read(0, null); } } return null; } /** * Create a buffered image from a URL. An image reader that * supports the given image data is automatically selected from the * collection of registered readers. If no registered reader can * handle the input format, null is returned. * * The image data will be cached in the current cache directory if * caching is enabled. * * This method does not locate readers that read data directly from * a URL. To locate such readers manually, use IIORegistry and * ImageReaderSpi. * * @param input the URL from which to retrieve the image file * * @return a new buffered image created from the given image URL, or * null * * @exception IllegalArgumentException if input is null * @exception IOException if a reading error occurs */ public static BufferedImage read(URL input) throws IOException { if (input == null) throw new IllegalArgumentException("null argument"); return read(input.openStream()); } /** * Create a buffered image from an input stream. An image reader * that supports the given image data is automatically selected from * the collection of registered readers. If no registered reader * can handle the input format, null is returned. * * The image data will be cached in the current cache directory if * caching is enabled. * * This method does not locate readers that read data directly from * an input stream. To locate such readers manually, use * IIORegistry and ImageReaderSpi. * * @param input the input stream from which to read the image data * * @return a new buffered image created from the given input stream, * or null * * @exception IllegalArgumentException if input is null * @exception IOException if a reading error occurs */ public static BufferedImage read(InputStream input) throws IOException { if (input == null) throw new IllegalArgumentException("null argument"); return read(new MemoryCacheImageInputStream(input)); } /** * Create a buffered image from a file. An image reader that * supports the given image data is automatically selected from the * collection of registered readers. If no registered reader can * handle the input format, null is returned. * * The image data will be cached in the current cache directory if * caching is enabled. * * This method does not locate readers that read data directly from * a file. To locate such readers manually, use IIORegistry and * ImageReaderSpi. * * @param input the file from which to read image data * * @return a new buffered image created from the given image file, * or null * * @exception IllegalArgumentException if input is null * @exception IOException if a reading error occurs */ public static BufferedImage read(File input) throws IOException { if (input == null) throw new IllegalArgumentException("null argument"); return read(new FileInputStream(input)); } /** * Create an image input stream from the given object. The * collection of ImageInputStreamSpis registered with the * IIORegistry is searched for an image input stream that can take * input from the given object. null is returned if no such SPI is * registered. * * The image data will be cached in the current cache directory if * caching is enabled. * * @param input an object from which to read image data * * @return an ImageInputStream that can read data from input, or * null * * @exception IllegalArgumentException if input is null * @exception IOException if caching is required but not enabled */ public static ImageInputStream createImageInputStream (Object input) throws IOException { if (input == null) throw new IllegalArgumentException ("null argument"); Iterator spis = getRegistry().getServiceProviders (ImageInputStreamSpi.class, true); ImageInputStreamSpi foundSpi = null; while(spis.hasNext()) { ImageInputStreamSpi spi = (ImageInputStreamSpi) spis.next(); if (input.getClass().equals(spi.getInputClass())) { foundSpi = spi; break; } } return foundSpi == null ? null : foundSpi.createInputStreamInstance (input, getUseCache(), getCacheDirectory()); } /** * Create an image output stream from the given object. The * collection of ImageOutputStreamSpis registered with the * IIORegistry is searched for an image output stream that can send * output to the given object. null is returned if no such SPI is * registered. * * The image data will be cached in the current cache directory if * caching is enabled. * * @param output an object to which to write image data * * @return an ImageOutputStream that can send data to output, or * null * * @exception IllegalArgumentException if output is null * @exception IOException if caching is required but not enabled */ public static ImageOutputStream createImageOutputStream (Object output) throws IOException { if (output == null) throw new IllegalArgumentException ("null argument"); Iterator spis = getRegistry().getServiceProviders (ImageOutputStreamSpi.class, true); ImageOutputStreamSpi foundSpi = null; while(spis.hasNext()) { ImageOutputStreamSpi spi = (ImageOutputStreamSpi) spis.next(); if (output.getClass().equals(spi.getOutputClass())) { foundSpi = spi; break; } } return foundSpi == null ? null : foundSpi.createOutputStreamInstance (output, getUseCache(), getCacheDirectory()); } /** * Retrieve an image reader corresponding to an image writer, or * null if writer is not registered or if no corresponding reader is * registered. * * @param writer a registered image writer * * @return an image reader corresponding to writer, or null * * @exception IllegalArgumentException if writer is null */ public static ImageReader getImageReader (ImageWriter writer) { if (writer == null) throw new IllegalArgumentException ("null argument"); ImageWriterSpi spi = (ImageWriterSpi) getRegistry() .getServiceProviderByClass(writer.getClass()); String[] readerSpiNames = spi.getImageReaderSpiNames(); ImageReader r = null; if (readerSpiNames != null) { try { Class readerClass = Class.forName (readerSpiNames[0]); r = (ImageReader) readerClass.newInstance (); } catch (Exception e) { return null; } } return r; } /** * Retrieve an iterator over the collection of registered image * readers that support reading data from the given object. * * @param input the object for which to retrieve image readers * * @return an iterator over a collection of image readers */ public static Iterator getImageReaders (Object input) { if (input == null) throw new IllegalArgumentException ("null argument"); return getRegistry().getServiceProviders (ImageReaderSpi.class, new ReaderObjectFilter(input), true); } /** * Retrieve an iterator over the collection of registered image * writers that support writing images of the given type and in the * given format. * * @param type the output image's colour and sample models * @param formatName the output image format * * @return an iterator over a collection of image writers */ public static Iterator getImageWriters (ImageTypeSpecifier type, String formatName) { if (type == null || formatName == null) throw new IllegalArgumentException ("null argument"); return getRegistry().getServiceProviders (ImageWriterSpi.class, new WriterObjectFilter(type, formatName), true); } /** * Retrieve an image writer corresponding to an image reader, or * null if reader is not registered or if no corresponding writer is * registered. This method is useful for preserving metadata * without needing to understand its format, since the returned * writer will be able to write, unchanged, the metadata passed to * it by the reader. * * @param reader a registered image reader * * @return an image writer corresponding to reader, or null * * @exception IllegalArgumentException if reader is null */ public static ImageWriter getImageWriter (ImageReader reader) { if (reader == null) throw new IllegalArgumentException ("null argument"); ImageReaderSpi spi = (ImageReaderSpi) getRegistry() .getServiceProviderByClass(reader.getClass()); String[] writerSpiNames = spi.getImageWriterSpiNames(); ImageWriter w = null; if (writerSpiNames != null) { try { Class writerClass = Class.forName (writerSpiNames[0]); w = (ImageWriter) writerClass.newInstance (); } catch (Exception e) { return null; } } return w; } /** * Retrieve an iterator over a collection of image transcoders that * support transcoding from the given image reader's metadata format * to the given writer's metadata format. * * @param reader an image reader * @param writer an image writer * * @return an iterator over a collection of image transcoders * * @exception IllegalArgumentException if either reader or writer is * null */ public static Iterator getImageTranscoders (ImageReader reader, ImageWriter writer) { if (reader == null || writer == null) throw new IllegalArgumentException ("null argument"); return getRegistry().getServiceProviders (ImageTranscoderSpi.class, new TranscoderFilter (reader, writer), true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -