Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何打开URL并为其截屏?_Java_Image_Url_Web_Frame - Fatal编程技术网

Java 如何打开URL并为其截屏?

Java 如何打开URL并为其截屏?,java,image,url,web,frame,Java,Image,Url,Web,Frame,我试图打开一个给定的URL,然后对它进行屏幕截图,并将其作为帧背景输出。我不知道如何完成这段代码。 另外,我如何缩放保存的图像以适应帧 public static void main(String[] args) throws URISyntaxException, IOException, AWTException { URI MyURL; String url; WebView view; url = "http://facebook.com";

我试图打开一个给定的URL,然后对它进行屏幕截图,并将其作为帧背景输出。我不知道如何完成这段代码。 另外,我如何缩放保存的图像以适应帧

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");
    }

}
Desktop.browse()
无法通知完成页面。 所以,您应该等待片刻并捕获屏幕

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

我看不出这个代码是如何工作的。没有
main
方法,我看不到调用onPageFinished的位置、时间、参数等。请提供一个MCVEN主方法。我创建了一个Frame类型的对象,并为jframe设置了一些属性。我不知道如何将参数发送到此函数,这也是问题之一。如果我将此onPageFinished移到主函数,则会截屏,但一旦我想将其放入此函数,则会在加载网页时截屏。请提供MCVE@StephenC我编辑这篇文章