如何在RFT中比较两个图像文件?

如何在RFT中比较两个图像文件?

 

下面脚本截获屏幕图像并保存到文件中,然后比较两个图像文件:

 

    public void testMain(Object[] args)

    {

        CaptureScreen.captureScreen("C://tmp1.jpg");

        startApp("calc");

        计算器window().activate();

        CaptureScreen.captureScreen("C://tmp2.jpg");

        boolean result = compareImages("C://tmp1.jpg","C://tmp2.jpg");

        if(result)

        {

            System.out.println("The two image are same!");

        }

        else

        {

            System.out.println("The two image are different.");

            doImageDiff("C://tmp1.jpg","C://tmp2.jpg","C://tmp3.jpg");

        }

    }

       

 

captureScreen的写法参考:

http://blog.csdn.net/Testing_is_believing/archive/2010/01/23/5247785.aspx

 

 

通过比较像素点的方式比较两个图像文件:

 

 

    public static boolean compareImages(String expectedImage, String actualImage)

    {

        BufferedImage expected = null, actual = null;

        try

        {

            FileInputStream in = new FileInputStream(expectedImage);

            JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);

            expected = decoder.decodeAsBufferedImage();

            in.close();

 

            in = new FileInputStream(actualImage);

            decoder = JPEGCodec.createJPEGDecoder(in);

            actual = decoder.decodeAsBufferedImage();

            in.close();

        }

        catch (Exception e) {

            System.out.println("Error in BitmapOps#compareImages: error reading images: " + e);

            return false;

        }

 

        if (expected == null || actual == null)

            return false;

        if (expected.getHeight() != actual.getHeight() || expected.getWidth() != actual.getWidth())

            return false;

 

        for (int y = 0; y < expected.getHeight(); ++y)

        {

            for (int x = 0; x < expected.getWidth(); ++x)

            {

                if (expected.getRGB(x, y) != actual.getRGB(x, y))

                    return false;

            }

        }

 

        return true;

    }

 

 

分析出两个图像文件不相等的地方,把差异的地方高亮显示并保存到图像文件:    

 

    public static void doImageDiff(String expectedImage, String actualImage, String diffImage)

    {

        BufferedImage expected = null, actual = null;

        try

        {

            FileInputStream in = new FileInputStream(expectedImage);

            JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);

            expected = decoder.decodeAsBufferedImage();

            in.close();

 

            in = new FileInputStream(actualImage);

            decoder = JPEGCodec.createJPEGDecoder(in);

            actual = decoder.decodeAsBufferedImage();

            in.close();

        }

        catch (Exception e) {

            System.out.println("Error in BitmapOps#doImageDiff: error reading images: " + e);

            return;

        }

 

        try {

            BufferedImage difference = new BufferedImage(expected.getWidth(), expected.getHeight(), expected.getType());

 

            if (expected == null || actual == null)

            {

                System.out.println("Error in BitmapOps#doImageDiff: Expected image or actual image is null");

                return;

            }

            if (expected.getHeight() != actual.getHeight() || expected.getWidth() != actual.getWidth())

            {

                System.out.println("Error in BitmapOps#doImageDiff: Images are not the same size");

                return;

            }

 

            for (int y = 0; y < expected.getHeight(); ++y)

            {

                for (int x = 0; x < expected.getWidth(); ++x)

                {

                    int rgb = expected.getRGB(x, y) - actual.getRGB(x, y);

                    difference.setRGB(x, y, rgb);

                }

            }

 

            FileOutputStream out = new FileOutputStream(diffImage);

            JPEGImageEncoder encoder =

            JPEGCodec.createJPEGEncoder(out);

            encoder.encode(difference);

            out.flush();

            out.close();

        }

        catch (Exception e) {

            System.out.println("Error in BitmapOps#doImageDiff: error writing image: " + e);

        }

    }

 

你可能感兴趣的:(如何在RFT中比较两个图像文件?)