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

时间:2017-02-13 15:49:30

标签: java selenium firefox

我正在尝试使用我在Google Code中存档的一些代码来比较两个图像。当我尝试运行它时,我收到以下错误:

起初我正在传递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)

以下是我正在使用的代码:

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");
    }

VisualComparison.Java:

 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;
        }


    }

0 个答案:

没有答案