This article introduces several java frameworks for storing web pages as images.

Source: Internet
Author: User

This article introduces several java frameworks for storing web pages as images.

First, let's talk about it. Java is very weak in image. It is not difficult to implement java in java. The principle is to save the current screen as a graph, get the position of the mouse, save the screenshot to the panel, and then generate the image: the sample code is not displayed, and there are many online examples. Below are some frameworks for saving webpages as images:

① Html2image

There are still a lot of questions on the Internet. I said this is a jar package encapsulated by the original java code. The effect is very poor, so the code will not be pasted on the Internet.

② Cobra

If you do not know this, you should have heard of lobobrowser, a pure java-implemented browser. After testing, you can not only slow startup, but also die.

This code is still good to intercept. If you don't talk about it, directly go to the Code:

package htmlToImage;import java.awt.image.BufferedImage;import java.awt.image.RenderedImage;import java.io.File;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.SwingUtilities;import org.lobobrowser.html.gui.HtmlPanel;import org.lobobrowser.html.test.SimpleHtmlRendererContext;import org.lobobrowser.html.test.SimpleUserAgentContext;public class CobraTest {public static void main(String[] args) throws Exception {JFrame window = new JFrame();HtmlPanel panel = new HtmlPanel();window.getContentPane().add(panel);window.setSize(600, 400);window.setVisible(true);new SimpleHtmlRendererContext(panel, new SimpleUserAgentContext()).navigate("http://jobs.zhaopin.com/377931819252715.htm?ssidkey=y&ss=201&ff=03");System.out.println("10");Thread.sleep(10000);BufferedImage image = new BufferedImage(panel.getWidth(),panel.getHeight(), BufferedImage.TYPE_INT_ARGB);// paint the editor onto the imageSwingUtilities.paintComponent(image.createGraphics(), panel,new JPanel(), 0, 0, image.getWidth(), image.getHeight());// save the image to fileImageIO.write((RenderedImage) image, "png", new File("html.png"));System.out.println("www");}}

But this framework should have a limit, and css3 should not be supported.

③ Cssbox

This is very good. If the website is not intentionally restricted, it will be perfect...

package htmlToImage;import java.io.File;import java.io.FileOutputStream;import org.fit.cssbox.demo.ImageRenderer;public class CssBox {public static void main(String[] args) throws Exception {ImageRenderer render = new ImageRenderer();System.out.println("kaishi");String url = "http://worldwide.espacenet.com/publicationDetails/originalDocument?CC=AU&NR=2014200109A1&KC=A1&FT=D&ND=3&date=20140821&DB=EPODOC&locale=en_EP";FileOutputStream out = new FileOutputStream(new File("D:"+File.separator+"html.png"));render.renderURL(url, out, ImageRenderer.TYPE_PNG);System.out.println("OK");}}

④ Java native code

Package htmlToImage; import java. awt. image. bufferedImage; import java. awt. image. renderedImage; import java. io. file; import java.net. URL; import javax. imageio. imageIO; import javax. swing. JEditorPane; import javax. swing. JPanel; import javax. swing. swingUtilities;/*** the principle is to display the webpage in the current awt or swing and save the content as an image * There is no way to control the latency. * @ Author zlqiao **/public class JavaCoreApi {public static void main (String [] args) throws Exception {// load the webpage into the editor // JEditorPane ed = new JEditorPane (new URL ("http://www.google.com ")); JEditorPane ed = new JEditorPane (new URL ("http://www.baidu.com"); System. out. println ("10"); Thread. sleep (10000); ed. setSize (1000,1000); // create a new imageBufferedImage image = new BufferedImage (ed. getWidth (), ed. getHeight (), BufferedImage. TYPE_INT_ARGB); // paint the editor onto the imageSwingUtilities. paintComponent (image. createGraphics (), ed, new JPanel (), 0, 0, image. getWidth (), image. getHeight (); // save the image to fileImageIO. write (RenderedImage) image, "png", new File ("html.png"); System. out. println ("OK ");}}









How to use JAVA to save a webpage as an image

Import java. awt. Dimension;
Import java. awt. Rectangle;
Import java. awt. Robot;
Import java. awt. Toolkit;
Import java. awt. image. BufferedImage;
Import java. io. File;

Import javax. imageio. ImageIO;

Public class F {
Public static void main (String [] args ){
Try {
// Obtain the screen size
Dimension dim = Toolkit. getdefatooltoolkit (). getScreenSize ();
//
BufferedImage bim = new Robot ()
. CreateScreenCapture (new Rectangle (0, 0, dim. width,
Dim. height ));
ImageIO. write (bim, "jpg", new File ("C: \ test.jpg "));
} Catch (Exception e ){
E. printStackTrace ();
}
}
}

How can I save java images on the webpage?

You can download a jdk online and install it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.