-
Reading in an image file
I am trying to read in a jpeg image to a BufferedImage and I am getting an error, "Can't read input file". Here is my code:
Code:
package StereoCorrespondence;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
*
* @author bheussler
*/
public class StereoCorrespondence {
public StereoCorrespondence() throws IOException {
StereoCorrespondence.loadImage("images/mapleft.jpeg");
}
public static BufferedImage loadImage(String ref) throws IOException {
BufferedImage bimg = null;
bimg = ImageIO.read(new File(ref));
System.out.println(bimg.getHeight());
return bimg;
}
public static void main(String[]args) throws IOException {
StereoCorrespondence stereoCorrespondence = new StereoCorrespondence();
}
}
-
What's the full exception you get?
Including stack trace.
-
check the image path u have specified.Make sure its under package images
-
Fix!?!
I got it to work. Here is the code:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package StereoCorrespondence;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
*
* @author bheussler
*/
public class StereoCorrespondence {
public StereoCorrespondence() throws IOException {
BufferedImage mapleft = StereoCorrespondence.loadImage("/Users/bheussler/NetBeansProjects/StereoCorrespondence/src/StereoCorrespondence/images/mapleft.jpeg");
BufferedImage mapright = StereoCorrespondence.loadImage("/Users/bheussler/NetBeansProjects/StereoCorrespondence/src/StereoCorrespondence/images/mapright.jpeg");
BufferedImage conesleft = StereoCorrespondence.loadImage("/Users/bheussler/NetBeansProjects/StereoCorrespondence/src/StereoCorrespondence/images/conesleft.jpeg");
BufferedImage conesright = StereoCorrespondence.loadImage("/Users/bheussler/NetBeansProjects/StereoCorrespondence/src/StereoCorrespondence/images/conesright.jpeg");
BufferedImage teddyleft = StereoCorrespondence.loadImage("/Users/bheussler/NetBeansProjects/StereoCorrespondence/src/StereoCorrespondence/images/teddyleft.jpeg");
BufferedImage teddyright = StereoCorrespondence.loadImage("/Users/bheussler/NetBeansProjects/StereoCorrespondence/src/StereoCorrespondence/images/teddyright.jpeg");
System.out.print(mapleft.getHeight());
}
public static BufferedImage loadImage(String ref) throws IOException {
BufferedImage bimg = null;
bimg = ImageIO.read(new File(ref));
return bimg;
}
public static void main(String[]args) throws IOException {
StereoCorrespondence stereoCorrespondence = new StereoCorrespondence();
}
}
The only issue is that I have to use the full path location for the images, rather than just referencing them from the local "images" directory.
-
<class>.getResourceAsStream().
-
This is the line that I try:
Code:
BufferedImage mapleft = StereoCorrespondence.loadImage(getClass().getResourceAsStream("images/mapleft.jpeg"));
It doesn't work and says, "Uncompilable source code"
-
It's the call to ImageIO.read that should have the getResourceAsStream. Unless you've chnged the signature to the loadImage() method to take an InputStream instead of a String.
By the way, I'm sure there's more detail to your problem than simply "Uncompilable source code".
-
I haven't changed the loadImage(). It still takes a string. So what you are saying is that my code should look like:
Code:
BufferedImage mapleft = StereoCorrespondence.loadImage(ImageIO.read(getResourceAsStream("http://www.java-forums.org/images/mapleft.jpeg")));
?
ps, i dont know why the code won't display
-
No no no no no no no no!
(and a couple more for luck ;))
Your loadImage() method has a String parameter.
Your call from a couple of posts up was passing an InputStream into it...hence (at a guess, since you haven't actually said what the error was) your compilation error.
I suggested passing a String in as you were originally, and changing the loadImage() code so it did ImageIO.read(getResourceAsStream(etc etct)), rather than the read(filename) that it is currently doing.