public class ImageProvider extends java.lang.Object
ImageIcon icon = new ImageProvider(name).setMaxSize(ImageSizes.MAP).get();
(there are more options, see below)
short form:
ImageIcon icon = ImageProvider.get(name);
Modifier and Type | Class and Description |
---|---|
static class |
ImageProvider.ImageSizes
Supported image sizes
|
static class |
ImageProvider.ImageType
Supported image types
|
Modifier and Type | Field and Description |
---|---|
protected java.io.File |
archive
archive file to take image from
|
private static java.util.Map<java.lang.String,ImageResource> |
cache
The icon cache
|
private static java.util.Set<java.lang.ClassLoader> |
classLoaders
set of class loaders to take images from
|
private static java.util.regex.Pattern |
dataUrlPattern
data:[<mediatype>][;base64],<data> |
private static double |
DEGREE_90
90 degrees in radians units
|
protected java.util.Collection<java.lang.String> |
dirs
directories in which images are searched
|
private static java.lang.String |
HTTP_PROTOCOL |
private static java.lang.String |
HTTPS_PROTOCOL |
protected java.lang.String |
id
caching identifier
|
private static java.util.concurrent.ExecutorService |
IMAGE_FETCHER |
protected java.lang.String |
inArchiveDir
directory inside the archive
|
protected boolean |
isDisabled
true if icon must be grayed out |
protected boolean |
multiResolution
true if multi-resolution image is requested |
protected java.lang.String |
name
image file name
|
protected boolean |
optional
In case of errors do not throw exception but return
null for missing image |
private static java.util.Map<OsmPrimitiveType,javax.swing.ImageIcon> |
osmPrimitiveTypeCache
small cache of critical images used in many parts of the application
|
protected java.util.List<ImageOverlay> |
overlayInfo
ordered list of overlay images
|
private static java.util.Map<java.awt.Dimension,java.util.Map<MapImage,javax.swing.ImageIcon>> |
paddedImageCache
larger cache of critical padded image icons used in many parts of the application
|
static java.lang.String |
PROP_TRANSPARENCY_COLOR
Property set on
BufferedImage returned by read(java.io.File, boolean, boolean) if metadata is required. |
static java.lang.String |
PROP_TRANSPARENCY_FORCED
Property set on
BufferedImage returned by makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color) . |
private static java.util.Map<java.awt.Image,java.util.Map<java.lang.Long,java.awt.Image>> |
ROTATE_CACHE
Caches the image data for rotated versions of the same image.
|
protected java.lang.String |
subdir
sub directory the image can be found in
|
protected boolean |
suppressWarnings
true if warnings should be suppressed |
private static com.kitfox.svg.SVGUniverse |
svgUniverse |
protected int |
virtualHeight
virtual height of the resulting image, -1 when original image data should be used
|
protected int |
virtualMaxHeight
virtual maximum height of the resulting image, -1 for no restriction
|
protected int |
virtualMaxWidth
virtual maximum width of the resulting image, -1 for no restriction
|
protected int |
virtualWidth
virtual width of the resulting image, -1 when original image data should be used
|
private static java.lang.String |
WIKI_PROTOCOL |
Constructor and Description |
---|
ImageProvider(ImageProvider image)
Constructs a new
ImageProvider from an existing one. |
ImageProvider(java.lang.String name)
Constructs a new
ImageProvider from a filename. |
ImageProvider(java.lang.String subdir,
java.lang.String name)
Constructs a new
ImageProvider from a filename in a given directory. |
Modifier and Type | Method and Description |
---|---|
static boolean |
addAdditionalClassLoader(java.lang.ClassLoader additionalClassLoader)
Add an additional class loader to search image for.
|
static boolean |
addAdditionalClassLoaders(java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)
Add a collection of additional class loaders to search image for.
|
ImageProvider |
addOverlay(ImageOverlay overlay)
Add an overlay over the image.
|
static void |
clearCache()
Clears the internal image caches.
|
static javax.swing.ImageIcon |
createBlankIcon(ImageProvider.ImageSizes size)
Creates a blank icon of the given size.
|
static java.awt.Image |
createBoundedImage(java.awt.Image img,
int maxSize)
Creates a scaled down version of the input image to fit maximum dimensions.
|
static java.awt.image.BufferedImage |
createImageFromSvg(com.kitfox.svg.SVGDiagram svg,
java.awt.Dimension dim)
Constructs an image from the given SVG data.
|
private static javax.imageio.stream.ImageInputStream |
createImageInputStream(java.lang.Object input) |
static java.awt.Image |
createRotatedImage(java.awt.Image img,
double rotatedAngle)
Creates a rotated version of the input image.
|
static java.awt.Image |
createRotatedImage(java.awt.Image img,
double rotatedAngle,
java.awt.Dimension dimension)
Creates a rotated version of the input image.
|
static java.awt.image.BufferedImage |
createScaledImage(java.awt.image.BufferedImage img,
int targetWidth,
int targetHeight,
java.lang.Object hint)
Returns a scaled instance of the provided
BufferedImage . |
javax.swing.ImageIcon |
get()
Execute the image request and scale result.
|
static javax.swing.ImageIcon |
get(OsmPrimitiveType type)
Replies the icon for an OSM primitive type
|
static javax.swing.ImageIcon |
get(java.lang.String name)
Load an image with a given file name.
|
static javax.swing.ImageIcon |
get(java.lang.String name,
ImageProvider.ImageSizes size)
Load an image with a given file name and size.
|
static javax.swing.ImageIcon |
get(java.lang.String subdir,
java.lang.String name)
Load an image with a given file name.
|
static javax.swing.ImageIcon |
get(java.lang.String subdir,
java.lang.String name,
ImageProvider.ImageSizes size)
Load an image from directory with a given file name and size.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
getAsync(java.util.function.Consumer<? super javax.swing.ImageIcon> action)
Load the image in a background thread.
|
static java.awt.Cursor |
getCursor(java.lang.String name,
java.lang.String overlay)
Load a cursor with a given file name, optionally decorated with an overlay image.
|
static javax.swing.ImageIcon |
getEmpty(ImageProvider.ImageSizes size)
Load an empty image with a given size.
|
static javax.swing.ImageIcon |
getIfAvailable(java.lang.String name)
Load an image with a given file name, but do not throw an exception
when the image cannot be found.
|
static javax.swing.ImageIcon |
getIfAvailable(java.lang.String subdir,
java.lang.String name)
Load an image with a given file name, but do not throw an exception
when the image cannot be found.
|
private static ImageResource |
getIfAvailableDataUrl(java.lang.String url)
Internal implementation of the image request for inline images (data: urls).
|
private static ImageResource |
getIfAvailableHttp(java.lang.String url,
ImageProvider.ImageType type)
Internal implementation of the image request for URL's.
|
private ImageResource |
getIfAvailableImpl()
Internal implementation of the image request.
|
private static ImageResource |
getIfAvailableLocalURL(java.net.URL path,
ImageProvider.ImageType type)
Internal implementation of the image request for local images.
|
private static ImageResource |
getIfAvailableWiki(java.lang.String name,
ImageProvider.ImageType type)
Internal implementation of the image request for wiki images.
|
private static ImageResource |
getIfAvailableZip(java.lang.String fullName,
java.io.File archive,
java.lang.String inArchiveDir,
ImageProvider.ImageType type)
Internal implementation of the image request for images in Zip archives.
|
private java.net.URL |
getImageUrl(java.lang.String imageName) |
private static java.net.URL |
getImageUrl(java.lang.String path,
java.lang.String name) |
private static java.lang.String |
getImgUrlFromWikiInfoPage(java.lang.String base,
java.lang.String fn)
Reads the wiki page on a certain file in html format in order to find the real image URL.
|
static javax.swing.ImageIcon |
getPadded(OsmPrimitive primitive,
java.awt.Dimension iconSize) |
static javax.swing.ImageIcon |
getPaddedIcon(MapImage mapImage,
java.awt.Dimension iconSize)
Returns an
ImageIcon for the given map image, at the specified size. |
ImageResource |
getResource()
Execute the image request.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
getResourceAsync(java.util.function.Consumer<? super ImageResource> action)
Load the image in a background thread.
|
private static com.kitfox.svg.SVGUniverse |
getSvgUniverse() |
private static javax.swing.ImageIcon |
getTaggedPadded(OsmPrimitive primitive,
java.awt.Dimension iconSize)
Computes a new padded icon for the given tagged primitive, using map paint styles.
|
static java.awt.Color |
getTransparentColor(java.awt.image.ColorModel model,
javax.imageio.ImageReader reader)
Returns the
TransparentColor defined in image reader metadata. |
static boolean |
hasTransparentColor(java.awt.image.BufferedImage bi)
Determines if the given
BufferedImage has a transparent color determined by a previous call to read(java.io.File, boolean, boolean) . |
boolean |
isRemote()
Determines if this icon is located on a remote location (http, https, wiki).
|
static boolean |
isTransparencyForced(java.awt.image.BufferedImage bi)
Determines if the transparency of the given
BufferedImage has been enforced by a previous call to makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color) . |
static java.awt.image.BufferedImage |
makeImageTransparent(java.awt.image.BufferedImage bi,
java.awt.Color color)
Returns a transparent version of the given image, based on the given transparent color.
|
private static java.awt.Color |
parseRGB(java.lang.String... s) |
static java.awt.image.BufferedImage |
read(java.io.File input,
boolean readMetadata,
boolean enforceTransparency)
Returns a
BufferedImage as the result of decoding
a supplied File with an ImageReader
chosen automatically from among those currently registered. |
static java.awt.image.BufferedImage |
read(javax.imageio.stream.ImageInputStream stream,
boolean readMetadata,
boolean enforceTransparency)
Returns a
BufferedImage as the result of decoding
a supplied ImageInputStream with an
ImageReader chosen automatically from among those
currently registered. |
static java.awt.image.BufferedImage |
read(java.io.InputStream input,
boolean readMetadata,
boolean enforceTransparency)
Returns a
BufferedImage as the result of decoding
a supplied InputStream with an ImageReader
chosen automatically from among those currently registered. |
static java.awt.image.BufferedImage |
read(java.net.URL input,
boolean readMetadata,
boolean enforceTransparency)
Returns a
BufferedImage as the result of decoding
a supplied URL with an ImageReader
chosen automatically from among those currently registered. |
ImageProvider |
resetMaxSize(java.awt.Dimension maxSize)
Limit the maximum size of the image.
|
ImageProvider |
setArchive(java.io.File archive)
Specify a zip file where the image is located.
|
ImageProvider |
setDirs(java.util.Collection<java.lang.String> dirs)
Directories to look for the image.
|
ImageProvider |
setDisabled(boolean disabled)
Set, if image must be filtered to grayscale so it will look like disabled icon.
|
ImageProvider |
setHeight(int height)
Set image height
|
ImageProvider |
setId(java.lang.String id)
Set an id used for caching.
|
ImageProvider |
setInArchiveDir(java.lang.String inArchiveDir)
Specify a base path inside the zip file.
|
ImageProvider |
setMaxHeight(int maxHeight)
Limit the maximum height of the image.
|
ImageProvider |
setMaxSize(java.awt.Dimension maxSize)
Limit the maximum size of the image.
|
ImageProvider |
setMaxSize(ImageProvider.ImageSizes size)
Limit the maximum size of the image.
|
ImageProvider |
setMaxSize(int maxSize)
Convenience method, see
setMaxSize(Dimension) . |
ImageProvider |
setMaxWidth(int maxWidth)
Limit the maximum width of the image.
|
ImageProvider |
setMultiResolution(boolean multiResolution)
Decide, if multi-resolution image is requested (default
true ). |
ImageProvider |
setOptional(boolean optional)
Decide, if an exception should be thrown, when the image cannot be located.
|
ImageProvider |
setSize(java.awt.Dimension size)
Set the dimensions of the image.
|
ImageProvider |
setSize(ImageProvider.ImageSizes size)
Set the dimensions of the image.
|
ImageProvider |
setSize(int width,
int height)
Set the dimensions of the image.
|
ImageProvider |
setSuppressWarnings(boolean suppressWarnings)
Suppresses warning on the command line in case the image cannot be found.
|
ImageProvider |
setWidth(int width)
Set image width
|
static void |
shutdown(boolean now)
Shutdown background image fetcher.
|
static java.awt.image.BufferedImage |
toBufferedImage(java.awt.Image image)
Converts an
Image to a BufferedImage instance. |
static java.awt.image.BufferedImage |
toBufferedImage(java.awt.Image image,
java.awt.Rectangle cropArea)
Converts an
Rectangle area of Image to a BufferedImage instance. |
java.lang.String |
toString() |
private static final java.lang.String HTTP_PROTOCOL
private static final java.lang.String HTTPS_PROTOCOL
private static final java.lang.String WIKI_PROTOCOL
public static final java.lang.String PROP_TRANSPARENCY_FORCED
BufferedImage
returned by makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
.public static final java.lang.String PROP_TRANSPARENCY_COLOR
BufferedImage
returned by read(java.io.File, boolean, boolean)
if metadata is required.private static final java.util.Set<java.lang.ClassLoader> classLoaders
protected java.util.Collection<java.lang.String> dirs
protected java.lang.String id
protected java.lang.String subdir
protected final java.lang.String name
protected java.io.File archive
protected java.lang.String inArchiveDir
protected int virtualWidth
protected int virtualHeight
protected int virtualMaxWidth
protected int virtualMaxHeight
protected boolean optional
null
for missing imageprotected boolean suppressWarnings
true
if warnings should be suppressedprotected java.util.List<ImageOverlay> overlayInfo
protected boolean isDisabled
true
if icon must be grayed outprotected boolean multiResolution
true
if multi-resolution image is requestedprivate static com.kitfox.svg.SVGUniverse svgUniverse
private static final java.util.Map<java.lang.String,ImageResource> cache
private static final java.util.Map<java.awt.Image,java.util.Map<java.lang.Long,java.awt.Image>> ROTATE_CACHE
private static final java.util.Map<OsmPrimitiveType,javax.swing.ImageIcon> osmPrimitiveTypeCache
private static final java.util.Map<java.awt.Dimension,java.util.Map<MapImage,javax.swing.ImageIcon>> paddedImageCache
private static final java.util.concurrent.ExecutorService IMAGE_FETCHER
private static final java.util.regex.Pattern dataUrlPattern
data:[<mediatype>][;base64],<data>
private static final double DEGREE_90
public ImageProvider(java.lang.String subdir, java.lang.String name)
ImageProvider
from a filename in a given directory.subdir
- subdirectory the image lies inname
- the name of the image. If it does not end with '.png' or '.svg',
both extensions are tried.java.lang.NullPointerException
- if name is nullpublic ImageProvider(java.lang.String name)
ImageProvider
from a filename.name
- the name of the image. If it does not end with '.png' or '.svg',
both extensions are tried.java.lang.NullPointerException
- if name is nullpublic ImageProvider(ImageProvider image)
ImageProvider
from an existing one.image
- the existing image provider to be copiedpublic ImageProvider setDirs(java.util.Collection<java.lang.String> dirs)
dirs
- The directories to look for.public ImageProvider setId(java.lang.String id)
http://
Id is not used for the cache.
(A URL is unique anyway.)id
- the id for the cached imagepublic ImageProvider setArchive(java.io.File archive)
archive
- zip file where the image is locatedpublic ImageProvider setInArchiveDir(java.lang.String inArchiveDir)
inArchiveDir
- path inside the archivepublic ImageProvider addOverlay(ImageOverlay overlay)
overlay
- overlay image and placement specificationpublic ImageProvider setSize(java.awt.Dimension size)
size
- final dimensions of the imagepublic ImageProvider setSize(ImageProvider.ImageSizes size)
size
- final dimensions of the imagepublic ImageProvider setSize(int width, int height)
width
- final width of the imageheight
- final height of the imagepublic ImageProvider setWidth(int width)
width
- final width of the imagesetSize(java.awt.Dimension)
public ImageProvider setHeight(int height)
height
- final height of the imagesetSize(java.awt.Dimension)
public ImageProvider setMaxSize(java.awt.Dimension maxSize)
maxSize
- maximum image sizepublic ImageProvider resetMaxSize(java.awt.Dimension maxSize)
maxSize
- maximum image sizesetMaxSize(Dimension)
public ImageProvider setMaxSize(ImageProvider.ImageSizes size)
size
- maximum image sizepublic ImageProvider setMaxSize(int maxSize)
setMaxSize(Dimension)
.maxSize
- maximum image sizepublic ImageProvider setMaxWidth(int maxWidth)
maxWidth
- maximum image widthsetMaxSize(java.awt.Dimension)
public ImageProvider setMaxHeight(int maxHeight)
maxHeight
- maximum image heightsetMaxSize(java.awt.Dimension)
public ImageProvider setOptional(boolean optional)
optional
- true, if JOSM should not throw a RuntimeException
in case the image cannot be located.public ImageProvider setSuppressWarnings(boolean suppressWarnings)
suppressWarnings
- if true
warnings are suppressedpublic static boolean addAdditionalClassLoader(java.lang.ClassLoader additionalClassLoader)
additionalClassLoader
- class loader to add to the internal settrue
if the set changed as a result of the callpublic static boolean addAdditionalClassLoaders(java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)
additionalClassLoaders
- class loaders to add to the internal settrue
if the set changed as a result of the callpublic ImageProvider setDisabled(boolean disabled)
disabled
- true, if image must be grayed out for disabled statepublic ImageProvider setMultiResolution(boolean multiResolution)
true
).
A java.awt.image.MultiResolutionImage
is a Java 9 Image
implementation, which adds support for HiDPI displays. The effect will be
that in HiDPI mode, when GUI elements are scaled by a factor 1.5, 2.0, etc.,
the images are not just up-scaled, but a higher resolution version of the image is rendered instead.
Use HiDPISupport.getBaseImage(java.awt.Image)
to extract the original image from a multi-resolution image.
See HiDPISupport.processMRImage(java.awt.Image, java.util.function.UnaryOperator<java.awt.Image>)
for how to process the image without removing the multi-resolution magic.
multiResolution
- true, if multi-resolution image is requestedpublic boolean isRemote()
true
if this icon is located on a remote location (http, https, wiki)public javax.swing.ImageIcon get()
public java.util.concurrent.CompletableFuture<java.lang.Void> getAsync(java.util.function.Consumer<? super javax.swing.ImageIcon> action)
action
- the action that will deal with the imagepublic ImageResource getResource()
public java.util.concurrent.CompletableFuture<java.lang.Void> getResourceAsync(java.util.function.Consumer<? super ImageResource> action)
action
- the action that will deal with the imagepublic static javax.swing.ImageIcon get(java.lang.String subdir, java.lang.String name)
subdir
- subdirectory the image lies inname
- The icon name (base name with or without '.png' or '.svg' extension)java.lang.RuntimeException
- if the image cannot be locatedpublic static javax.swing.ImageIcon get(java.lang.String name)
name
- The icon name (base name with or without '.png' or '.svg' extension)get(String, String)
public static javax.swing.ImageIcon get(java.lang.String subdir, java.lang.String name, ImageProvider.ImageSizes size)
subdir
- subdirectory the image lies inname
- The icon name (base name with or without '.png' or '.svg' extension)size
- Target icon sizejava.lang.RuntimeException
- if the image cannot be locatedpublic static javax.swing.ImageIcon getEmpty(ImageProvider.ImageSizes size)
size
- Target icon sizepublic static javax.swing.ImageIcon getIfAvailable(java.lang.String subdir, java.lang.String name)
subdir
- subdirectory the image lies inname
- The icon name (base name with or without '.png' or '.svg' extension)get(String, String)
public static javax.swing.ImageIcon get(java.lang.String name, ImageProvider.ImageSizes size)
name
- The icon name (base name with or without '.png' or '.svg' extension)size
- Target icon sizeget(String, String)
public static javax.swing.ImageIcon getIfAvailable(java.lang.String name)
name
- The icon name (base name with or without '.png' or '.svg' extension)getIfAvailable(String, String)
public static void clearCache()
private ImageResource getIfAvailableImpl()
private static ImageResource getIfAvailableHttp(java.lang.String url, ImageProvider.ImageType type)
url
- URL of the imagetype
- data type of the imageprivate static ImageResource getIfAvailableDataUrl(java.lang.String url)
url
- the data URL for image extractionprivate static ImageResource getIfAvailableWiki(java.lang.String name, ImageProvider.ImageType type)
name
- image file nametype
- data type of the imageprivate static ImageResource getIfAvailableZip(java.lang.String fullName, java.io.File archive, java.lang.String inArchiveDir, ImageProvider.ImageType type)
fullName
- image file namearchive
- the archive to get image frominArchiveDir
- directory of the image inside the archive or null
type
- data type of the imageprivate static ImageResource getIfAvailableLocalURL(java.net.URL path, ImageProvider.ImageType type)
path
- image file pathtype
- data type of the imageprivate static java.net.URL getImageUrl(java.lang.String path, java.lang.String name)
private java.net.URL getImageUrl(java.lang.String imageName)
private static java.lang.String getImgUrlFromWikiInfoPage(java.lang.String base, java.lang.String fn)
base
- base URL for Wiki imagefn
- filename of the Wiki imagepublic static java.awt.Cursor getCursor(java.lang.String name, java.lang.String overlay)
name
- the cursor image filename in "cursor" directoryoverlay
- optional overlay imagepublic static java.awt.Image createRotatedImage(java.awt.Image img, double rotatedAngle)
img
- the image to be rotated.rotatedAngle
- the rotated angle, in degree, clockwise. It could be any double but we
will mod it with 360 before using it. More over for caching performance, it will be rounded to
an entire value between 0 and 360.public static java.awt.Image createRotatedImage(java.awt.Image img, double rotatedAngle, java.awt.Dimension dimension)
img
- the image to be rotated.rotatedAngle
- the rotated angle, in degree, clockwise. It could be any double but we
will mod it with 360 before using it. More over for caching performance, it will be rounded to
an entire value between 0 and 360.dimension
- ignoredpublic static java.awt.Image createBoundedImage(java.awt.Image img, int maxSize)
img
- the image to be scaled down.maxSize
- the maximum size in pixels (both for width and height)public static java.awt.image.BufferedImage createScaledImage(java.awt.image.BufferedImage img, int targetWidth, int targetHeight, java.lang.Object hint)
BufferedImage
.
This method will use a multi-step scaling technique that provides higher quality than the usual
one-step technique (only useful in downscaling cases, where targetWidth
or targetHeight
is
smaller than the original dimensions, and generally only when the BILINEAR
hint is specified).
From https://community.oracle.com/docs/DOC-983611: "The Perils of Image.getScaledInstance()"img
- the original image to be scaledtargetWidth
- the desired width of the scaled instance, in pixelstargetHeight
- the desired height of the scaled instance, in pixelshint
- one of the rendering hints that corresponds to
RenderingHints.KEY_INTERPOLATION
(e.g.
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR
,
RenderingHints.VALUE_INTERPOLATION_BILINEAR
,
RenderingHints.VALUE_INTERPOLATION_BICUBIC
)BufferedImage
public static javax.swing.ImageIcon get(OsmPrimitiveType type)
type
- the typepublic static javax.swing.ImageIcon getPadded(OsmPrimitive primitive, java.awt.Dimension iconSize)
primitive
- Object for which an icon shall be fetched. The icon is chosen based on tags.iconSize
- Target size of icon. Icon is padded if required.primitive
that fits in cell.private static javax.swing.ImageIcon getTaggedPadded(OsmPrimitive primitive, java.awt.Dimension iconSize)
primitive
- tagged OSM primitiveiconSize
- icon size in pixelspublic static javax.swing.ImageIcon getPaddedIcon(MapImage mapImage, java.awt.Dimension iconSize)
ImageIcon
for the given map image, at the specified size.
Uses a cache to improve performance.mapImage
- map imageiconSize
- size in pixelsImageIcon
for the given map image, at the specified sizeclearCache()
public static java.awt.image.BufferedImage createImageFromSvg(com.kitfox.svg.SVGDiagram svg, java.awt.Dimension dim)
svg
- the SVG datadim
- the desired image dimensionprivate static com.kitfox.svg.SVGUniverse getSvgUniverse()
public static java.awt.image.BufferedImage read(java.io.File input, boolean readMetadata, boolean enforceTransparency) throws java.io.IOException
BufferedImage
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
.
input
- a File
to read from.readMetadata
- if true
, makes sure to read image metadata to detect transparency color, if any.
In that case the color can be retrieved later through PROP_TRANSPARENCY_COLOR
.
Always considered true
if enforceTransparency
is also true
enforceTransparency
- if true
, makes sure to read image metadata and, if the image does not
provide an alpha channel but defines a TransparentColor
metadata node, that the resulting image
has a transparency set to TRANSLUCENT
and uses the correct transparent color.BufferedImage
containing the decoded contents of the input, or null
.java.lang.IllegalArgumentException
- if input
is null
.java.io.IOException
- if an error occurs during reading.BufferedImage.getProperty(java.lang.String, java.awt.image.ImageObserver)
public static java.awt.image.BufferedImage read(java.io.InputStream input, boolean readMetadata, boolean enforceTransparency) throws java.io.IOException
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.
input
- an InputStream
to read from.readMetadata
- if 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 PROP_TRANSPARENCY_COLOR
.
Always considered true
if enforceTransparency
is also true
enforceTransparency
- if true
, makes sure to read image metadata and, if the image does not
provide an alpha channel but defines a TransparentColor
metadata node, that the resulting image
has a transparency set to TRANSLUCENT
and uses the correct transparent color.BufferedImage
containing the decoded contents of the input, or null
.java.lang.IllegalArgumentException
- if input
is null
.java.io.IOException
- if an error occurs during reading.public static java.awt.image.BufferedImage read(java.net.URL input, boolean readMetadata, boolean enforceTransparency) throws java.io.IOException
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
.
input
- a URL
to read from.readMetadata
- if 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 PROP_TRANSPARENCY_COLOR
.
Always considered true
if enforceTransparency
is also true
enforceTransparency
- if true
, makes sure to read image metadata and, if the image does not
provide an alpha channel but defines a TransparentColor
metadata node, that the resulting image
has a transparency set to TRANSLUCENT
and uses the correct transparent color.BufferedImage
containing the decoded contents of the input, or null
.java.lang.IllegalArgumentException
- if input
is null
.java.io.IOException
- if an error occurs during reading.public static java.awt.image.BufferedImage read(javax.imageio.stream.ImageInputStream stream, boolean readMetadata, boolean enforceTransparency) throws java.io.IOException
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.
stream
- an ImageInputStream
to read from.readMetadata
- if 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 PROP_TRANSPARENCY_COLOR
.
Always considered true
if enforceTransparency
is also true
enforceTransparency
- if true
, makes sure to read image metadata and, if the image does not
provide an alpha channel but defines a TransparentColor
metadata node, that the resulting image
has a transparency set to TRANSLUCENT
and uses the correct transparent color. For Java < 11 only.BufferedImage
containing the decoded
contents of the input, or null
.java.lang.IllegalArgumentException
- if stream
is null
.java.io.IOException
- if an error occurs during reading.public static java.awt.Color getTransparentColor(java.awt.image.ColorModel model, javax.imageio.ImageReader reader) throws java.io.IOException
TransparentColor
defined in image reader metadata.model
- The image color modelreader
- The image readerTransparentColor
defined in image reader metadata, or null
java.io.IOException
- if an error occurs during readingprivate static java.awt.Color parseRGB(java.lang.String... s)
public static java.awt.image.BufferedImage makeImageTransparent(java.awt.image.BufferedImage bi, java.awt.Color color)
bi
- The image to convertcolor
- The transparent colorbi
where all pixels of the given color are transparent.
This resulting image has also the special property PROP_TRANSPARENCY_FORCED
set to color
BufferedImage.getProperty(java.lang.String, java.awt.image.ImageObserver)
,
isTransparencyForced(java.awt.image.BufferedImage)
public static boolean isTransparencyForced(java.awt.image.BufferedImage bi)
BufferedImage
has been enforced by a previous call to makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
.bi
- The BufferedImage
to testtrue
if the transparency of bi
has been enforced by a previous call to makeImageTransparent
.makeImageTransparent(java.awt.image.BufferedImage, java.awt.Color)
public static boolean hasTransparentColor(java.awt.image.BufferedImage bi)
BufferedImage
has a transparent color determined by a previous call to read(java.io.File, boolean, boolean)
.bi
- The BufferedImage
to testtrue
if bi
has a transparent color determined by a previous call to read
.read(java.io.File, boolean, boolean)
public static void shutdown(boolean now)
now
- if true
, attempts to stop all actively executing tasks, halts the processing of waiting tasks.
if false
, initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be acceptedpublic static java.awt.image.BufferedImage toBufferedImage(java.awt.Image image)
Image
to a BufferedImage
instance.image
- image to convertBufferedImage
instance for the given Image
.public static java.awt.image.BufferedImage toBufferedImage(java.awt.Image image, java.awt.Rectangle cropArea)
Rectangle
area of Image
to a BufferedImage
instance.image
- image to convertcropArea
- rectangle to crop image withBufferedImage
instance for the cropped area of Image
.private static javax.imageio.stream.ImageInputStream createImageInputStream(java.lang.Object input) throws java.io.IOException
java.io.IOException
public static javax.swing.ImageIcon createBlankIcon(ImageProvider.ImageSizes size)
size
- image sizepublic java.lang.String toString()
toString
in class java.lang.Object