Results 1 to 9 of 9
  1. #1
    meestafa is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default 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:

    Java 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();
    
        }
    
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    What's the full exception you get?
    Including stack trace.

  3. #3
    RockoAndy is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    check the image path u have specified.Make sure its under package images

  4. #4
    meestafa is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default Fix!?!

    I got it to work. Here is the code:

    Java 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.
    Last edited by meestafa; 11-17-2010 at 03:11 PM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    <class>.getResourceAsStream().

  6. #6
    meestafa is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    This is the line that I try:

    Java Code:
     BufferedImage mapleft = StereoCorrespondence.loadImage(getClass().getResourceAsStream("images/mapleft.jpeg"));
    It doesn't work and says, "Uncompilable source code"

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    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".

  8. #8
    meestafa is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    I haven't changed the loadImage(). It still takes a string. So what you are saying is that my code should look like:


    Java 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
    Last edited by meestafa; 11-17-2010 at 05:12 PM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    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.

Similar Threads

  1. Replies: 9
    Last Post: 10-20-2009, 10:52 AM
  2. Replies: 3
    Last Post: 05-09-2009, 11:31 PM
  3. Image reading from URL problem
    By paras in forum Advanced Java
    Replies: 4
    Last Post: 04-18-2009, 07:26 AM
  4. [SOLVED] Image reading from URL problem
    By paras in forum New To Java
    Replies: 3
    Last Post: 04-18-2009, 05:11 AM
  5. To open an image file such as Jpeg file using JAva Program
    By itmani2020 in forum Advanced Java
    Replies: 10
    Last Post: 07-11-2008, 09:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •