簡體   English   中英

用java截屏

[英]Taking screenshot with java

我正在網頁中測試游戲,我使用的代碼是

 private void takeScreenShot(String screenShotName) {
        File path = new File("/Users/myUser");
        String extension = ".jpg";
        File screenShot = new File(path+"/"+screenShotName );
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(scrFile, screenShot);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

屏幕截圖是這樣的(我一直在為許多應用程序使用相同的代碼,沒有任何問題),我在 OS X 上使用此代碼。

錯誤的截圖

你可以試試這個:

   private void takeScreenShot(String screenShotName) throws AWTException, IOException {

        Robot robot = new Robot();
        String path = "\\Users\\myUser";
        String extension  = "jpg";
        String fileName = path +"\\"+ screenShotName +"."+ extension;

        Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
        ImageIO.write(screenFullImage, extension , new File(fileName));           

} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM