在RFT中如何擷取螢幕影像並儲存到檔案中?

TIB發表於2010-01-23

在RFT中,如何擷取螢幕影像並儲存到檔案中?下面的指令碼實現了3種型別的影像擷取,包括擷取整個螢幕的影像、擷取指定區域的影像、擷取某個測試物件的影像:

 

    public void testMain(Object[] args)

    {

        // TODO 在此插入程式碼

        captureScreen("C://temp1.jpg");

        captureScreen("C://temp2.jpg",100,100,100,100);

       

        startApp("calc");

        計算器window().waitForExistence();

        計算器window().activate();

        captureScreen("C://temp3.jpg",(TestObject)計算器window());

    }

   

擷取整個螢幕的影像:

 

    public static void captureScreen(String filename)

    {

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        int width = screenSize.width;

        int height = screenSize.height;

        doScreenCapture(filename, 0, 0, width, height);

    }

   

擷取指定的區域影像:

    public static void captureScreen(String filename, int x, int y, int width, int height)

    {

        doScreenCapture (filename, x, y, width, height);

    }

 

擷取指定測試物件的影像:

    public static void captureScreen(String filename, TestObject to)

    {

        Rectangle r = null;

        //html

        if (to.getProperties().containsKey(".bounds"))

            r = (Rectangle)to.getProperty(".bounds");

        //win

        else if (to.getProperties().containsKey(".screenRectangle"))

            r = (Rectangle)to.getProperty(".screenRectangle");

        //swing

        else if (to.getProperties().containsKey("bounds"))

        {

            r = (Rectangle)to.getProperty("bounds");           

    

            java.awt.Point point = null;

            if (to.getProperties().containsKey("location"))  //swt

                point = (Point)to.getProperty("location");

            else

                point = (Point)to.getProperty("locationOnScreen");

 

            if (point != null)

                r.setLocation(point);

        }

        else

        {

            System.out.println("Error in captureScreen: could not capture test object");

            return;

        }

        doScreenCapture(filename, r.x, r.y, r.width, r.height);

    }

   

 

    protected static void doScreenCapture (String filename, int x, int y, int width, int height)

    {

        try {

            BufferedImage capture = null;

            Rectangle area = new Rectangle(x, y, width, height);

            Robot robot = new Robot();         

            capture = robot.createScreenCapture(area);

            FileOutputStream out =

            new FileOutputStream(filename);

            JPEGImageEncoder encoder =

            JPEGCodec.createJPEGEncoder(out);

            encoder.encode(capture);

            out.flush();

            out.close();

        }

        catch (Exception e) {

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

        }

    }

   

}

相關文章