TechieDrill Your World Of Technical Tutorials

Reading image files from Java

01.05.2010 · Posted in Java

Here is the code to read a image file from java. Image file can be read even if it is located inside a compressed JAR file or even inside a folder.  imageIO class from javax is the main element of this functionality.

Here is the code

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ReadingImage
{
public void getImage()
{
try
{
BufferedImage image =
ImageIO.read(getClass().getResource(”Sunset.jpg”));
// Do something with the image.
} catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ReadingImage ri = new ReadingImage();
ri.getImage();
}
}

import javax.imageio.ImageIO;

import java.awt.image.*;

import java.io.IOException;

import java.awt.image.BufferedImage;

public class ReadImageDemo

{

public void displayImage()

{

try

{

BufferedImage imageSrc =

ImageIO.read(getClass().getResource(”Corsa.jpg”));

// Do whatever you wanna do with this image.

} catch (IOException exception)

{

exception.printStackTrace();

}

}

public static void main(String argum[])

{

ReadImageDemo readImageDemo  = new ReadImageDemo();

readImageDemo. displayImage();

}

}

Leave a Reply

You must be logged in to post a comment.