I've been reading the OS X Java Developer Tools, in order to help make my application more "native" with the operating system. I found something interesting in this particular section. (emphasis mine)
To load a resolution-independent tiff, icns, or pdf file from the Resources folder of your application bundle into your Java application, use the
getImage()
method ofjava.awt.Toolkit
. The string you pass intogetImage()
is of the form"NSImage://MyImage"
. Do not include the file extension of the image. Also be aware that the Sun 2D renderer is disabled when the user interface scale factor does not have a value of 1.0. Use the Quartz renderer so that your images scale smoothly.
Being familiar with javax.imageio
, this comes as a complete surprise, as I hadn't known any other way to load other filetypes into images. Especially with an outdated platform and absolutely no support for files such as .tiff
. For example, a quick test on my computer gives me this:
Supported read formats: [jpg, bmp, gif, png, wbmp, jpeg]
Supported write formats: [jpg, bmp, gif, png, wbmp, jpeg]
'JPEG' reader: com.sun.imageio.plugins.jpeg.JPEGImageReader@5e9f23b4
'JPEG' reader: com.sun.imageio.plugins.jpeg.JPEGImageWriter@378fd1ac
I tried loading a simple .tiff
image and tested this out:
static Image n; public static void main(String[] args) { JFrame f = new JFrame(); JPanel p = new JPanel() { @Override public void paintComponent(Graphics graphics) { graphics.drawImage(n, 0, 0, null); } } f.add(p); n = Toolkit.getDefaultToolkit().getImage(("/Users/zinedine/Desktop/test_image.tiff"); f.setVisible(true) }
It yields nothing:
I tried again: This time, adding the image into the base folder of my java project, and typed this in as a string: "NSImage://test_image.tiff"
. Like everything I do, it doesn't work.
However, If I change my fancy path string to an NSImage one, such as "NSImage://NSApplicationIcon"
...
It works. I did a quick spotlight search for NSImage, and found one. It looks like the file type for these images are .png
. This is kind of disturbing, since I expected a proper image to come out of it. Mind you, I also kind of expected it: If it expects arguments of the form "NSImage://something"
, then it might just ignore anything else.
Obviously, I've got a couple questions:
How does the Toolkit load the image? If I try to load a
.tiff
image from my desktop, this is what I get if I call.toString()
:sun.awt.image.ToolkitImage@25f38edc // Also can't be cast to java.awt.BufferedImage
Are the readers (and writers if any) part of a public API? In other words, can I call something to load my
.tiff
file into anImage
(which I can then cast into a `BufferedImage?And then again, if the readers/writers are part of the API, why doesn't the
javax.imageio
package locate them?
This may look like a handful, (yes I'm sorry for ruining your day on this question), but to me, this looks like expected, but at the same time erroneous behaviour. Bonus marks: Is there any friendly (i.e. Open Source) imaging api (Not the JAI) that can process .tiff
files (and others)?