Java has a great efficient Image handling API in javax.imageio package. Using this package one can do all important image related operations, like Creating Images, Get an ImageData instance from an existing Image, save the image to the local disk etc. To do these operations first we need to scan for the plug-in for the related image type.
Today, we have a variety of Image formats all for their some specific usage. To read or write these image formats we need their specific codec to encode or decode image files, the com.sun.image.codec.*, packages are not part of the core Java APIs, they are a part of Sun’s JDK and JRE distributions, after encoding/decoding we use imageReader and ImageWriter objects which are the parts of the ImageIO package for read/write operations.
Generally we use these codecs to read complex image formats. To get these image data we need reader for the images which decodes all the registered image type that currently registered ImageReaders that claims to be able to decode the named format.
The initial part of the Imaging starts from Image class. In Java AWT package which have Image and BufferedImage classes which uses more often in Java Imaging. Others are ImageIcon etc. AWT package is responsible for the getting and setting sample models, rasters, color models, imaging inerface and image data clsass, these all needed in image processing. Other than it, the Graphics class is used to display the image on the screen
If we analyze images processing precisely we found that there are main three stage starting from source image then processing operation and at the end the resultant destination image object.
We can get a BufferedImage from a specific file using the following code:
BufferedImage img = ImageIO.read(new File("c:\\example.jpg")); // here ImageIO is used to read the image from the file at location "c:\example.jpg" or get an Image object using code : Image image = new Image(display, "exampleFile.gif"); // where display is the object of class Display getting by Display display = new Display();
To display the image on screen we use Graphics of the container (defined in AWT)class, Let the container would be frame then,
Graphics2D g = (Graphics2D) frame.getRootPane().getGraphics(); //Get the surfaces Graphics object g.drawImage(img, null, 0, 0); // Now draw the image at 0,0 on the frame. After some operations on image we can save the image in the local disk using the code : ImageIO.write (img, "png", new File(“C:\\resultImage”+ ".png"));
// The three write method’s parameters are :
img –
a
RenderedImage
to be written.
“png” – The FormatName,
a
String contains
the informal name of the format.
Output –
a
File
to be written to.