Summary
Extend the image loading capabilities of JavaFX by using the javax.imageio API, allowing applications to use third-party libraries to load a wide variety of image formats.
Problem
JavaFX can load BMP, GIF, PNG, and JPEG images with its built-in image loaders. It has been a long-standing request to support more image formats, most notably (but not limited to) SVG. However, adding more built-in image loaders is a significant effort not only in creating the functionality, but also in maintaining the additional dependencies.
This will probably not happen any time soon, so we are left with three alternatives:
- Accept the fact that JavaFX will never be able to load additional image formats.
- Create a public image loader API, and hope that developers in the JavaFX ecosystem will create image loader plugins.
- Leverage the existing Java Image I/O API.
From these options, supporting existing Java APIs seems to be the most sensible choice; both because it is the shortest and most realistic path forward, but also because it doesn't bifurcate pluggable image loading in the Java ecosystem.
Solution
The built-in JavaFX image loaders continue to be used for all supported image formats (BMP, GIF, PNG, JPEG). If an image can't be loaded using one of the built-in image loaders, JavaFX tries to use the new J2DImageLoader
to probe the ImageIO registry for image readers that can load the image instead.
J2DImageLoader
might fail if the java.desktop
module is not on the module path. This is not an error, it simply means that ImageIO will not be available for the JavaFX application.
Specification
--- a/modules/javafx.graphics/src/main/java/javafx/scene/image/Image.java
+++ b/modules/javafx.graphics/src/main/java/javafx/scene/image/Image.java
@@ -65,7 +65,7 @@
* images from a specified URL.
*
* <p>
- * Supported image formats are:
+ * JavaFX has built-in support for the following image formats:
* <ul>
* <li><a href="http://msdn.microsoft.com/en-us/library/dd183376(v=vs.85).aspx">BMP</a></li>
* <li><a href="http://www.w3.org/Graphics/GIF/spec-gif89a.txt">GIF</a></li>
@@ -72,6 +72,8 @@
* <li><a href="http://www.ijg.org">JPEG</a></li>
* <li><a href="http://www.libpng.org/pub/png/spec/">PNG</a></li>
* </ul>
+ * For all other formats, JavaFX uses the {@link javax.imageio Java Image I/O API} on supported platforms.
+ * Image I/O is extensible so that developers can "plug-in" support for additional formats.
*
* <p>
* Images can be resized as they are loaded (for example to reduce the amount of
- csr of
-
JDK-8306707 Support pluggable image loading via javax.imageio
- Resolved