java在图像这一块非常弱。用java实现java截图倒不难,原理吗就是把当前屏幕存成一个图,然后获取鼠标拉去的想去位置然后把截取的图保存到panel里边,再生成图片即可:示例代码就不展示了,网上很多。下边说几个将网页保存为图片的框架:
①html2image
网上炒这个还不少呢。我说这个就是原声的java代码进行封装的一个jar包。效果非常差,代码就不贴了网上好多。
②cobra
如果你不知道这个的话,你应该听说过lobobrowser,纯java实现的浏览器,测试了下,除了启动慢的要死其他还可以。
这个代码截取还是不错的,不说了直接上代码:
1 packagehtmlToImage;2
3 importjava.awt.image.BufferedImage;4 importjava.awt.image.RenderedImage;5 importjava.io.File;6
7 importjavax.imageio.ImageIO;8 importjavax.swing.JFrame;9 importjavax.swing.JPanel;10 importjavax.swing.SwingUtilities;11
12 importorg.lobobrowser.html.gui.HtmlPanel;13 importorg.lobobrowser.html.test.SimpleHtmlRendererContext;14 importorg.lobobrowser.html.test.SimpleUserAgentContext;15
16 public classCobraTest {17 public static void main(String[] args) throwsException {18 JFrame window = newJFrame();19 HtmlPanel panel = newHtmlPanel();20 window.getContentPane().add(panel);21 window.setSize(600, 400);22 window.setVisible(true);23 new SimpleHtmlRendererContext(panel, newSimpleUserAgentContext())24 .navigate("http://jobs.zhaopin.com/377931819252715.htm?ssidkey=y&ss=201&ff=03");25 System.out.println("10");26 Thread.sleep(10000);27 BufferedImage image = newBufferedImage(panel.getWidth(),28 panel.getHeight(), BufferedImage.TYPE_INT_ARGB);29
30 //paint the editor onto the image
31 SwingUtilities.paintComponent(image.createGraphics(), panel,32 new JPanel(), 0, 0, image.getWidth(), image.getHeight());33 //save the image to file
34 ImageIO.write((RenderedImage) image, "png", new File("html.png"));35 System.out.println("www");36 }37 }
但是这个框架应该有个限制,css3应该支持不了。
③cssbox
这个非常不错。如果网站不做故意限制的话,截图非常完美。。。
packagehtmlToImage;importjava.io.File;importjava.io.FileOutputStream;importorg.fit.cssbox.demo.ImageRenderer;public classCssBox {public static void main(String[] args) throwsException {
ImageRenderer render= newImageRenderer();
System.out.println("kaishi");
String url= "http://www.xiachufang.com/";
FileOutputStream out= new FileOutputStream(new File("D:"+File.separator+"html.png"));
render.renderURL(url, out, ImageRenderer.TYPE_PNG);
System.out.println("OK");
}
}
④java原生代码
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;/*** 原理就是在现在的awt或者swing上显示网页然后将内容保存为一个图片
* 没办法控制延迟啊。
*@authorzlqiao
**/
public classJavaCoreApi {public static void main(String[] args) throwsException {//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 = 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", new File("html.png"));
System.out.println("ok");
}
}