简体   繁体   English

Java代码将网页另存为图像

[英]Java Code to save Web page as image

I have an application developed using struts2. 我有一个使用struts2开发的应用程序。 One of my web page has a div in which it displays a world map created using Google map API. 我的网页之一有一个div,其中显示了使用Google Map API创建的世界地图。 On click of a button I want to save this map as an image on the server location. 单击按钮后,我想将此地图另存为服务器位置上的图像。 I tried this using the ROBOT class but this is not working. 我使用ROBOT类尝试了此操作,但这不起作用。 My application supports IE8. 我的应用程序支持IE8。 Below is the code I wrote: 以下是我编写的代码:

Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rect = new Rectangle(screenDim);
Robot rob = new Robot();
BufferedImage img = rob.createScreenCapture(rect);
String FileName="D:\\SP_Maps\\Map.png";
ImageIO.write(img, "png", new File(FileName));

Basically I tried to take a screen shot of the page on click of a button and save it as an image. 基本上,我试图通过单击按钮对页面进行截图,然后将其另存为图像。 This works fine on my local host but, when I deploy this on my server and try to get the screen shot I just get a black page saved as png image. 这在我的本地主机上运行良好,但是当我在服务器上部署它并尝试获取屏幕快照时,我只是得到了一个保存为png图像的黑页。

While you are developing a web application you may use javascript if you are interested.. 在开发Web应用程序时,如果您感兴趣,可以使用javascript。

To get a screen shot and save it with any format you may use PhantomJS 要获取屏幕截图并将其保存为任何格式,可以使用PhantomJS

PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast
and native support for various web standards: DOM handling, CSS selector, 
JSON, Canvas, and SVG.

Check those examples written with PhantomJS: 检查那些用PhantomJS编写的示例:

https://github.com/ariya/phantomjs/wiki/Examples https://github.com/ariya/phantomjs/wiki/示例

Also check this tutorial Taking website screenshots using PhantomJS 另请查看本教程使用PhantomJS拍摄网站截图

The tutorial is about taking a web shot and saving it as JPEG, PNG, PDF ... etc 本教程是关于拍摄网络快照并将其另存为JPEG,PNG,PDF等的。

Hope this helps you... 希望这对您有帮助...

Can you explain your use case clearly ? 您可以清楚地说明用例吗? If I understand correctly you have a web application which has a functionality to take a screen capture upon user action? 如果我正确理解您的Web应用程序,该应用程序具有可以根据用户操作进行屏幕截图的功能? Ideally, Java Robot utility should use to do automated testing of java applications. 理想情况下,应使用Java Robot实用程序对Java应用程序进行自动化测试。 As per the doc 根据文档

The primary purpose of Robot is to facilitate automated testing of Java platform implementations. Robot的主要目的是促进Java平台实现的自动化测试。

Maybe you can try using phantomjs. 也许您可以尝试使用phantomjs。 example

var page = require('webpage').create();
page.open('http://google.com', function () {
    page.render('google.png');
    phantom.exit();
});

https://github.com/ariya/phantomjs/wiki/Quick-Start https://github.com/ariya/phantomjs/wiki/快速入门

To perform user actions like button clicks, you can use casper.js http://casperjs.org/quickstart.html 要执行诸如按钮单击之类的用户操作,您可以使用casper.js http://casperjs.org/quickstart.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM