ImageIcon icon = new ImageProvider(name).setMaxWidth(24).setMaxHeight(24).get();
* (there are more options, see below)
*
* short form:
* ImageIcon icon = ImageProvider.get(name);
*
* @author imi
*/
public class ImageProvider {
/**
* Position of an overlay icon
* @author imi
*/
public static enum OverlayPosition {
NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST
}
/**
* Supported image types
*/
public static enum ImageType {
/** Scalable vector graphics */
SVG,
/** Everything else, e.g. png, gif (must be supported by Java) */
OTHER
}
/**
* Property set on {@code BufferedImage} returned by {@link #makeImageTransparent}.
* @since 7132
*/
public static String PROP_TRANSPARENCY_FORCED = "josm.transparency.forced";
/**
* Property set on {@code BufferedImage} returned by {@link #read} if metadata is required.
* @since 7132
*/
public static String PROP_TRANSPARENCY_COLOR = "josm.transparency.color";
protected CollectionBufferedImage
as the result of decoding
* a supplied File
with an ImageReader
* chosen automatically from among those currently registered.
* The File
is wrapped in an
* ImageInputStream
. If no registered
* ImageReader
claims to be able to read the
* resulting stream, null
is returned.
*
* The current cache settings from getUseCache
and
* getCacheDirectory
will be used to control caching in the
* ImageInputStream
that is created.
*
*
Note that there is no read
method that takes a
* filename as a String
; use this method instead after
* creating a File
from the filename.
*
*
This method does not attempt to locate
* ImageReader
s that can read directly from a
* File
; that may be accomplished using
* IIORegistry
and ImageReaderSpi
.
*
* @param input a File
to read from.
* @param readMetadata if {@code true}, makes sure to read image metadata to detect transparency color, if any.
* In that case the color can be retrieved later through {@link #PROP_TRANSPARENCY_COLOR}.
* Always considered {@code true} if {@code enforceTransparency} is also {@code true}
* @param enforceTransparency if {@code true}, makes sure to read image metadata and, if the image does not
* provide an alpha channel but defines a {@code TransparentColor} metadata node, that the resulting image
* has a transparency set to {@code TRANSLUCENT} and uses the correct transparent color.
*
* @return a BufferedImage
containing the decoded
* contents of the input, or null
.
*
* @throws IllegalArgumentException if input
is null
.
* @throws IOException if an error occurs during reading.
* @since 7132
* @see BufferedImage#getProperty
*/
public static BufferedImage read(File input, boolean readMetadata, boolean enforceTransparency) throws IOException {
CheckParameterUtil.ensureParameterNotNull(input, "input");
if (!input.canRead()) {
throw new IIOException("Can't read input file!");
}
ImageInputStream stream = ImageIO.createImageInputStream(input);
if (stream == null) {
throw new IIOException("Can't create an ImageInputStream!");
}
BufferedImage bi = read(stream, readMetadata, enforceTransparency);
if (bi == null) {
stream.close();
}
return bi;
}
/**
* Returns a BufferedImage
as the result of decoding
* a supplied InputStream
with an ImageReader
* chosen automatically from among those currently registered.
* The InputStream
is wrapped in an
* ImageInputStream
. If no registered
* ImageReader
claims to be able to read the
* resulting stream, null
is returned.
*
*
The current cache settings from getUseCache
and
* getCacheDirectory
will be used to control caching in the
* ImageInputStream
that is created.
*
*
This method does not attempt to locate
* ImageReader
s that can read directly from an
* InputStream
; that may be accomplished using
* IIORegistry
and ImageReaderSpi
.
*
*
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.
*
* @param input an InputStream
to read from.
* @param readMetadata if {@code true}, makes sure to read image metadata to detect transparency color for non translucent images, if any.
* In that case the color can be retrieved later through {@link #PROP_TRANSPARENCY_COLOR}.
* Always considered {@code true} if {@code enforceTransparency} is also {@code true}
* @param enforceTransparency if {@code true}, makes sure to read image metadata and, if the image does not
* provide an alpha channel but defines a {@code TransparentColor} metadata node, that the resulting image
* has a transparency set to {@code TRANSLUCENT} and uses the correct transparent color.
*
* @return a BufferedImage
containing the decoded
* contents of the input, or null
.
*
* @throws IllegalArgumentException if input
is null
.
* @throws IOException if an error occurs during reading.
* @since 7132
*/
public static BufferedImage read(InputStream input, boolean readMetadata, boolean enforceTransparency) throws IOException {
CheckParameterUtil.ensureParameterNotNull(input, "input");
ImageInputStream stream = ImageIO.createImageInputStream(input);
BufferedImage bi = read(stream, readMetadata, enforceTransparency);
if (bi == null) {
stream.close();
}
return bi;
}
/**
* Returns a BufferedImage
as the result of decoding
* a supplied URL
with an ImageReader
* chosen automatically from among those currently registered. An
* InputStream
is obtained from the URL
,
* which is wrapped in an ImageInputStream
. If no
* registered ImageReader
claims to be able to read
* the resulting stream, null
is returned.
*
*
The current cache settings from getUseCache
and
* getCacheDirectory
will be used to control caching in the
* ImageInputStream
that is created.
*
*
This method does not attempt to locate
* ImageReader
s that can read directly from a
* URL
; that may be accomplished using
* IIORegistry
and ImageReaderSpi
.
*
* @param input a URL
to read from.
* @param readMetadata if {@code true}, makes sure to read image metadata to detect transparency color for non translucent images, if any.
* In that case the color can be retrieved later through {@link #PROP_TRANSPARENCY_COLOR}.
* Always considered {@code true} if {@code enforceTransparency} is also {@code true}
* @param enforceTransparency if {@code true}, makes sure to read image metadata and, if the image does not
* provide an alpha channel but defines a {@code TransparentColor} metadata node, that the resulting image
* has a transparency set to {@code TRANSLUCENT} and uses the correct transparent color.
*
* @return a BufferedImage
containing the decoded
* contents of the input, or null
.
*
* @throws IllegalArgumentException if input
is null
.
* @throws IOException if an error occurs during reading.
* @since 7132
*/
public static BufferedImage read(URL input, boolean readMetadata, boolean enforceTransparency) throws IOException {
CheckParameterUtil.ensureParameterNotNull(input, "input");
InputStream istream = null;
try {
istream = input.openStream();
} catch (IOException e) {
throw new IIOException("Can't get input stream from URL!", e);
}
ImageInputStream stream = ImageIO.createImageInputStream(istream);
BufferedImage bi;
try {
bi = read(stream, readMetadata, enforceTransparency);
if (bi == null) {
stream.close();
}
} finally {
istream.close();
}
return bi;
}
/**
* Returns a BufferedImage
as the result of decoding
* a supplied ImageInputStream
with an
* ImageReader
chosen automatically from among those
* currently registered. If no registered
* ImageReader
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, unless null
is returned,
* in which case this method does not close the stream.
*
* @param stream an ImageInputStream
to read from.
* @param readMetadata if {@code true}, makes sure to read image metadata to detect transparency color for non translucent images, if any.
* In that case the color can be retrieved later through {@link #PROP_TRANSPARENCY_COLOR}.
* Always considered {@code true} if {@code enforceTransparency} is also {@code true}
* @param enforceTransparency if {@code true}, makes sure to read image metadata and, if the image does not
* provide an alpha channel but defines a {@code TransparentColor} metadata node, that the resulting image
* has a transparency set to {@code TRANSLUCENT} and uses the correct transparent color.
*
* @return a BufferedImage
containing the decoded
* contents of the input, or null
.
*
* @throws IllegalArgumentException if stream
is null
.
* @throws IOException if an error occurs during reading.
* @since 7132
*/
public static BufferedImage read(ImageInputStream stream, boolean readMetadata, boolean enforceTransparency) throws IOException {
CheckParameterUtil.ensureParameterNotNull(stream, "stream");
Iterator