介绍几个java把网页报存为图片的框架
- - CSDN博客Web前端推荐文章用java实现java截图倒不难,原理吗就是把当前屏幕存成一个图,然后获取鼠标拉去的想去位置然后把截取的图保存到panel里边,再生成图片即可:示例代码就不展示了,网上很多. 下边说几个将网页保存为图片的框架:. 我说这个就是原声的java代码进行封装的一个jar包. 效果非常差,代码就不贴了网上好多.
首先说一下。java在图像这一块非常弱。用java实现java截图倒不难,原理吗就是把当前屏幕存成一个图,然后获取鼠标拉去的想去位置然后把截取的图保存到panel里边,再生成图片即可:示例代码就不展示了,网上很多。下边说几个将网页保存为图片的框架:
①html2image
网上炒这个还不少呢。我说这个就是原声的java代码进行封装的一个jar包。效果非常差,代码就不贴了网上好多。
②cobra
如果你不知道这个的话,你应该听说过lobobrowser,纯java实现的浏览器,测试了下,除了启动慢的要死其他还可以。
这个代码截取还是不错的,不说了直接上代码:
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 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")); System.out.println("www"); } }
③cssbox
这个非常不错。如果网站不做故意限制的话,截图非常完美。。。
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"); } }
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; /** * 原理就是在现在的awt或者swing上显示网页然后将内容保存为一个图片 * 没办法控制延迟啊。 * @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 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")); System.out.println("ok"); } }