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
    评论
Java WebMagic是一个开源的Java爬虫框架,可以用于爬取网页数据,包括图片。它提供了简单易用的API,可以方便地定义爬取规则和处理爬取结果。 要使用Java WebMagic进行图片爬取,首先需要添加WebMagic的依赖到你的项目中。然后,你可以按照以下步骤进行操作: 1. 创建一个Java类,作为你的爬虫程序的入口点。 2. 在该类中,使用WebMagic提供的注解和API定义你的爬取规则。你可以指定要爬取的网页URL、要提取的图片链接等信息。 3. 实现一个自定义的处理器(Pipeline),用于处理爬取到的图片数据。你可以将图片保存到本地或者进行其他处理。 4. 创建一个爬虫对象,并设置好爬取规则和处理器。 5. 启动爬虫,开始爬取图片。 以下是一个简单的示例代码,演示了如何使用Java WebMagic进行图片爬取: ```java import us.codecraft.webmagic.Spider; import us.codecraft.webmagic.pipeline.FilePipeline; import us.codecraft.webmagic.processor.PageProcessor; public class ImageSpider { public static void main(String[] args) { // 创建一个PageProcessor对象,用于定义爬取规则 PageProcessor pageProcessor = new MyPageProcessor(); // 创建一个Pipeline对象,用于处理爬取结果 FilePipeline filePipeline = new FilePipeline("保存图片的目录"); // 创建一个Spider对象,并设置好PageProcessor和Pipeline Spider spider = Spider.create(pageProcessor) .addUrl("要爬取的网页URL") .addPipeline(filePipeline); // 启动爬虫 spider.run(); } // 自定义的PageProcessor类,用于定义爬取规则 static class MyPageProcessor implements PageProcessor { @Override public void process(Page page) { // 提取图片链接,并将其添加到爬取队列中 page.addTargetRequests(page.getHtml().$("img[src]").all()); // 获取图片链接,并保存到结果中 page.putField("image", page.getHtml().$("img[src]").all()); } @Override public Site getSite() { return Site.me(); } } } ``` 请注意,上述示例代码中的"要爬取的网页URL"和"保存图片的目录"需要根据实际情况进行替换。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值