HTML转图片

PhantomJS
NodeJS + PhantomJS 抓取页面信息以及截图 http://www.cnblogs.com/justany/p/3279717.html
PhantomJS1.7 API http://www.cnblogs.com/ziyunfei/archive/2012/09/28/2705369.html
配置: Linux把PhantomJS加入到环境变量,window的直接运行exe文件。
利用phantomjs+casperjs实现登陆抓取网页并截图 http://my.oschina.net/jiuxiaoyao/blog/148456

利用phantomjs将HTML源代码转换成图片 http://neoremind.net/2013/02/%E5%88%A9%E7%94%A8phantomjs%E5%B0%86html%E6%BA%90%E4%BB%A3%E7%A0%81%E8%BD%AC%E6%8D%A2%E6%88%90%E5%9B%BE%E7%89%87/
PhantomJS是一个无界面的WebKit浏览器引擎,还有配套的JavaScript API.它原生支持各种web标准技术: DOM处理, CSS选择器, JSON, Canvas, 以及SVG.
通常我们的需求是将网页渲染成图片保存,那么也会有这样的情况,将HTML源代码转换成图片,下面的代码将满足需求:
var webpage = require('webpage')
  , page = webpage.create();
page.viewportSize = { width: 1024, height: 800 };
page.clipRect = { top: 0, left: 0, width: 1024, height: 800 };
page.settings = {
  javascriptEnabled: false,
  loadImages: true,
  userAgent: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) PhantomJS/19.0'
};
page.open('http://neoremind.net/2013/02/%E5%88%A9%E7%94%A8phantomjs%E5%B0%86html%E6%BA%90%E4%BB%A3%E7%A0%81%E8%BD%AC%E6%8D%A2%E6%88%90%E5%9B%BE%E7%89%87/', function (status) {
  var data;
  if (status === 'fail') {
    console.log('open page fail!');
  } else { 
    page.render('./snapshot/test.png');
  }
  // release the memory
  page.close();
  phantom.exit();
});

保存成texttoimage.js
运行: phantomjs texttoimage.js
图片保存到newimage.png中

无需浏览器执行Javascript脚本: SlimerJS http://www.open-open.com/lib/view/open1374371986777.html
SlimerJS能够利用Javascript脚本来操作一个网页。SlimerJS允许你在浏览器外执行javascript脚本。
SlimerJS用于做功能测试,页面自动化,网络监控,屏幕捕捉,等。
SlimerJS类似于PhantomJs,但它运行在Gecko之上。而SlimerJS采用Mozilla Firefox浏览器引擎来代替Webkit。

CutyCapt: 开源网页截屏工具 CutyCapt Linux 下安装和使用
http://ddbean.wordpress.com/2011/02/19/%E5%BC%80%E6%BA%90%E7%BD%91%E9%A1%B5%E6%88%AA%E5%B1%8F%E5%B7%A5%E5%85%B7-cutycapt-linux-%E4%B8%8B%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8/
截取指定网页图片-CutyCapt的应用 http://jingyan.baidu.com/article/90895e0f8f704364ec6b0b97.html



Html2Image https://code.google.com/p/java-html2image/
在这里看到更新的包: http://evgenyg.artifactoryonline.com/evgenyg/repo/gui/ava/html2image/
        <repository>
            <id>yoava</id>
            <name>AOL yoava</name>
            <url>http://repo.jfrog.org/artifactory/third-party-releases-local</url>
        </repository>
......
        <dependency>
            <groupId>gui.ava</groupId>
            <artifactId>html2image</artifactId>
            <version>2.0.1</version>
        </dependency>


<dependency>
   <groupId>gui.ava</groupId>
   <artifactId>html2image</artifactId>
   <version>0.9</version>
</dependency>

<repositories>
   <repository>
      <id>yoava</id>
      <name>AOL yoava</name>
      <url>http://yoava.artifactoryonline.com/yoava/repo</url>
   </repository>
</repositories>


HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
imageGenerator.loadHtml("<b>Hello World!</b> Please goto <a title=\"Goto Google\" href=\"http://www.google.com\">Google</a>.");
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.
loadHtml(html) - Loads HTML source.
saveAsImage(file) - Save loaded HTML as image.
saveAsHtmlWithMap(file, imageUrl) - Creates an HTML file containing client-side image-map <map> generated from HTML's links.
getLinks() - List all links in the HTML document and their corresponding href, target, title, position and dimension.
getBufferedImage() - Get AWT buffered image of the HTML.
getLinksMapMarkup(mapName) - Get HTML snippet of the client-side image-map <map> generated from the links.
get/setOrientation(orientation) - Get/Set document orientation (left-to-right or right-to-left).
get/setSize(dimension) - Get/Set size of the generated image


这个还能截图,但是要打开一个新窗口。要是服务器没有安装浏览器,那它就无能为力了。
private void test2(String url) {
        try {
            // 此方法仅适用于JdK1.6及以上版本
            Desktop.getDesktop().browse(
                    new URL(url).toURI());
            Robot robot = new Robot();
            robot.delay(10000);
            Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
            int width = (int) d.getWidth();
            int height = (int) d.getHeight();
            // 最大化浏览器
            robot.keyRelease(KeyEvent.VK_F11);
            robot.delay(2000);
            Image image = robot.createScreenCapture(new Rectangle(0, 0, width,
                    height));
            BufferedImage bi = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics g = bi.createGraphics();
            g.drawImage(image, 0, 0, width, height, null);
            // 保存图片
            ImageIO.write(bi, "jpg", new File("/mnt/D/work_documents/workspace/dev_cpm/2.jpg"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



Cobra: 介绍: http://blog.csdn.net/peerlessbloom/article/details/7046777
Cobra主页: http://lobobrowser.org/
Cobra API: http://lobobrowser.org/cobra-api/index.html
只支持jdk6  不支持jdk7, 因为jdk7删除了一些字体包

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://www.hefeipet.com/client/chongwuzhishi/shenghuozatan/2012/0220/95.html");  
BufferedImage image = new BufferedImage(panel.getWidth(),  
panel.getHeight(), BufferedImage.TYPE_INT_ARGB);  
SwingUtilities.paintComponent(image.createGraphics(), panel,  new JPanel(), 0, 0, image.getWidth(), image.getHeight());  
ImageIO.write((RenderedImage) image, "png", new File("html.png"));




jsp网页快照,根据url获取页面缩略图 http://hi.baidu.com/bdgaojj/item/1ef674c17e0c807bcfd4f8de
java网页快照-网页转存为图片实现代码 http://www.111cn.net/jsp/java/43586.htm
个人觉得免费的java实现方式,最方便的网页快照生成方式。可以生成大型网页的快照。非常棒! 使用代码前需要导入 jar包。需要三个jar包 : swt-3.6M3-win32-win32-x86.jar,DJNativeSwing-SWT.jar,DJNativeSwing.jar
JSP网页快照代码(自动生成静态页面) http://blog.sina.com.cn/s/blog_40612750010005f5.html
Java 实现HTML 页面转成image 图片 http://blog.csdn.net/oscar999/article/details/8696769
public class HtmlToImage {

	protected static void generateOutput() 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.hefeipet.com/client/chongwuzhishi/shenghuozatan/2012/0220/95.html"));
		ed.setSize(200,200);

		//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"));
	}
	public static void main(String[] args) {
		try {
			generateOutput();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

猜你喜欢

转载自panyongzheng.iteye.com/blog/1945683