public final class ImageIO extends Object
ImageReader
s and ImageWriter
s, and
performing simple encoding and decoding.-
Method Summary
Modifier and Type Method Description static ImageInputStream
createImageInputStream(Object input)
Returns anImageInputStream
that will take its input from the givenObject
.static ImageOutputStream
createImageOutputStream(Object output)
Returns anImageOutputStream
that will send its output to the givenObject
.static File
getCacheDirectory()
Returns the current value set bysetCacheDirectory
, ornull
if no explicit setting has been made.static ImageReader
getImageReader(ImageWriter writer)
Returns anImageReader
corresponding to the givenImageWriter
, if there is one, ornull
if the plug-in for thisImageWriter
does not specify a correspondingImageReader
, or if the givenImageWriter
is not registered.static Iterator<ImageReader>
getImageReaders(Object input)
Returns anIterator
containing all currently registeredImageReader
s that claim to be able to decode the suppliedObject
, typically anImageInputStream
.static Iterator<ImageReader>
getImageReadersByFormatName(String formatName)
Returns anIterator
containing all currently registeredImageReader
s that claim to be able to decode the named format.static Iterator<ImageReader>
getImageReadersByMIMEType(String MIMEType)
Returns anIterator
containing all currently registeredImageReader
s that claim to be able to decode files with the given MIME type.static Iterator<ImageReader>
getImageReadersBySuffix(String fileSuffix)
Returns anIterator
containing all currently registeredImageReader
s that claim to be able to decode files with the given suffix.static Iterator<ImageTranscoder>
getImageTranscoders(ImageReader reader, ImageWriter writer)
Returns anIterator
containing all currently registeredImageTranscoder
s that claim to be able to transcode between the metadata of the givenImageReader
andImageWriter
.static ImageWriter
getImageWriter(ImageReader reader)
Returns anImageWriter
corresponding to the givenImageReader
, if there is one, ornull
if the plug-in for thisImageReader
does not specify a correspondingImageWriter
, or if the givenImageReader
is not registered.static Iterator<ImageWriter>
getImageWriters(ImageTypeSpecifier type, String formatName)
Returns anIterator
containing all currently registeredImageWriter
s that claim to be able to encode images of the given layout (specified using anImageTypeSpecifier
) in the given format.static Iterator<ImageWriter>
getImageWritersByFormatName(String formatName)
Returns anIterator
containing all currently registeredImageWriter
s that claim to be able to encode the named format.static Iterator<ImageWriter>
getImageWritersByMIMEType(String MIMEType)
Returns anIterator
containing all currently registeredImageWriter
s that claim to be able to encode files with the given MIME type.static Iterator<ImageWriter>
getImageWritersBySuffix(String fileSuffix)
Returns anIterator
containing all currently registeredImageWriter
s that claim to be able to encode files with the given suffix.static String[]
getReaderFileSuffixes()
Returns an array ofString
s listing all of the file suffixes associated with the formats understood by the current set of registered readers.static String[]
getReaderFormatNames()
Returns an array ofString
s listing all of the informal format names understood by the current set of registered readers.static String[]
getReaderMIMETypes()
Returns an array ofString
s listing all of the MIME types understood by the current set of registered readers.static boolean
getUseCache()
Returns the current value set bysetUseCache
, ortrue
if no explicit setting has been made.static String[]
getWriterFileSuffixes()
Returns an array ofString
s listing all of the file suffixes associated with the formats understood by the current set of registered writers.static String[]
getWriterFormatNames()
Returns an array ofString
s listing all of the informal format names understood by the current set of registered writers.static String[]
getWriterMIMETypes()
Returns an array ofString
s listing all of the MIME types understood by the current set of registered writers.static BufferedImage
read(File input)
Returns aBufferedImage
as the result of decoding a suppliedFile
with anImageReader
chosen automatically from among those currently registered.static BufferedImage
read(InputStream input)
Returns aBufferedImage
as the result of decoding a suppliedInputStream
with anImageReader
chosen automatically from among those currently registered.static BufferedImage
read(URL input)
Returns aBufferedImage
as the result of decoding a suppliedURL
with anImageReader
chosen automatically from among those currently registered.static BufferedImage
read(ImageInputStream stream)
Returns aBufferedImage
as the result of decoding a suppliedImageInputStream
with anImageReader
chosen automatically from among those currently registered.static void
scanForPlugins()
Scans for plug-ins on the application class path, loads their service provider classes, and registers a service provider instance for each one found with theIIORegistry
.static void
setCacheDirectory(File cacheDirectory)
Sets the directory where cache files are to be created.static void
setUseCache(boolean useCache)
Sets a flag indicating whether a disk-based cache file should be used when creatingImageInputStream
s andImageOutputStream
s.static boolean
write(RenderedImage im, String formatName, File output)
Writes an image using an arbitraryImageWriter
that supports the given format to aFile
.static boolean
write(RenderedImage im, String formatName, OutputStream output)
Writes an image using an arbitraryImageWriter
that supports the given format to anOutputStream
.static boolean
write(RenderedImage im, String formatName, ImageOutputStream output)
Writes an image using the an arbitraryImageWriter
that supports the given format to anImageOutputStream
.
-
Method Details
-
scanForPlugins
public static void scanForPlugins()Scans for plug-ins on the application class path, loads their service provider classes, and registers a service provider instance for each one found with theIIORegistry
.This method is needed because the application class path can theoretically change, or additional plug-ins may become available. Rather than re-scanning the classpath on every invocation of the API, the class path is scanned automatically only on the first invocation. Clients can call this method to prompt a re-scan. Thus this method need only be invoked by sophisticated applications which dynamically make new plug-ins available at runtime.
The
getResources
method of the contextClassLoader
is used locate JAR files containing files namedMETA-INF/services/javax.imageio.spi.
classname, where classname is one ofImageReaderSpi
,ImageWriterSpi
,ImageTranscoderSpi
,ImageInputStreamSpi
, orImageOutputStreamSpi
, along the application class path.The contents of the located files indicate the names of actual implementation classes which implement the aforementioned service provider interfaces; the default class loader is then used to load each of these classes and to instantiate an instance of each class, which is then placed into the registry for later retrieval.
The exact set of locations searched depends on the implementation of the Java runtime environment.
-
setUseCache
public static void setUseCache(boolean useCache)Sets a flag indicating whether a disk-based cache file should be used when creatingImageInputStream
s andImageOutputStream
s.When reading from a standard
InputStream
, it may be necessary to save previously read information in a cache since the underlying stream does not allow data to be re-read. Similarly, when writing to a standardOutputStream
, a cache may be used to allow a previously written value to be changed before flushing it to the final destination.The cache may reside in main memory or on disk. Setting this flag to
false
disallows the use of disk for future streams, which may be advantageous when working with small images, as the overhead of creating and destroying files is removed.On startup, the value is set to
true
.- Parameters:
useCache
- aboolean
indicating whether a cache file should be used, in cases where it is optional.- See Also:
getUseCache()
-
getUseCache
public static boolean getUseCache()Returns the current value set bysetUseCache
, ortrue
if no explicit setting has been made.- Returns:
- true if a disk-based cache may be used for
ImageInputStream
s andImageOutputStream
s. - See Also:
setUseCache(boolean)
-
setCacheDirectory
Sets the directory where cache files are to be created. A value ofnull
indicates that the system-dependent default temporary-file directory is to be used. IfgetUseCache
returns false, this value is ignored.- Parameters:
cacheDirectory
- aFile
specifying a directory.- Throws:
SecurityException
- if the security manager denies access to the directory.IllegalArgumentException
- ifcacheDir
is non-null
but is not a directory.- See Also:
File.createTempFile(String, String, File)
,getCacheDirectory()
-
getCacheDirectory
Returns the current value set bysetCacheDirectory
, ornull
if no explicit setting has been made.- Returns:
- a
File
indicating the directory where cache files will be created, ornull
to indicate the system-dependent default temporary-file directory. - See Also:
setCacheDirectory(java.io.File)
-
createImageInputStream
Returns anImageInputStream
that will take its input from the givenObject
. The set ofImageInputStreamSpi
s registered with theIIORegistry
class is queried and the first one that is able to take input from the supplied object is used to create the returnedImageInputStream
. If no suitableImageInputStreamSpi
exists,null
is returned.The current cache settings from
getUseCache
andgetCacheDirectory
will be used to control caching.- Parameters:
input
- anObject
to be used as an input source, such as aFile
, readableRandomAccessFile
, orInputStream
.- Returns:
- an
ImageInputStream
, ornull
. - Throws:
IllegalArgumentException
- ifinput
isnull
.IOException
- if a cache file is needed but cannot be created.- See Also:
ImageInputStreamSpi
-
createImageOutputStream
Returns anImageOutputStream
that will send its output to the givenObject
. The set ofImageOutputStreamSpi
s registered with theIIORegistry
class is queried and the first one that is able to send output from the supplied object is used to create the returnedImageOutputStream
. If no suitableImageOutputStreamSpi
exists,null
is returned.The current cache settings from
getUseCache
andgetCacheDirectory
will be used to control caching.- Parameters:
output
- anObject
to be used as an output destination, such as aFile
, writableRandomAccessFile
, orOutputStream
.- Returns:
- an
ImageOutputStream
, ornull
. - Throws:
IllegalArgumentException
- ifoutput
isnull
.IOException
- if a cache file is needed but cannot be created.- See Also:
ImageOutputStreamSpi
-
getReaderFormatNames
Returns an array ofString
s listing all of the informal format names understood by the current set of registered readers.- Returns:
- an array of
String
s.
-
getReaderMIMETypes
Returns an array ofString
s listing all of the MIME types understood by the current set of registered readers.- Returns:
- an array of
String
s.
-
getReaderFileSuffixes
Returns an array ofString
s listing all of the file suffixes associated with the formats understood by the current set of registered readers.- Returns:
- an array of
String
s. - Since:
- 1.6
-
getImageReaders
Returns anIterator
containing all currently registeredImageReader
s that claim to be able to decode the suppliedObject
, typically anImageInputStream
.The stream position is left at its prior position upon exit from this method.
- Parameters:
input
- anImageInputStream
or otherObject
containing encoded image data.- Returns:
- an
Iterator
containingImageReader
s. - Throws:
IllegalArgumentException
- ifinput
isnull
.- See Also:
ImageReaderSpi.canDecodeInput(java.lang.Object)
-
getImageReadersByFormatName
Returns anIterator
containing all currently registeredImageReader
s that claim to be able to decode the named format.- Parameters:
formatName
- aString
containing the informal name of a format (e.g., "jpeg" or "tiff".- Returns:
- an
Iterator
containingImageReader
s. - Throws:
IllegalArgumentException
- ifformatName
isnull
.- See Also:
ImageReaderWriterSpi.getFormatNames()
-
getImageReadersBySuffix
Returns anIterator
containing all currently registeredImageReader
s that claim to be able to decode files with the given suffix.- Parameters:
fileSuffix
- aString
containing a file suffix (e.g., "jpg" or "tiff").- Returns:
- an
Iterator
containingImageReader
s. - Throws:
IllegalArgumentException
- iffileSuffix
isnull
.- See Also:
ImageReaderWriterSpi.getFileSuffixes()
-
getImageReadersByMIMEType
Returns anIterator
containing all currently registeredImageReader
s that claim to be able to decode files with the given MIME type.- Parameters:
MIMEType
- aString
containing a file suffix (e.g., "image/jpeg" or "image/x-bmp").- Returns:
- an
Iterator
containingImageReader
s. - Throws:
IllegalArgumentException
- ifMIMEType
isnull
.- See Also:
ImageReaderWriterSpi.getMIMETypes()
-
getWriterFormatNames
Returns an array ofString
s listing all of the informal format names understood by the current set of registered writers.- Returns:
- an array of
String
s.
-
getWriterMIMETypes
Returns an array ofString
s listing all of the MIME types understood by the current set of registered writers.- Returns:
- an array of
String
s.
-
getWriterFileSuffixes
Returns an array ofString
s listing all of the file suffixes associated with the formats understood by the current set of registered writers.- Returns:
- an array of
String
s. - Since:
- 1.6
-
getImageWritersByFormatName
Returns anIterator
containing all currently registeredImageWriter
s that claim to be able to encode the named format.- Parameters:
formatName
- aString
containing the informal name of a format (e.g., "jpeg" or "tiff".- Returns:
- an
Iterator
containingImageWriter
s. - Throws:
IllegalArgumentException
- ifformatName
isnull
.- See Also:
ImageReaderWriterSpi.getFormatNames()
-
getImageWritersBySuffix
Returns anIterator
containing all currently registeredImageWriter
s that claim to be able to encode files with the given suffix.- Parameters:
fileSuffix
- aString
containing a file suffix (e.g., "jpg" or "tiff").- Returns:
- an
Iterator
containingImageWriter
s. - Throws:
IllegalArgumentException
- iffileSuffix
isnull
.- See Also:
ImageReaderWriterSpi.getFileSuffixes()
-
getImageWritersByMIMEType
Returns anIterator
containing all currently registeredImageWriter
s that claim to be able to encode files with the given MIME type.- Parameters:
MIMEType
- aString
containing a file suffix (e.g., "image/jpeg" or "image/x-bmp").- Returns:
- an
Iterator
containingImageWriter
s. - Throws:
IllegalArgumentException
- ifMIMEType
isnull
.- See Also:
ImageReaderWriterSpi.getMIMETypes()
-
getImageWriter
Returns anImageWriter
corresponding to the givenImageReader
, if there is one, ornull
if the plug-in for thisImageReader
does not specify a correspondingImageWriter
, or if the givenImageReader
is not registered. This mechanism may be used to obtain anImageWriter
that will understand the internal structure of non-pixel metadata (as encoded byIIOMetadata
objects) generated by theImageReader
. By obtaining this data from theImageReader
and passing it on to theImageWriter
obtained with this method, a client program can read an image, modify it in some way, and write it back out preserving all metadata, without having to understand anything about the structure of the metadata, or even about the image format. Note that this method returns the "preferred" writer, which is the first in the list returned byjavax.imageio.spi.ImageReaderSpi.getImageWriterSpiNames()
.- Parameters:
reader
- an instance of a registeredImageReader
.- Returns:
- an
ImageWriter
, or null. - Throws:
IllegalArgumentException
- ifreader
isnull
.- See Also:
getImageReader(ImageWriter)
,ImageReaderSpi.getImageWriterSpiNames()
-
getImageReader
Returns anImageReader
corresponding to the givenImageWriter
, if there is one, ornull
if the plug-in for thisImageWriter
does not specify a correspondingImageReader
, or if the givenImageWriter
is not registered. This method is provided principally for symmetry withgetImageWriter(ImageReader)
. Note that this method returns the "preferred" reader, which is the first in the list returned by javax.imageio.spi.ImageWriterSpi.getImageReaderSpiNames()
.- Parameters:
writer
- an instance of a registeredImageWriter
.- Returns:
- an
ImageReader
, or null. - Throws:
IllegalArgumentException
- ifwriter
isnull
.- See Also:
getImageWriter(ImageReader)
,ImageWriterSpi.getImageReaderSpiNames()
-
getImageWriters
Returns anIterator
containing all currently registeredImageWriter
s that claim to be able to encode images of the given layout (specified using anImageTypeSpecifier
) in the given format.- Parameters:
type
- anImageTypeSpecifier
indicating the layout of the image to be written.formatName
- the informal name of theformat
.- Returns:
- an
Iterator
containingImageWriter
s. - Throws:
IllegalArgumentException
- if any parameter isnull
.- See Also:
ImageWriterSpi.canEncodeImage(ImageTypeSpecifier)
-
getImageTranscoders
public static Iterator<ImageTranscoder> getImageTranscoders(ImageReader reader, ImageWriter writer)Returns anIterator
containing all currently registeredImageTranscoder
s that claim to be able to transcode between the metadata of the givenImageReader
andImageWriter
.- Parameters:
reader
- anImageReader
.writer
- anImageWriter
.- Returns:
- an
Iterator
containingImageTranscoder
s. - Throws:
IllegalArgumentException
- ifreader
orwriter
isnull
.
-
read
Returns aBufferedImage
as the result of decoding a suppliedFile
with anImageReader
chosen automatically from among those currently registered. TheFile
is wrapped in anImageInputStream
. If no registeredImageReader
claims to be able to read the resulting stream,null
is returned.The current cache settings from
getUseCache
andgetCacheDirectory
will be used to control caching in theImageInputStream
that is created.Note that there is no
read
method that takes a filename as aString
; use this method instead after creating aFile
from the filename.This method does not attempt to locate
ImageReader
s that can read directly from aFile
; that may be accomplished usingIIORegistry
andImageReaderSpi
.- Parameters:
input
- aFile
to read from.- Returns:
- a
BufferedImage
containing the decoded contents of the input, ornull
. - Throws:
IllegalArgumentException
- ifinput
isnull
.IOException
- if an error occurs during reading or when not able to create required ImageInputStream.
-
read
Returns aBufferedImage
as the result of decoding a suppliedInputStream
with anImageReader
chosen automatically from among those currently registered. TheInputStream
is wrapped in anImageInputStream
. If no registeredImageReader
claims to be able to read the resulting stream,null
is returned.The current cache settings from
getUseCache
andgetCacheDirectory
will be used to control caching in theImageInputStream
that is created.This method does not attempt to locate
ImageReader
s that can read directly from anInputStream
; that may be accomplished usingIIORegistry
andImageReaderSpi
.This method does not close the provided
InputStream
after the read operation has completed; it is the responsibility of the caller to close the stream, if desired.- Parameters:
input
- anInputStream
to read from.- Returns:
- a
BufferedImage
containing the decoded contents of the input, ornull
. - Throws:
IllegalArgumentException
- ifinput
isnull
.IOException
- if an error occurs during reading or when not able to create required ImageInputStream.
-
read
Returns aBufferedImage
as the result of decoding a suppliedURL
with anImageReader
chosen automatically from among those currently registered. AnInputStream
is obtained from theURL
, which is wrapped in anImageInputStream
. If no registeredImageReader
claims to be able to read the resulting stream,null
is returned.The current cache settings from
getUseCache
andgetCacheDirectory
will be used to control caching in theImageInputStream
that is created.This method does not attempt to locate
ImageReader
s that can read directly from aURL
; that may be accomplished usingIIORegistry
andImageReaderSpi
.- Parameters:
input
- aURL
to read from.- Returns:
- a
BufferedImage
containing the decoded contents of the input, ornull
. - Throws:
IllegalArgumentException
- ifinput
isnull
.IOException
- if an error occurs during reading or when not able to create required ImageInputStream.
-
read
Returns aBufferedImage
as the result of decoding a suppliedImageInputStream
with anImageReader
chosen automatically from among those currently registered. If no registeredImageReader
claims to be able to read the stream,null
is returned.Unlike most other methods in this class, this method does close the provided
ImageInputStream
after the read operation has completed, unlessnull
is returned, in which case this method does not close the stream.- Parameters:
stream
- anImageInputStream
to read from.- Returns:
- a
BufferedImage
containing the decoded contents of the input, ornull
. - Throws:
IllegalArgumentException
- ifstream
isnull
.IOException
- if an error occurs during reading.
-
write
public static boolean write(RenderedImage im, String formatName, ImageOutputStream output) throws IOExceptionWrites an image using the an arbitraryImageWriter
that supports the given format to anImageOutputStream
. The image is written to theImageOutputStream
starting at the current stream pointer, overwriting existing stream data from that point forward, if present.This method does not close the provided
ImageOutputStream
after the write operation has completed; it is the responsibility of the caller to close the stream, if desired.- Parameters:
im
- aRenderedImage
to be written.formatName
- aString
containing the informal name of the format.output
- anImageOutputStream
to be written to.- Returns:
false
if no appropriate writer is found.- Throws:
IllegalArgumentException
- if any parameter isnull
.IOException
- if an error occurs during writing.
-
write
Writes an image using an arbitraryImageWriter
that supports the given format to aFile
. If there is already aFile
present, its contents are discarded.- Parameters:
im
- aRenderedImage
to be written.formatName
- aString
containing the informal name of the format.output
- aFile
to be written to.- Returns:
false
if no appropriate writer is found.- Throws:
IllegalArgumentException
- if any parameter isnull
.IOException
- if an error occurs during writing or when not able to create required ImageOutputStream.
-
write
public static boolean write(RenderedImage im, String formatName, OutputStream output) throws IOExceptionWrites an image using an arbitraryImageWriter
that supports the given format to anOutputStream
.This method does not close the provided
OutputStream
after the write operation has completed; it is the responsibility of the caller to close the stream, if desired.The current cache settings from
getUseCache
andgetCacheDirectory
will be used to control caching.- Parameters:
im
- aRenderedImage
to be written.formatName
- aString
containing the informal name of the format.output
- anOutputStream
to be written to.- Returns:
false
if no appropriate writer is found.- Throws:
IllegalArgumentException
- if any parameter isnull
.IOException
- if an error occurs during writing or when not able to create required ImageOutputStream.
-