18

I have a problem with Java application, particular in loading a image from a location in my computer.

Following this post I used a BufferedImage and a InputFileStream to load an image on my computer. First, I put the image (pic2.jpg) into the source code and that is working. However, if I put the image to another place (let's say C:\\ImageTest\pic2.jpg), Java IDE show me an IllegalArgumentException

return ImageIO.read(in);

here is the code:

public class MiddlePanel extends JPanel {
    private BufferedImage img;

    public MiddlePanel(int width) {    
        //img = getImage("pic2.jpg");       
        img = getImage("C:\\ImageTest\\pic2.jpg");

        this.setPreferredSize(new Dimension(800,460));

    }

    public void paintComponent(Graphics g) {
        // ...
    }

    private BufferedImage getImage(String filename) {
        // This time, you can use an InputStream to load
        try {
            // Grab the InputStream for the image.                    
            InputStream in = getClass().getResourceAsStream(filename);

            // Then read it.
            return ImageIO.read(in);
        } catch (IOException e) {
            System.out.println("The image was not loaded.");
            //System.exit(1);
        }

        return null;
    }
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Lup
  • 197
  • 1
  • 1
  • 7
  • 1
    Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230), hope it helps :-) – nIcE cOw Oct 18 '13 at 10:25

5 Answers5

26

To read an .jpg file from non-relative path you could use this:

BufferedImage img = null;

try 
{
    img = ImageIO.read(new File("C:/ImageTest/pic2.jpg")); // eventually C:\\ImageTest\\pic2.jpg
} 
catch (IOException e) 
{
    e.printStackTrace();
}

I do not have any Java environment at the moment, so hope it works and is written correctly.

Tafari
  • 2,639
  • 4
  • 20
  • 28
  • @Andrew Thompson Yeah it was needed, even more when writing this without any java environment. – Tafari Oct 18 '13 at 10:48
7

getResource & getResourceAsStream do not work with file paths, but paths relative the code base. If the code base is C: then a relative path that would locate the resource is /ImageTest/pic2.jpg.

..difference between load file by FileInputStream and getResourceAsStream?

One major difference is that the getResource.. will work with a resource inside a Jar, which is no longer a File. Therefore FileInputStream cannot be used to access such a resource.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Ya, i use FileInputStream to load file path instead and it works :D , thank you so much :D. Could you please tell me the difference between load file by FileInputStream and getResourceAsStream? – Lup Oct 18 '13 at 10:26
4

You cannot use Class#getResource(String) or Class#getResourceAsStream(String) in this case. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String).

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a / (\u002f), then the absolute name of the resource is the portion of the name following the /. Otherwise, the absolute name is of the following form: modified_package_name/name

Where the modified_package_name is the package name of this object with / substituted for . (\u002e).

Generally, it is not a good thing to hard code the system location of your resources in your code. The neat and clean way is to put your resources in your classpath and access them. Hope this clarifies why it's not working

Neuron
  • 5,141
  • 5
  • 38
  • 59
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
1
//This code snippet read an image from location on the computer and writes it to a different location on the disk
try {
    byte[] imageInByte;

    BufferedImage imageOnDisk = ImageIO.read(new File("C:\\ImageTest\\pic2.jpg"));

    //Create a ByteArrayOutputStrea object to write image to
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    //Write the image to the OutputStream
    ImageIO.write(imageOnDisk, "jpg", baos);

    baos.flush();

    //Initialise the byte array object with the image that was written to the OutputStream
    imageInByte = baos.toByteArray();
    baos.close();

    // convert byte array back to BufferedImage
    InputStream in = new ByteArrayInputStream(imageInByte);
    BufferedImage bImageFromConvert = ImageIO.read(in);

    //write the image to a new location with a different file name(optionally)
    ImageIO.write(bImageFromConvert, "jpg", new File(
            "c:\\index.jpg"));
} catch (IOException e) {
    e.printStackTrace();
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Charles
  • 320
  • 2
  • 8
-1

To find the image Width, height and size

BufferedImage image = null;    
int imageWidth = -1;
int imageHeight = -1;
int fileSize = -1;
try {
    File imageFile = new File(imagePath);
    int fileSize = (int) imageFile.length();
    image = ImageIO.read(imageFile); // Reading the Image from the file system
    imageWidth = image.getWidth();
    imageHeight = image.getHeight();
} catch (IOException e) {
    e.printStackTrace();
}
Neuron
  • 5,141
  • 5
  • 38
  • 59