View RSS Feed

Java AWT

Reading/Loading an Image

Rate this Entry
by , 03-07-2012 at 04:59 PM (972 Views)
Sampled images formats come into mind when we think about digital images. Like GIF images thare used at web pages or JPEG images that are commonly used in digital photography. Program using them shall 1st convert them into an internal format from an external format.

Java 2Dô provides support to load such external image format to BufferedImage format. This is done by using Image I/O API that is present in javax.imageio package. Built in support is present in Image I/O for GIF,JPEG,WBMP, PNG and BMP.It is possible to make imageI/O extensible therefore administrators and developers could plu-in support, for the additional formats. E.g. plug-in are separately present for JPEG & TIFF.

Following code shall be used for loading image, from one specified file.
Java Code:
BufferedImage img = null;
try {
    img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
}
As JPEG format, file contents are recognized by Image I/O which then decodes it to BufferedImage which might be used directly by Java 2D.

LoadImageApp.java tell that how image shall be displayed.

If code run in an applet this means that it is an easy task to get the images, from the applet codebase :
Java Code:
try {
   URL url = new URL(getCodeBase(), "strawberry.jpg");
   img = ImageIO.read(url);
} catch (IOException e) {
}

Submit "Reading/Loading an Image" to Facebook Submit "Reading/Loading an Image" to Digg Submit "Reading/Loading an Image" to del.icio.us Submit "Reading/Loading an Image" to StumbleUpon Submit "Reading/Loading an Image" to Google

Comments