Java ImageIO错误:无法读取输入文件

Java ImageIO错误:无法读取输入文件,java,selenium,firefox,Java,Selenium,Firefox,我正在尝试使用我在谷歌代码中找到的一些存档代码来比较两个图像。当我尝试运行它时,会出现以下错误: public static void compareScreenshots() throws IOException { VisualComparison cti = new VisualComparison("C:/workspace/Screenshots/Contact Information Capital One Canada/baseline.jpg", "C:/workspace/S

我正在尝试使用我在谷歌代码中找到的一些存档代码来比较两个图像。当我尝试运行它时,会出现以下错误:

public static void compareScreenshots() throws IOException {
 VisualComparison cti = new VisualComparison("C:/workspace/Screenshots/Contact Information Capital One Canada/baseline.jpg", "C:/workspace/Screenshots/Contact Information Capital One Canada/compare.jpg" );
    cti.setParameters(10, 10);
    cti.compare();
    if (!cti.isIdentic()) {
        System.out.println("no match");
        VisualComparison.saveJPG(cti.getImageResult(), "C:/workspace/Screenshots/Contact Information Capital One Canada/diff.jpg");
    } else {
        System.out.println("match");
    }
 import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;


    public class VisualComparison {
        private static BufferedImage image1, image2, imageResult;
        private static boolean isIdentic;
        // a number of pieces along vertical and horizontal
        private static int compareX, compareY;

        // �tolerance� comparison parameter that allows to treat similar colors as the same
        private static double sensitivity = 1.00;

        public VisualComparison(String file1, String file2) throws IOException {
            image1 = loadJPG(file1);
            image2 = loadJPG(file2);
        }

        // set a number of pieces along vertical and horizontal
        public void setParameters(int compareX, int compareY) {
            VisualComparison.compareX = compareX;
            VisualComparison.compareY = compareY;
        }

        // compare the two images in this object.
        public void compare() {
            // setup change display image
            imageResult = new BufferedImage(image2.getWidth(null), image2.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = imageResult.createGraphics();
            g2.drawImage(image2, null, null);
            g2.setColor(Color.RED);
            // assign size of each section
            int blocksX = (int)(image1.getWidth()/compareX);
            int blocksY = (int)(image1.getHeight()/compareY);
            VisualComparison.isIdentic = true;
            for (int y = 0; y < compareY; y++) {
                for (int x = 0; x < compareX; x++) {
                    int result1 [][] = convertTo2D(image1.getSubimage(x*blocksX, y*blocksY, blocksX - 1, blocksY - 1));
                    int result2 [][] = convertTo2D(image2.getSubimage(x*blocksX, y*blocksY, blocksX - 1, blocksY - 1));
                    for (int i = 0; i < result1.length; i++) {
                        for (int j = 0; j < result1[0].length; j++) {
                            int diff = Math.abs(result1[i][j] - result2[i][j]);
                            if (diff/Math.abs(result1[i][j]) > sensitivity) {
                                // draw an indicator on the change image to show where change was detected.
                                g2.fillRect(x*blocksX, y*blocksY, blocksX - 1, blocksY - 1);
                                isIdentic = false;
                            }
                        }
                    }
                }
            }
        }

        public BufferedImage getImageResult() {
            return imageResult;
        }

        // representation fragment's of the picture in a two-dimensional integer array
        public int[][] convertTo2D(BufferedImage subimage) {
            int width = subimage.getWidth();
            int height = subimage.getHeight();
            int[][] result = new int[height][width];

            for (int row = 0; row < height; row++) {
                for (int col = 0; col < width; col++) {
                    result[row][col] = subimage.getRGB(col, row);
                }
            }
            return result;
        }

        // reading picture from disk
        public static BufferedImage loadJPG(String filename) throws IOException {
            BufferedImage img = null;
            try {
                img = ImageIO.read(new File(filename));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return img;

        }

        // writing picture into disk
        public static void saveJPG(BufferedImage bimg, String filename) {
            try {
                File outputfile = new File(filename);
                ImageIO.write(bimg, "jpg", outputfile);
            } catch(IOException e) {
                e.printStackTrace();
            }
        }

        public boolean isIdentic() {
            return isIdentic;
        }


    }
起初,我正在传递PNG文件,我认为将文件更改为JPG可以解决问题,但在测试了这个理论之后,它就不起作用了

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at mjs.textparser.VisualComparison.loadJPG(VisualComparison.java:82)
    at mjs.textparser.VisualComparison.<init>(VisualComparison.java:21)
    at mjs.textparser.App.compareScreenshots(App.java:89)
    at mjs.textparser.App.VisionTest(App.java:26)
    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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at mjs.textparser.VisualComparison.loadJPG(VisualComparison.java:82)
    at mjs.textparser.VisualComparison.<init>(VisualComparison.java:22)
    at mjs.textparser.App.compareScreenshots(App.java:89)
    at mjs.textparser.App.VisionTest(App.java:26)
    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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
VisualComparison.Java:

public static void compareScreenshots() throws IOException {
 VisualComparison cti = new VisualComparison("C:/workspace/Screenshots/Contact Information Capital One Canada/baseline.jpg", "C:/workspace/Screenshots/Contact Information Capital One Canada/compare.jpg" );
    cti.setParameters(10, 10);
    cti.compare();
    if (!cti.isIdentic()) {
        System.out.println("no match");
        VisualComparison.saveJPG(cti.getImageResult(), "C:/workspace/Screenshots/Contact Information Capital One Canada/diff.jpg");
    } else {
        System.out.println("match");
    }
 import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;


    public class VisualComparison {
        private static BufferedImage image1, image2, imageResult;
        private static boolean isIdentic;
        // a number of pieces along vertical and horizontal
        private static int compareX, compareY;

        // �tolerance� comparison parameter that allows to treat similar colors as the same
        private static double sensitivity = 1.00;

        public VisualComparison(String file1, String file2) throws IOException {
            image1 = loadJPG(file1);
            image2 = loadJPG(file2);
        }

        // set a number of pieces along vertical and horizontal
        public void setParameters(int compareX, int compareY) {
            VisualComparison.compareX = compareX;
            VisualComparison.compareY = compareY;
        }

        // compare the two images in this object.
        public void compare() {
            // setup change display image
            imageResult = new BufferedImage(image2.getWidth(null), image2.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = imageResult.createGraphics();
            g2.drawImage(image2, null, null);
            g2.setColor(Color.RED);
            // assign size of each section
            int blocksX = (int)(image1.getWidth()/compareX);
            int blocksY = (int)(image1.getHeight()/compareY);
            VisualComparison.isIdentic = true;
            for (int y = 0; y < compareY; y++) {
                for (int x = 0; x < compareX; x++) {
                    int result1 [][] = convertTo2D(image1.getSubimage(x*blocksX, y*blocksY, blocksX - 1, blocksY - 1));
                    int result2 [][] = convertTo2D(image2.getSubimage(x*blocksX, y*blocksY, blocksX - 1, blocksY - 1));
                    for (int i = 0; i < result1.length; i++) {
                        for (int j = 0; j < result1[0].length; j++) {
                            int diff = Math.abs(result1[i][j] - result2[i][j]);
                            if (diff/Math.abs(result1[i][j]) > sensitivity) {
                                // draw an indicator on the change image to show where change was detected.
                                g2.fillRect(x*blocksX, y*blocksY, blocksX - 1, blocksY - 1);
                                isIdentic = false;
                            }
                        }
                    }
                }
            }
        }

        public BufferedImage getImageResult() {
            return imageResult;
        }

        // representation fragment's of the picture in a two-dimensional integer array
        public int[][] convertTo2D(BufferedImage subimage) {
            int width = subimage.getWidth();
            int height = subimage.getHeight();
            int[][] result = new int[height][width];

            for (int row = 0; row < height; row++) {
                for (int col = 0; col < width; col++) {
                    result[row][col] = subimage.getRGB(col, row);
                }
            }
            return result;
        }

        // reading picture from disk
        public static BufferedImage loadJPG(String filename) throws IOException {
            BufferedImage img = null;
            try {
                img = ImageIO.read(new File(filename));
            } catch (IOException e) {
                e.printStackTrace();
            }
            return img;

        }

        // writing picture into disk
        public static void saveJPG(BufferedImage bimg, String filename) {
            try {
                File outputfile = new File(filename);
                ImageIO.write(bimg, "jpg", outputfile);
            } catch(IOException e) {
                e.printStackTrace();
            }
        }

        public boolean isIdentic() {
            return isIdentic;
        }


    }
导入java.awt.Color;
导入java.awt.Graphics2D;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
公共类可视化比较{
专用静态缓冲区image1、image2、imageResult;
私有静态布尔值;
//沿垂直和水平方向的若干块
私有静态int compareX,compareY;
//ëë½公差ëë½比较参数,允许将相似颜色视为相同颜色
专用静态双灵敏度=1.00;
公共VisualComparison(字符串文件1、字符串文件2)引发IOException{
image1=loadJPG(文件1);
image2=loadJPG(file2);
}
//沿垂直和水平方向设置若干块
公共void setParameters(int-compareX,int-compareY){
VisualComparison.compareX=compareX;
VisualComparison.compareY=compareY;
}
//比较此对象中的两个图像。
公共空间比较(){
//设置更改显示图像
imageResult=new BuffereImage(image2.getWidth(null)、image2.getHeight(null)、BuffereImage.TYPE_INT_RGB);
Graphics2D g2=imageResult.createGraphics();
g2.drawImage(图像2,空,空);
g2.设置颜色(颜色为红色);
//指定每个部分的大小
int blocksX=(int)(image1.getWidth()/compareX);
int blocksY=(int)(image1.getHeight()/compareY);
VisualComparison.isIdentic=true;
对于(int y=0;y灵敏度){
//在更改图像上绘制一个指示器,以显示检测到更改的位置。
g2.fillRect(x*blocksX,y*blocksY,blocksX-1,blocksY-1);
isIdentic=假;
}
}
}
}
}
}
公共缓冲区图像getImageResult(){
返回图像结果;
}
//二维整数数组中图片的表示片段
公共int[][]转换为2D(BuffereImage子映像){
int width=subimage.getWidth();
int height=subimage.getHeight();
int[][]结果=新的int[高度][宽度];
对于(int row=0;row
尝试转义文件名中的空格(即
\\
)。另外,转义斜杠位于文件夹名称之间。@RC。感谢您的快速回复,我尝试了两种文件名方法。事实上,我以前就有过这种情况,并将其切换到
/
,看看这是否是问题所在。@Alfabravo文件夹正确分开了,我使用selenium将文件夹名称基于从网页中获取的页面标题。尝试在文件名中转义空格(即
\\
)。此外,在文件夹名称之间转义斜杠。@RC。感谢您的快速回复,我尝试了两种文件名方法。事实上,我以前就是这样做的,我把它切换到
/
,看看这是否是问题所在。@Alfabravo文件夹正确地分开了。我使用selenium将文件夹名称基于从网页中获取的页面标题。