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

查看:453
本文介绍了为什么同一段代码在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

请问这是怎么回事啊?

解决方案

看上去就是路径的问题啊,没读到图片。

这里有兔子那张图片吗。现在的代码,image应该跟生成的程序同级目录。

这篇关于为什么同一段代码在IDEA和Eclipse之中运行的结果不一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆