如何在RFT中比較兩個影像檔案?

TIB發表於2010-01-23

如何在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);

        }

    }

 

相關文章