Convert HTML pages into image images using Java

Source: Internet
Author: User

In java, HTML is converted into an image file. The idea is that the webpage is displayed on the AWT or Swing Panel and the Panel is output as an image file. Java APIs provide relevant results, but the results are not very good, so there are some libraries. java Core API [java] public class HtmlToImage {protected static void generateOutput () 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.hefeipet.com/client/chongwuzhishi/shenghuozatan/2012/0220/95.html"); ed. setSize (, 20 0); // create a new image BufferedImage image = new BufferedImage (ed. getWidth (), ed. getHeight (), BufferedImage. TYPE_INT_ARGB); // paint the editor onto the image SwingUtilities. paintComponent (image. createGraphics (), ed, new JPanel (), 0, 0, image. getWidth (), image. getHeight (); // save the image to file ImageIO. write (RenderedImage) image, "png", new File ("html.png");} public static void main (String [] Args) {try {generateOutput ();} catch (Exception e) {e. printStackTrace () ;}} java-html2image open source, very concise to use. Both API Doc and source code are available. [Java] import gui.ava.html. image. generator. htmlImageGenerator; public class Html2ImageTest {public static void main (String [] args) {HtmlImageGenerator imageGenerator = new HtmlImageGenerator (); // imageGenerator //. loadHtml ("<B> Hello World! </B> Please goto <a title = \ "Goto Google \" href = \ "http://www.google.com \"> Google </a>. "); // imageGenerator. loadUrl ("http: // mtkplmap2: 7001/esdm_web"); imageGenerator. loadUrl ("http://www.hefeipet.com/client/chongwuzhishi/shenghuozatan/2012/0220/95.html"); imageGenerator. saveAsImage ("hello-world.png"); imageGenerator. saveAsHtmlWithMap ("hello-world.html", "hello-world.png") ;}} Cobra free, open source [java] 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://www.hefeipet.com/client/chongwuzhishi/shenghuozatan/2012/0220/95.html"); BufferedImage image = new BufferedImage (panel. getWidth (), panel. getHeight (), BufferedImage. TYPE_INT_ARGB); // paint the editor onto the image SwingUtilities. paintComponent (image. createGraphics (), panel, new JPanel (), 0, 0, image. getWidth (), image. getHeight (); // save the image to file ImageIO. write (RenderedImage) image, "png", new File ("html.png "));}}

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.