search

Home  >  Q&A  >  body text

为什么同一段代码在IDEA和Eclipse之中运行的结果不一样?

public class ToGray {

    /*二值化*/
    public void binaryImage() throws IOException {
        File file = new File("image/rabbit.jpeg");
        BufferedImage image = ImageIO.read(file);

        int width = image.getWidth();
        int height = image.getHeight();

        BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);// 重点,技巧在这个参数BufferedImage.TYPE_BYTE_BINARY
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int rgb = image.getRGB(i, j);
                grayImage.setRGB(i, j, rgb);
            }
        }

        File newFile = new File("image/binary_rabbit");
        ImageIO.write(grayImage, "jpg", newFile);
    }

    /*灰度图片*/
    public void grayImage() throws IOException {
        File file = new File("image/rabbit.jpeg");
        BufferedImage image = ImageIO.read(file);

        int width = image.getWidth();
        int height = image.getHeight();

        BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);// 重点,技巧在这个参数BufferedImage.TYPE_BYTE_GRAY
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int rgb = image.getRGB(i, j);
                grayImage.setRGB(i, j, rgb);
            }
        }

        File newFile = new File("image/ggray_rabbit.jpg");
        ImageIO.write(grayImage, "jpg", newFile);
    }

    public static void main(String[] args) throws IOException {
        ToGray demo = new ToGray();
        demo.binaryImage();
        demo.grayImage();
        System.out.println("hello image!");
    }

}

在Eclipse之下可以正常通过,但是在IDEA下面会出现无法读取的错误,具体代码如下:

Exception in thread "main" javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at basicoperation.ToGray.grayImage(ToGray.java:70)
    at basicoperation.ToGray.main(ToGray.java:93)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Process finished with exit code 1

请问这是怎么回事啊?

阿神阿神2765 days ago1039

reply all(4)I'll reply

  • 阿神

    阿神2017-04-24 09:15:34

    It seems to be a problem with the path. I didn’t read the picture.

    Is there a picture of a rabbit here? In the current code, the image should be in the same directory as the generated program.

    reply
    0
  • PHPz

    PHPz2017-04-24 09:15:34

    The relative path of idea is relative to the project root directory, not relative to the output root directory.

    reply
    0
  • 阿神

    阿神2017-04-24 09:15:34

    The file was not read. Your image file is not in the packaged directory. Idea should be in the out or build directory.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-24 09:15:34

    @oriental star mark @fabricated belief ,

    The first picture is the folder situation in Eclipse, and the second picture is the folder situation in IDEA.
    But in IDEA, change it to BufferedImage image = ImageIO.read(this.getClass().getResource((path))); and the compilation will pass. I feel it is a classpath problem or a path problem, but it is not specific. not sure.

    ------Update------
    The problem has been solved, The reason is that under IDEA, the relative path defaults to the Project path or Module path, so if you either put the images folder and In a directory at the same level as the .idea folder, or in a deeper folder, the high-level directory where the images folder is located must be reflected when creating a file or getting a path, so that There will no longer be a situation where the file cannot be read.

    reply
    0
  • Cancelreply