如何打开网址并为其截屏?

时间:2015-12-19 23:00:34

标签: java image url web frame

我正在尝试打开一个给定的网址,然后为其拍摄一个屏幕截图并将其输出为框架背景。我不知道如何填写此代码。 我如何缩放保存的图像以适应框架?

public static void main(String[] args) throws URISyntaxException, IOException, AWTException 
{ 
     URI MyURL;
     String url;
     WebView view;

     url = "http://facebook.com";
     MyURL = new URI (url);
     getDesktop().browse(MyURL);

    BufferedImage Image = ImageIO.read(new File ("C:\\Users\\hagar001\\Desktop\\OOP\\tt.jpg"));
}

public void onPageFinished(WebView view, String web, String url) throws IOException, AWTException 
{
    if(web.equals(url))
    {
        Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage capture = new Robot().createScreenCapture(screenRect);
        capture =  (BufferedImage) capture.getScaledInstance(700, 700, BufferedImage.SCALE_DEFAULT);
        ImageIO.write(capture, "jpg", new File ("C:\\Users\\hagar001\\Desktop\\OOP\\tt.jpg"));
    }
    else 
    {
        System.out.println ("Error loading the page");
    }

}

1 个答案:

答案 0 :(得分:0)

Desktop.browse()无法通知整理页面。 所以你应该等一会儿才能捕获屏幕。

// ...
getDesktop().browse(MyURL);
Thread.sleep(3000);  // wait 3 seconds
onPageFinished(null, url, url);
// ...