Java 将网页另存为图像
作为一个爱好项目,我正在探索将网页(HTML)保存为图像的方法,主要是使用c/c++/javascript/java编程。到目前为止,我遇到了以下几种方法:Java 将网页另存为图像,java,javascript,c++,html,image,Java,Javascript,C++,Html,Image,作为一个爱好项目,我正在探索将网页(HTML)保存为图像的方法,主要是使用c/c++/javascript/java编程。到目前为止,我遇到了以下几种方法: 获取页面正文的IHTMLElement,并使用它查询ihtmlelementtrender,然后使用其DrawToDC方法(Ref:)。但问题是,它并不适用于所有页面(大多数页面都嵌入了iframe) 我能想到的另一种方法是使用一些web浏览器组件,当页面完全加载时,使用BitBlt(Ref:)捕获它。但问题是,我请求的页面可能比我的屏幕长
IHTMLElement
,并使用它查询ihtmlelementtrender
,然后使用其DrawToDC
方法(Ref:)。但问题是,它并不适用于所有页面(大多数页面都嵌入了iframe)BitBlt
(Ref:)捕获它。但问题是,我请求的页面可能比我的屏幕长,并且它无法装入web浏览器组件对于解决上述问题的任何指导/建议或替代方法,我们将不胜感激。如果您使用Python,则有和。不过,它们都有一些依赖关系 编辑:哦,Python不在您的首选语言列表中。无论如何,我会把这个答案留在这里,因为你说的是“主要”而不是“唯一”。另一个(有点迂回)选项是运行像Tomcat这样的服务器,并使用Java调用命令行工具来截图。通过谷歌搜索“命令行屏幕截图窗口”可以找到一些合理的可能性。不过,除了运行服务器之外,我还不知道从javascript运行本地可执行文件的好方法。不过,这种方法可以使它跨浏览器,这是一个优点(当您需要屏幕截图时,只需对脚本进行ajax调用) 不幸的是,我实际上不知道如何部署war文件。使用Tomcat可能会更麻烦;我提到它是因为Java是首选语言。运行XAMPP并使用此PHP代码段将非常简单,您不必真正学习PHP:
<?php
exec("/path/to/exec args");
?>
编辑
你知道,我不确定这是否真的回答了你的问题。这是一种方法,但它是从JavaScript端而不是脚本端开始的。如果您想通过脚本编写来完成,您可以始终使用Selenium。它支持捕获整个页面的屏幕截图,并且可以通过Java进行控制。通过阅读以下两篇文章,我们最终能够破解它:
如果您可以使用javascript,我建议您使用 来自
谢谢kijin。。我已经考虑过了,但是为了使用它,我必须学习python:)谢谢,你能在这里详细介绍一下tomcat和java的用法吗。假设我想访问tomcat部件的安装位置。Selenium是一个不错的选择,我会研究一下。实际上,我不知道在Tomcat上部署web应用程序有多复杂。使用XAMPP和PHP代码片段可能更容易。编辑了我的答案。
var page = new WebPage(),
address = 'http://dev.sencha.com/deploy/ext-4.0.7-gpl/examples/feed-viewer/feed-viewer.html';
page.viewportSize = {
width : 800,
height : 600
};
// define the components we want to capture
var components = [{
output : 'feed-viewer-left.png',
//ExtJS has a nice component query engine
selector : 'feedpanel'
},{
output : 'feed-viewer-preview-btn.png',
selector : 'feeddetail > feedgrid > toolbar > cycle'
},{
output : 'feed-viewer-collapsed.png',
//executed before the rendering
before : function(){
var panel = Ext.ComponentQuery.query('feedpanel')[0];
panel.animCollapse = false; // cancel animation, no need to wait before capture
panel.collapse();
},
selector : 'viewport'
}];
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
/*
* give some time to ExtJS to
* - render the application
* - load asynchronous data
*/
window.setTimeout(function () {
components.forEach(function(component){
//execute the before function
component.before && page.evaluate(component.before);
// get the rectangular area to capture
/*
* page.evaluate() is sandboxed
* so that 'component' is not defined.
*
* It should be possible to pass variables in phantomjs 1.5
* but for now, workaround!
*/
eval('function workaround(){ window.componentSelector = "' + component.selector + '";}')
page.evaluate(workaround);
var rect = page.evaluate(function(){
// find the component
var comp = Ext.ComponentQuery.query(window.componentSelector)[0];
// get its bounding box
var box = comp.el.getBox();
// box is {x, y, width, height}
// we want {top, left, width, height}
box.top = box.y;
box.left = box.x;
return box;
});
page.clipRect = rect;
page.render(component.output);
});
// job done, exit
phantom.exit();
}, 2000);
}
});