Skip to main content

JAI vs. ImageIO

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
2 replies [Last post]
bruehlicke
Offline
Joined: 2005-04-18

Hi all,
I just found an excellent example to view a long PNG using DisplayJAI in a scrollpane. This just uses classes from the jai_codec.jar and jai_core.jar and no native code needed so still pure java. VERY IMPRESSIVE faster than my current implementation.

Now, has any of you a link to some FAQ's/comparison table which helps me to answer the question:

What can you do with the basic ImageIO package in JDK 6 versus when do you need to go to JAI ?

The reason is of course some layzness from my side (I admit) as I now have a working test app which does what I want but I cannot get my head around if I could do something similar (aka read long PNG into a JScollPane other than the naive solution of creating an ImageIcon and stick it into a JLabel) with the ImageIO stuff shipped with JDK 6 ? I have found code which do split a PNG into chunks and I could do my own caching by that, but it seems rather cumbersome and I wonder if all are as lazy as I am and just jump using JAI just because of its already simple build in caching ?
Happy new year to all of you
B-)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bruehlicke
Offline
Joined: 2005-04-18

as an add-on I am writing on this forum as the PNG's I am trying to render are not a picture of my grandmother but typically 20-80 Mb in size with typically over 150,000 pixels in height.

So - on eway to read those guys simply using JDK 6 is

String filename = "images/myimage.png";
FileInputStream inputStream = new FileInputStream(filename);
String extensionName = filename.substring(filename.lastIndexOf('.') + 1);
Iterator readers = ImageIO.getImageReadersBySuffix(extensionName);
ImageReader imageReader = (ImageReader) readers.next();
ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream);
imageReader.setInput(imageInputStream, true);

BufferedImage image = imageReader.read(0);

But now the question arrives : How to view those ? Are the only 2 current ways if I only use the JDK to
a) Make my own JPanel and override paint
b) Stick the BufferedImage into a ImageIcon and than setImage on a JLabel
?

So the only reason I am trying JAI is that using DisplayJAI has turned out to be phantastic fast ....

bruehlicke
Offline
Joined: 2005-04-18

After some research in the jai_core code I found that you actually can use most of the above and only need a NullOpImage to gain access to the JAI build in caching. So the following code works fine.

FileInputStream inputStream = new FileInputStream(filename);
String extensionName = filename.substring(filename.lastIndexOf('.') + 1);
Iterator readers = ImageIO.getImageReadersBySuffix(extensionName);
ImageReader imageReader = (ImageReader) readers.next();
ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream);
imageReader.setInput(imageInputStream, true);
RenderedImage op = new NullOpImage(imageReader.readAsRenderedImage(imageToLoad, null), null, null, 2);
JPanel displayPanel = new DisplayJAI(op);

Voila ! Using most standard JDK for the readers and only the JAI NullOpImage to gain the caching and the DisplayJAI to wrap the RenderedImage to a JPanel - very nice.
One could replace the RenderedImage line with standard JDK code like
RenderedImage op = imageReader.readAsRenderedImage(imageToLoad, null);
but that will bring it back to the poor performance as no cache is applied.