Java实现网页全屏截图并保存图片实例代码介绍



Java实现网页全屏截图并保存图片实例代码介绍。

所需jar包和完整代码下载地址:http://download.csdn.net/detail/zajin/6705271

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. package shot;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.awt.FlowLayout;
  5. import java.awt.geom.AffineTransform;
  6. import java.awt.image.AffineTransformOp;
  7. import javax.swing.SwingUtilities;
  8. import chrriis.dj.nativeswing.swtimpl.NativeComponent;
  9. import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;
  10. /**
  11. * @Author Rain
  12. * @Date   Dec 12, 2013
  13. * @Email  2527350@QQ.com
  14. * @Discription  undefined
  15. */
  16. public class Main extends JPanel {
  17.     private static final long serialVersionUID = 1L;
  18.     // 行分隔符
  19.     final static public String LS = System.getProperty(“line.separator”, “\n”);
  20.     // 文件分割符
  21.     final static public String FS = System.getProperty(“file.separator”, “\\”);
  22.     //以javascript脚本获得网页全屏后大小
  23.     final static StringBuffer jsDimension;
  24.     static {
  25.         jsDimension = new StringBuffer();
  26.         jsDimension.append(“var width = 0;”).append(LS);
  27.         jsDimension.append(“var height = 0;”).append(LS);
  28.         jsDimension.append(“if(document.documentElement) {“).append(LS);
  29.         jsDimension.append(“  width = Math.max(width, document.documentElement.scrollWidth);”).append(LS);
  30.         jsDimension.append(“  height = Math.max(height, document.documentElement.scrollHeight);”).append(LS);
  31.         jsDimension.append(“}”).append(LS);
  32.         jsDimension.append(“if(self.innerWidth) {“).append(LS);
  33.         jsDimension.append(“  width = Math.max(width, self.innerWidth);”).append(LS);
  34.         jsDimension.append(“  height = Math.max(height, self.innerHeight);”).append(LS);
  35.         jsDimension.append(“}”).append(LS);
  36.         jsDimension.append(“if(document.body.scrollWidth) {“).append(LS);
  37.         jsDimension.append(“  width = Math.max(width, document.body.scrollWidth);”).append(LS);
  38.         jsDimension.append(“  height = Math.max(height, document.body.scrollHeight);”).append(LS);
  39.         jsDimension.append(“}”).append(LS);
  40.         jsDimension.append(“return width + ‘:’ + height;”);
  41.     }
  42.     public Main(final String url, final int maxWidth, final int maxHeight) {
  43.         super(new BorderLayout());
  44.         JPanel webBrowserPanel = new JPanel(new BorderLayout());
  45.         final String fileName = System.currentTimeMillis() + “.jpg”;
  46.         final JWebBrowser webBrowser = new JWebBrowser(null);
  47.         webBrowser.setBarsVisible(false);
  48.         webBrowser.navigate(url);
  49.         webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
  50.         add(webBrowserPanel, BorderLayout.CENTER);
  51.         JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
  52.         webBrowser.addWebBrowserListener(new WebBrowserAdapter() {
  53.             // 监听加载进度
  54.             public void loadingProgressChanged(WebBrowserEvent e) {
  55.                 // 当加载完毕时
  56.                 if (e.getWebBrowser().getLoadingProgress() == 100) {
  57.                     String result = (String) webBrowser.executeJavascriptWithResult(jsDimension.toString());
  58.                     int index = result == null ? -1 : result.indexOf(“:”);
  59.                     NativeComponent nativeComponent = webBrowser.getNativeComponent();