Java framework to save a Web page as a picture

Source: Internet
Author: User

The first thing to understand is that Java is very weak in the image. Java implementation is not difficult, the principle is to save the current screen into a diagram, and then get the mouse to pull to go to the location and then save the captured image to the panel inside, and then generate pictures. So what are we going to say here? Okay, here's a couple of frames to save the page as a picture:

1, Html2image

This is a lot of speculation online. I say this is a jar package that the original Java code encapsulates. The effect is very poor, the code is not posted on the Internet a lot.

2. Cobra

If you do not know this, you should have heard of Lobobrowser, the pure Java implementation of the browser, tested under, except to start the slow dying other can also.

This code interception is still good, do not say directly on the code:

Packagehtmltoimage;

Importjava.awt.image.BufferedImage;

Importjava.awt.image.RenderedImage;

Importjava.io.File;

Importjavax.imageio.ImageIO;

Importjavax.swing.JFrame;

Importjavax.swing.JPanel;

importjavax.swing.SwingUtilities;

Importorg.lobobrowser.html.gui.HtmlPanel;

Importorg.lobobrowser.html.test.SimpleHtmlRendererContext;

Importorg.lobobrowser.html.test.SimpleUserAgentContext;

Publicclasscobratest {

Publicstaticvoidmain (string[] args) throwsexception {

JFrame window =newjframe ();

htmlpanel panel =newhtmlpanel ();

Window.getcontentpane (). Add (panel);

window.setsize (600,400);

window.setvisible (true);

Newsimplehtmlrenderercontext (Panel,newsimpleuseragentcontext ())

. Navigate ("http://www.elsyy.com/");

System.out.println ("ten");

Thread.Sleep (10000);

bufferedimage Image =newbufferedimage (Panel.getwidth (),

panel.getheight (), BUFFEREDIMAGE.TYPE_INT_ARGB);

//Paint the editor onto the image

swingutilities.paintcomponent (Image.creategraphics (), panel,

Newjpanel (), 0,0, Image.getwidth (), Image.getheight ());

//Save the image to file

Imageio.write ((renderedimage) image, "PNG", NewFile ("Html.png"));

System.out.println ("www");

}

}

But the framework should have a limit, and CSS3 should not support it.

3, Cssbox

this is very good. It's perfect if the site doesn't make any intentional restrictions .


Packagehtmltoimage;

Importjava.io.File;

Importjava.io.FileOutputStream;

Importorg.fit.cssbox.demo.ImageRenderer;

Publicclasscssbox {

Publicstaticvoidmain (string[] args) throwsexception {

Imagerenderer render =newimagerenderer ();

System.out.println ("Kaishi");

String url = "http://www.elsyy.com/";

FileOutputStream out =newfileoutputstream (NewFile ("D:" +file.separator+ "html.png"));

render.renderurl (URL, out, imagerenderer.type_png);

System.out.println ("OK");

}

}


4. Java native Code


Packagehtmltoimage;

Importjava.awt.image.BufferedImage;

Importjava.awt.image.RenderedImage;

Importjava.io.File;

Importjava.net.URL;

Importjavax.imageio.ImageIO;

Importjavax.swing.JEditorPane;

Importjavax.swing.JPanel;

importjavax.swing.SwingUtilities;


/**

* The principle is to display the page on the current AWT or swing and save the content as a picture

* There's no way to control the delay.

* @author Zlqiao

*

*/

Publicclassjavacoreapi {

Publicstaticvoidmain (string[] args) throwsexception {

//load the webpage into the editor

//jeditorpane ed = new JEditorPane (New URL ("http://www.google.com"));

jeditorpane Ed =newjeditorpane (Newurl ("http://www.baidu.com"));

System.out.println ("ten");

Thread.Sleep (10000);

ed.setsize (1000,1000);

//create a new image

bufferedimage Image =newbufferedimage (Ed.getwidth (), Ed.getheight (),

Bufferedimage.type_int_argb);

//paint the editor onto the image

swingutilities.paintcomponent (Image.creategraphics (),

Ed,

Newjpanel (),

0,0, Image.getwidth (), Image.getheight ());

//save the image to file

Imageio.write ((renderedimage) image, "PNG", NewFile ("Html.png"));

System.out.println ("OK");

}

}

The above is the introduction of several pages saved as a picture of the Java framework, I hope to help you, If you want to learn more Programming Language Tutorials Knowledge Please login e mentor net.


This article is from the "Add Language" blog, please make sure to keep this source http://yuguotianqing.blog.51cto.com/9292883/1551978

Java framework to save a Web page as a picture

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.