当前位置: 代码迷 >> Java Web开发 >> 有人做过用JWebBrowser网页截图吗?该怎么解决
  详细解决方案

有人做过用JWebBrowser网页截图吗?该怎么解决

热度:2321   发布时间:2013-02-25 21:12:49.0
有人做过用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下载   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.setBarsVisible(false);   63.        webBrowser.navigate(url);   64.        webBrowserPanel.add(webBrowser, BorderLayout.CENTER);   65.        add(webBrowserPanel, BorderLayout.CENTER);   66.        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));   67.        webBrowser.addWebBrowserListener(new WebBrowserAdapter() {   68.            // 监听加载进度   69.            public void loadingProgressChanged(WebBrowserEvent e) {   70.                // 当加载完毕时   71.                if (e.getWebBrowser().getLoadingProgress() == 100) {   72.                    String result = (String) webBrowser   73.                            .executeJavascriptWithResult(jsDimension.toString());   74.                    int index = result == null ? -1 : result.indexOf(":");   75.                    NativeComponent nativeComponent = webBrowser   76.                            .getNativeComponent();   77.                    Dimension originalSize = nativeComponent.getSize();   78.                    Dimension imageSize = new Dimension(Integer.parseInt(result   79.                            .substring(0, index)), Integer.parseInt(result   80.                            .substring(index + 1)));   81.                    imageSize.width = Math.max(originalSize.width,   82.                            imageSize.width + 50);   83.                    imageSize.height = Math.max(originalSize.height,   84.                            imageSize.height + 50);   85.                    nativeComponent.setSize(imageSize);   86.                    BufferedImage image = new BufferedImage(imageSize.width,   87.                            imageSize.height, BufferedImage.TYPE_INT_RGB);   88.                    nativeComponent.paintComponent(image);   89.                    nativeComponent.setSize(originalSize);   90.                    // 当网页超出目标大小时   91.                    if (imageSize.width > maxWidth   92.                            || imageSize.height > maxHeight) {   93.                        //截图部分图形   94.                        image = image.getSubimage(0, 0, maxWidth, maxHeight);   95.                        /*此部分为使用缩略图  96.                        int width = image.getWidth(), height = image  97.                            .getHeight();  98.                         AffineTransform tx = new AffineTransform();  99.                        tx.scale((double) maxWidth / width, (double) maxHeight  100.                                / height);  101.                        AffineTransformOp op = new AffineTransformOp(tx,  102.                                AffineTransformOp.TYPE_NEAREST_NEIGHBOR);  103.                        //缩小  104.                        image = op.filter(image, null);*/  105.                    }   106.                    try {   107.                        // 输出图像   108.                        ImageIO.write(image, "jpg", new File(fileName));   109.                    } catch (IOException ex) {   110.                        ex.printStackTrace();   111.                    }   112.                    // 退出操作   113.                    System.exit(0);   114.                }   115.            }   116.        }   117.        );   118.        add(panel, BorderLayout.SOUTH);   119.    }   120.    public static void main(String[] args) {   121.        NativeInterface.open();   122.        SwingUtilities.invokeLater(new Runnable() {   123.            public void run() {   124.                // SWT组件转Swing组件,不初始化父窗体将无法启动webBrowser   125.                JFrame frame = new JFrame("以DJ组件保存指定网页截图");   126.                // 加载指定页面,最大保存为640x480的截图   127.                frame.getContentPane().add(   128.                        new Main("http://blog.csdn.net/cping1982", 640, 480),   129.                        BorderLayout.CENTER);   130.                frame.setSize(800, 600);   131.                // 仅初始化,但不显示   132.                frame.invalidate();   133.                frame.pack();   134.                frame.setVisible(false);   135.            }   136.        });   137.        NativeInterface.runEventPump();   138.    }   139.}
  相关解决方案