Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between the ways to read an Image file in Java?

There are various ways of reading an image file in java such as BufferedImage and ImageIcon to name a few. I want to know what is the difference between these cases? Are they context dependent that in a particular case only one of them can be used?

What would be the best way of reading a image selected by JFileChooser by the user and separating the color channels of an image?

like image 336
Deepak kumar Jha Avatar asked Jan 03 '13 09:01

Deepak kumar Jha


People also ask

What is the difference between image and BufferedImage in Java?

Image is an abstract class. You can't instantiate Image directly. BufferedImage is a descendant, and you can instantiate that one. So, if you understand abstract classes and inheritance, you'll understand when to use each.

Can Java read JPEG?

Java 2D supports loading these external image formats into its BufferedImage format using its Image I/O API which is in the javax. imageio package. Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP.

What is Imageio read?

Imageio is a Python library that provides an easy interface to read and write a wide range of image data, including animated images, volumetric data, and scientific formats. It is cross-platform, runs on Python 3.5+, and is easy to install. Main website: https://imageio.readthedocs.io/


2 Answers

A good way is to use the different ImageIO.read methods, which return BufferedImage objects.

Image is an abstract class, so I think the real question is which subclass is more efficient for your program. Use VolatileImage if you need hardware acceleration. More on that here.

like image 146
Alexis Dufrenoy Avatar answered Sep 17 '22 15:09

Alexis Dufrenoy


ImageIcon (and Toolkit#createImage/Toolkit#getImage) use a background loading process. That is, after you call these methods, they will return immediately, having created a background thread to actually load the image data.

These were/are used when loading large images across slow connections, like ye old 28k modems (ah, how I remember the days). This means that your application can continue running while the images are been downloaded.

You'll find in the Graphics class the drawImage methods accept an ImageObserver interface and that java.awt.Component implements this interface, this allows components the ability to automatically update themselves once the image has actually finished loading.

ImageIO on the other hand will not return until the image is fully loaded. It also makes it easier to introduce new readers/writers, making the API far more flexible then the original API. ImageIO also supports a wider range of images out of the box.

BufferedImage is also a far more flexible image class, especially when it comes to apply effects to the image.

Now, I, personally, prefer ImageIO. If I know I'm loading large images or images over a potentially slow connection, I will create my own background thread to load them. While a little more complicated, the trade offs greatly out weight the small amount of extra work -IMHO

What would be the best way of reading a image selected by JFileChooser by the user and separating the color channels of an image?

ImageIO without a doubt. In order to do any serious manipulation of an image loaded using something ImageIcon, you'd have to convert that image to a BufferedImage anyway

like image 22
MadProgrammer Avatar answered Sep 16 '22 15:09

MadProgrammer