简体   繁体   中英

reading and storing a .bmp file in java

I'm am trying to read a .bmp file called circle1.bmp . It is in a package that I have imported into the following file.

So far I have the following code, but when I run the following code I get:

javax.imageio.llOException: Can't read input file!

public void setUp() throws IOException
{
    BufferedImage image = ImageIO.read(new File("circle1.bmp"));
    byte[][] greenInputData = new byte[30][40];

    for (int x = 0; x < inputData.length; x++)
    {
        for (int y = 0; y < inputData[x].length; y++)
        {
            int color = image.getRGB(x, y);
            //alpha[x][y] = (byte)(color>>24);
            //red[x][y] = (byte)(color>>16);
            greenInputData[x][y] = (byte)(color>>8);
            //blue[x][y] = (byte)(color);
        }
    }
    this.inputData = greenInputData;

    System.out.println(this.inputData);
}

你应该尝试像

image = ImageIO.read(getClass().getResourceAsStream("path/to/your/file.bmp"));

Likely your image's file path isn't correct relative to the user directory. To find out where Java is starting to look, where the user directory is, place something like this line of code somewhere in your program:

System.out.println(System.getProperty("user.dir"));

Perhaps you'd be better off getting the image as an InputStream obtained from a resource and not as a file. eg,

image = ImageIO.read(getClass().getResourceAsStream("circle1.bmp")); //prefered

or

image = ImageIO.read(getClass().getResource("circle1.bmp"));

This will look for the image at the path given relative to the location of the class files, and in fact this is what you must do if your image is located in your jar file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM