日期:2014-05-17  浏览次数:21003 次

有人做过用JWebBrowser网页截图吗?
由于工作需要,尝试用JWebBrowser网页截图,遇到一些问题。截图是只针对内部网站,由于一个页面里可能在页面加载完成后(我们用的是jQuery(function() {}))可能会发出一个或多个AJAX请求,于是在截图的事件中:
public void loadingProgressChanged(WebBrowserEvent e) {
if (e.getWebBrowser().getLoadingProgress() == 100) {
//截图代码
}
}
请求一个URL,截图代码会被执行多次,也就是e.getWebBrowser().getLoadingProgress()有多次为100了。且当URL为多个时,都不能确定在何时让浏览器转到下一个URL了。

大侠们遇到过这个问题没有呢,都是怎么解决的呢?

------解决方案--------------------
sf~~很遗憾告诉楼主~没做过..
------解决方案--------------------
Java code

1.import java.awt.BorderLayout;   
2.import java.awt.Dimension;   
3.import java.awt.FlowLayout;   
4.import java.awt.image.BufferedImage;   
5.import java.io.File;   
6.import java.io.IOException;   
7.import javax.imageio.ImageIO;   
8.import javax.swing.JFrame;   
9.import javax.swing.JPanel;   
10.import javax.swing.SwingUtilities;   
11.import chrriis.dj.nativeswing.swtimpl.NativeComponent;   
12.import chrriis.dj.nativeswing.swtimpl.NativeInterface;   
13.import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;   
14.import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;   
15.import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;   
16.public class Main extends JPanel {   
17.    /**  
18.     *   
19.     */  
20.    private static final long serialVersionUID = 1L;   
21.    // 行分隔符   
22.    final static public String LS = System.getProperty("line.separator", "\n");   
23.    // 文件分割符   
24.    final static public String FS = System.getProperty("file.separator", "\\");   
25.    //以javascript脚本获得网页全屏后大小   
26.    final static StringBuffer jsDimension;   
27.       
28.    static {   
29.        jsDimension = new StringBuffer();   
30.        jsDimension.append("var width = 0;").append(LS);   
31.        jsDimension.append("var height = 0;").append(LS);   
32.        jsDimension.append("if(document.documentElement) {").append(LS);   
33.        jsDimension.append(   
34.                        "  width = Math.max(width, document.documentElement.scrollWidth);")   
35.                .append(LS);   
36.        jsDimension.append(   
37.                        "  height = Math.max(height, document.documentElement.scrollHeight);")   
38.                .append(LS);   
39.        jsDimension.append("}").append(LS);   
40.        jsDimension.append("if(self.innerWidth) {").append(LS);   
41.        jsDimension.append("  width = Math.max(width, self.innerWidth);")   
42.                .append(LS);   
43.        jsDimension.append("  height = Math.max(height, self.innerHeight);")   
44.                .append(LS);   
45.        jsDimension.append("}").append(LS);   
46.        jsDimension.append("if(document.body.scrollWidth) {").append(LS);   
47.        jsDimension.append(   
48.                "  width = Math.max(width, document.body.scrollWidth);")   
49.                .append(LS);   
50.        jsDimension.append(   
51.                "  height = Math.max(height, document.body.scrollHeight);")   
52.                .append(LS);   
53.        jsDimension.append("}").append(LS);   
54.        jsDimension.append("return width + ':' + height;");   
55.    }   
56.  //DJNativeSwing组件请于http://djproject.sourceforge.net/main/index.html下载   
57.    public Main(final String url, final int maxWidth, final int maxHeight) {   
58.        super(new BorderLayout());   
59.        JPanel webBrowserPanel = new JPanel(new BorderLayout());   
60.        final String fileName = System.currentTimeMillis() + ".jpg";   
61.        final JWebBrowser webBrowser = new JWebBrowser(null);   
62.        webBrowser.setBarsVisi