How can I use Java or other technologies to convert html into images?Chat with friends ~
I found several methods online.
1. html2image [java]
Html2image: http://code.google.com/p/java-html2image/
Jar: http://code.google.com/p/java-html2image/downloads/list
HtmlImageGenerator imageGenerator = new HtmlImageGenerator();imageGenerator.loadHtml(Hello World! Please goto Google.);imageGenerator.saveAsImage(hello-world.png);imageGenerator.saveAsHtmlWithMap(hello-world.html, hello-world.png);
HtmlImageGenerator Methods
LoadUrl (url)-Loads HTML from URL object or URL string. (load html from url)
LoadHtml (html)-Loads HTML source. (load local html)
SaveAsImage (file)-Save loaded HTML as image. (Save html as an image)
SaveAsHtmlWithMap (file, imageUrl)-Creates an HTML file containing client-side image-mapGenerated from HTML's links. (create an HTML file containing the client image-map)
GetLinks ()-List all links in the HTML document and their corresponding href, target, title, position and dimension. (list all links in HTML documents and corresponding href, targets, titles, positions, and sizes)
GetBufferedImage ()-Get AWT buffered image of the HTML. (Get the awt, html buffered image)
GetLinksMapMarkup (mapName)-Get HTML snippet of the client-side image-mapGenerated from the links. (link generated by the client image-map <map> in the HTML code segment)
Get/setOrientation (orientation)-Get/Set document orientation (left-to-right or right-to-left). (get/set text location)
Get/setSize (dimension)-Get/Set size of the generated image. (Set the size of the generated image)
Test results: Images with relative paths are not supported, and the layout is disordered.
2. pure Java http://www.tuicool.com/articles/2yAryy
Conversion of HTML fragments in Java to images does not require calling additional jar packages. css is supported, but css files written between styles and external css files are not supported and can only be written on labels.
I know that external css files are not supported and I will not be tested.
Sample Code:
Import java. awt. color; import java. awt. dimension; import java. awt. graphics; import java. awt. graphics2D; import java. awt. image. bufferedImage; import java. io. byteArrayOutputStream; import javax. swing. JTextPane; import javax. swing. border. emptyBorder; import javax. swing. plaf. basic. basicEditorPaneUI; import com.sun.image.codec.jpeg. export codec; import com.sun.image.codec.jpeg. JPEGEncodeParam; import com. sun. image. co Dec.jpeg. using imageencoder; import com. xxx. util. fileUtil;/*** HTML conversion method ***/public class HTML2Picture {public static int DEFAULT_IMAGE_WIDTH = 730; public static int DEFAULT_IMAGE_HEIGHT = 700; public static boolean paintPage (Graphics g, int hPage, int pageIndex, JTextPane panel) {Graphics2D g2 = (Graphics2D) g; Dimension d = (BasicEditorPaneUI) panel. getUI ()). getPreferredSize (panel); doubl E panelHeight = d. height; double pageHeight = hPage; int totalNumPages = (int) Math. ceil (panelHeight/pageHeight); g2.translate (0f,-(pageIndex-1) * pageHeight); panel. paint (g2); boolean ret = true; if (pageIndex> = totalNumPages) {ret = false; return ret ;} /*** convert html to jpeg file ** @ param bgColor * background color of the image * @ param html * html Text information * @ param width * display the width of the image Text container * @ param height * Display the height of the Text container of the image * @ param eb * set the border of the container * @ return * @ throws Exception */private static void html2jpeg (Color bgColor, String html, int width, int height, EmptyBorder eb) throws Exception {JTextPane tp = new JTextPane (); tp. setSize (width, height); if (eb = null) {eb = new EmptyBorder (0, 50, 0, 50);} if (bgColor! = Null) {tp. setBackground (bgColor);} if (width <= 0) {width = DEFAULT_IMAGE_WIDTH;} if (height <= 0) {height = DEFAULT_IMAGE_HEIGHT;} tp. setBorder (eb); tp. setContentType (text/html); tp. setText (html); int pageIndex = 1; boolean bcontinue = true; while (bcontinue) {BufferedImage image = new java. awt. image. bufferedImage (width, height, java. awt. image. bufferedImage. TYPE_INT_RGB); Graphics g = ima Ge. getGraphics (); g. setClip (0, 0, width, height); bcontinue = paintPage (g, height, pageIndex, tp); g. dispose (); ByteArrayOutputStream baos = new ByteArrayOutputStream (); required imageencoder encoder = required codec. createJPEGEncoder (baos); incluencodeparam param = encoder. getdefajpegincluencodeparam (image); param. setQuality (1.0f, false); encoder. setincluencodeparam (param); encoder. encode (image); byte [] bytes = Baos. toByteArray (); baos. close (); FileUtil. writeBinFile (C: S.jpg, bytes); pageIndex ++ ;}} public static void main (String [] args) throws Exception {html2jpeg (Color. white, FileUtil. readAscFile (C: able.html), DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_WIDTH, new EmptyBorder (0, 0, 0, 0); System. out. println (over !); }}
3. html2canvas [JS technology]
Official Website: http://html2canvas.hertzen.com/
Test results: compared with the previous two types, the image and css external links are supported. The disadvantage is that some of them still do not work properly.
4. Waiting for research
DJNativeSwing simulated Browser