HTML转图片

PhantomJS

NodeJS+PhantomJS抓取页面信息以及截图http://www.cnblogs.com/justany/p/3279717.html

PhantomJS1.7APIhttp://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浏览器引擎,还有配套的JavaScriptAPI.它原生支持各种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

运行:phantomjstexttoimage.js

图片保存到newimage.png中

无需浏览器执行Javascript脚本:SlimerJShttp://www.open-open.com/lib/view/open1374371986777.html

SlimerJS能够利用Javascript脚本来操作一个网页。SlimerJS允许你在浏览器外执行javascript脚本。

SlimerJS用于做功能测试,页面自动化,网络监控,屏幕捕捉,等。

SlimerJS类似于PhantomJs,但它运行在Gecko之上。而SlimerJS采用MozillaFirefox浏览器引擎来代替Webkit。

CutyCapt:开源网页截屏工具CutyCaptLinux下安装和使用

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

Html2Imagehttps://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");

HtmlImageGeneratorMethods

loadUrl(url)-LoadsHTMLfromURLobjectorURLstring.

loadHtml(html)-LoadsHTMLsource.

saveAsImage(file)-SaveloadedHTMLasimage.

saveAsHtmlWithMap(file,imageUrl)-CreatesanHTMLfilecontainingclient-sideimage-map<map>generatedfromHTML'slinks.

getLinks()-ListalllinksintheHTMLdocumentandtheircorrespondinghref,target,title,positionanddimension.

getBufferedImage()-GetAWTbufferedimageoftheHTML.

getLinksMapMarkup(mapName)-GetHTMLsnippetoftheclient-sideimage-map<map>generatedfromthelinks.

get/setOrientation(orientation)-Get/Setdocumentorientation(left-to-rightorright-to-left).

get/setSize(dimension)-Get/Setsizeofthegeneratedimage

这个还能截图,但是要打开一个新窗口。要是服务器没有安装浏览器,那它就无能为力了。

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/

CobraAPI: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();
		}
	}

}

相关推荐