java把网页报存为图片的框架

先说一下。java在图像这一块非常弱。用java实现java截图倒不难,原理吗就是把当前屏幕存成一个图,然后获取鼠标拉去的想去位置然后把截取的图保存到panel里边,再生成图片即可:示例代码就不展示了,网上很多。下边说几个将网页保存为图片的框架:

①html2image

网上炒这个还不少呢。我说这个就是原声的java代码进行封装的一个jar包。效果非常差,代码就不贴了网上好多。

②cobra

如果你不知道这个的话,你应该听说过lobobrowser,纯java实现的浏览器,测试了下,除了启动慢的要死其他还可以。

这个代码截取还是不错的,不说了直接上代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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" );
     }
}

但是这个框架应该有个限制,css3应该支持不了。

③cssbox

这个非常不错。如果网站不做故意限制的话,截图非常完美。。。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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" );
         FileOutputStream out = new FileOutputStream( new File( "D:" +File.separator+ "html.png" ));
         render.renderURL(url, out, ImageRenderer.TYPE_PNG);
         System.out.println( "OK" );
     }
}

④java原生代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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" );
     
     }
}


CSSBox

http://sourceforge.net/projects/cssbox/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值