How to take a screenshot on the Internet?

I use this code below, but the generated image does not work. I think this is probably due to visualization options. Does anyone know what is going on?

package webpageprinter;

import java.net.URL;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import javax.swing.text.html.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;

public class WebPagePrinter {
private BufferedImage image = null;

public BufferedImage Download(String webpageurl) {
try
{
    URL url = new URL(webpageurl);
    final JEditorPane jep = new JEditorPane();
    jep.setContentType("text/html");
    ((HTMLDocument)jep.getDocument()).setBase(url);
    jep.setEditable(false);
    jep.setBounds(0,0,1024,768);
    jep.addPropertyChangeListener("page",new
    PropertyChangeListener() {
                @Override
    public void propertyChange(PropertyChangeEvent e) {
    try
    {
        image = new
        BufferedImage(1024,768,BufferedImage.TYPE_INT_RGB );
        Graphics g = image.getGraphics();
        Graphics2D graphics = (Graphics2D) g;
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        jep.paint(graphics);
        ImageIO.write(image,"png",new File("C:/webpage.png"));
    }
    catch (Exception re)
    {
        re.printStackTrace();
    }
    }});
    jep.setPage(url);

}
catch (Exception e)
{
e.printStackTrace();
}
return image;
}

    public static void main(String[] args) {

        new WebPagePrinter().Download("http://www.google.com");

    }
}
+5
source share
6 answers

I think there are 3 problems and one fragility in this code:

Problems

  • JEditorPane It was never intended to be a browser.
  • setPage(URL)loaded asynchronously. To determine the page load, you must add a listener.
  • You may find that some sites automatically refuse to connect to Java clients.

Fragility

Fragile nature is included in the challenge setBounds(). Use layouts.

Image at 400x600

Google screen shot

, , , 3 , 2 . 1. JEditorPane . JavaScript, JEP , .

+3

Java Robot (API ).

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class RobotExp {

    public static void main(String[] args) {

        try {

            Robot robot = new Robot();
            // Capture the screen shot of the area of the screen defined by the rectangle
            BufferedImage bi=robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
            ImageIO.write(bi, "jpg", new File("C:/imageTest.jpg"));

        } catch (AWTException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

. .

+2

, Java JEditorPane -, HTML. -, .

- Java, , , Selenium , Firefox.

+1

javadoc

HTML. , , - javax.swing.text.html.HTMLEditorKit, HTML 3.2.

, , , HTML4, 5 XHTML.....

SO Java: Java/Swing?

+1

flying-saucer. PDF- HTML-.

+1

Selenium WebDriver, VirtualFramebuffer Firefox Binary. ubuntu. xvfb Firefox. : , .

Firefox :

aptitude install xvfb firefox

, /tmp/screenshot.png

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CaptureScreenshotTest
{
    private static int DISPLAY_NUMBER=99;
    private static String XVFB="/usr/bin/Xvfb";
    private static String XVFB_COMMAND= XVFB + " :" + DISPLAY_NUMBER;
    private static String URL="http://www.google.com/";
    private static String RESULT_FILENAME="/tmp/screenshot.png";

    public static void main ( String[] args ) throws IOException
    {
        Process p = Runtime.getRuntime().exec(XVFB_COMMAND);
        FirefoxBinary firefox = new FirefoxBinary();
        firefox.setEnvironmentProperty("DISPLAY", ":" + DISPLAY_NUMBER);
        WebDriver driver = new FirefoxDriver(firefox, null);
        driver.get(URL);
        File scrFile = ( (TakesScreenshot) driver ).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File(RESULT_FILENAME));
        driver.close();
        p.destroy();
    }
}
+1

All Articles