`
oywl2008
  • 浏览: 1001436 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

网页快照 java 实现 .

 
阅读更多
/**
 * 
 */
package com.test;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import chrriis.dj.nativeswing.swtimpl.NativeComponent;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;

public class Main extends JPanel {
 /**
  * 
  */
 private static final long serialVersionUID = 1L;

 // 行分隔符
 final static public String LS = System.getProperty("line.separator", "/n");

 // 文件分割符
 final static public String FS = System.getProperty("file.separator", "//");

 //以javascript脚本获得网页全屏后大小
 final static StringBuffer jsDimension;
 
 static {
  jsDimension = new StringBuffer();
  jsDimension.append("var width = 0;").append(LS);
  jsDimension.append("var height = 0;").append(LS);
  jsDimension.append("if(document.documentElement) {").append(LS);
  jsDimension.append(
      "  width = Math.max(width, document.documentElement.scrollWidth);")
    .append(LS);
  jsDimension.append(
      "  height = Math.max(height, document.documentElement.scrollHeight);")
    .append(LS);
  jsDimension.append("}").append(LS);
  jsDimension.append("if(self.innerWidth) {").append(LS);
  jsDimension.append("  width = Math.max(width, self.innerWidth);")
    .append(LS);
  jsDimension.append("  height = Math.max(height, self.innerHeight);")
    .append(LS);
  jsDimension.append("}").append(LS);
  jsDimension.append("if(document.body.scrollWidth) {").append(LS);
  jsDimension.append(
    "  width = Math.max(width, document.body.scrollWidth);")
    .append(LS);
  jsDimension.append(
    "  height = Math.max(height, document.body.scrollHeight);")
    .append(LS);
  jsDimension.append("}").append(LS);
  jsDimension.append("return width + ':' + height;");
 }

 public Main(final String url, final int maxWidth, final int maxHeight) {
  super(new BorderLayout());
  JPanel webBrowserPanel = new JPanel(new BorderLayout());
  final String fileName = System.currentTimeMillis() + ".jpg";
  final JWebBrowser webBrowser = new JWebBrowser(null);
  webBrowser.setBarsVisible(false);
  webBrowser.navigate(url);
  webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
  add(webBrowserPanel, BorderLayout.CENTER);

  JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));

  webBrowser.addWebBrowserListener(new WebBrowserAdapter() {

   // 监听加载进度
   public void loadingProgressChanged(WebBrowserEvent e) {
    // 当加载完毕时
    if (e.getWebBrowser().getLoadingProgress() == 100) {
     String result = (String) webBrowser
       .executeJavascriptWithResult(jsDimension.toString());
     int index = result == null ? -1 : result.indexOf(":");
     NativeComponent nativeComponent = webBrowser
       .getNativeComponent();
     Dimension originalSize = nativeComponent.getSize();
     Dimension imageSize = new Dimension(Integer.parseInt(result
       .substring(0, index)), Integer.parseInt(result
       .substring(index + 1)));
     imageSize.width = Math.max(originalSize.width,
       imageSize.width + 50);
     imageSize.height = Math.max(originalSize.height,
       imageSize.height + 50);
     nativeComponent.setSize(imageSize);
     BufferedImage image = new BufferedImage(imageSize.width,
       imageSize.height, BufferedImage.TYPE_INT_RGB);
     nativeComponent.paintComponent(image);
     nativeComponent.setSize(originalSize);
     // 当网页超出目标大小时
     if (imageSize.width > maxWidth
       || imageSize.height > maxHeight) {
      //截图部分图形
      image = image.getSubimage(0, 0, maxWidth, maxHeight);
      /*此部分为使用缩略图
      int width = image.getWidth(), height = image
       .getHeight();
       AffineTransform tx = new AffineTransform();
      tx.scale((double) maxWidth / width, (double) maxHeight
        / height);
      AffineTransformOp op = new AffineTransformOp(tx,
        AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
      //缩小
      image = op.filter(image, null);*/
     }
     try {
      // 输出图像
      ImageIO.write(image, "jpg", new File(fileName));
     } catch (IOException ex) {
      ex.printStackTrace();
     }
     // 退出操作
     System.exit(0);
    }
   }
  }

  );
  add(panel, BorderLayout.SOUTH);

 }

 public static void main(String[] args) {
  NativeInterface.open();
  SwingUtilities.invokeLater(new Runnable() {
   public void run() {
    // SWT组件转Swing组件,不初始化父窗体将无法启动webBrowser
    JFrame frame = new JFrame("以DJ组件保存指定网页截图");
    // 加载google,最大保存为640x480的截图
    frame.getContentPane().add(
      new Main("www.ifeng.com", 1024, 768),
      BorderLayout.CENTER);
    frame.setSize(800, 600);
    // 仅初始化,但不显示
    frame.invalidate();
    frame.pack();
    frame.setVisible(false);
   }
  });
  NativeInterface.runEventPump();
 }

}

 

分享到:
评论

相关推荐

    java 实现整张网页快照

    解压后先把依赖包加到pom中,然后再执行代码测试。利用Jwebbrower实现网页快照(完整的),很多查到的资料要么只有电脑桌面这么大要么就截不完整。本工具截一条百度新闻页实测效果还是比较好的。

    纯Java实现整个网页截图项目源代码

    精品必须10分啊,里面包含了java项目源代码和所需jar包,使用了DJNativeSwing组件技术实现。

    JAVA线程根据给定URL生成网页快照

    NULL 博文链接:https://gqsunrise.iteye.com/blog/1973158

    snap.zip网页快照功能代码

    (一)PhantomJS是可使用JavaScript编写脚本的无头Web浏览器。它可以在Windows,macOS,Linux...(三)本例子中,提供了屏幕截图功能的使用,可以实现网页快照的功能,输入一个网址,可以获取到该网址的快照截图功能。

    javajava概要设计方案.doc

    总体设计 2.1需求规定 2.1.1系统功能 本系统功能定位为为用户提供网页搜索功能,通过简单的提交关键字,实现页面检索 2.1.2系统性能 索引时间 检索时间 不同关键词时间不同) 2.1.2.1精度 支持中文检索,对了以索引...

    多线程搜索引擎java实现源代码

    java代码实现搜索引擎功能,将快照存在本地服务器,通过索引获取要查询的网页

    Tabletop-Pendragon-Model:Pendragon 模型库

    Tabletop 的 Pendragon 模型Pendragon 模型 API 的默认实现。网页目前该项目没有网页。文档网页该项目有一个和其中包含来自最新版本的信息。 还有一个和一个,从最新的开发快照生成。地位该项目仍在开发中,因此预计...

    Google Android SDK开发范例大全(第3版) 1/5

    Web Service存取服务:内嵌网页浏览器、Ajax网页特效、手机气象局、网络播放mp3、网络安装apk程序、远程下载手机铃声、XML-RPC移动博客发布器、手机RSS阅读器、地震速报、网页快照等。 完备的Google网络服务:Google...

    Google Android SDK开发范例大全(第3版) 4/5

    Web Service存取服务:内嵌网页浏览器、Ajax网页特效、手机气象局、网络播放mp3、网络安装apk程序、远程下载手机铃声、XML-RPC移动博客发布器、手机RSS阅读器、地震速报、网页快照等。 完备的Google网络服务:Google...

    Google Android SDK开发范例大全(第3版) 3/5

    Web Service存取服务:内嵌网页浏览器、Ajax网页特效、手机气象局、网络播放mp3、网络安装apk程序、远程下载手机铃声、XML-RPC移动博客发布器、手机RSS阅读器、地震速报、网页快照等。 完备的Google网络服务:Google...

    Google Android SDK开发范例大全(第3版) 5/5

    Web Service存取服务:内嵌网页浏览器、Ajax网页特效、手机气象局、网络播放mp3、网络安装apk程序、远程下载手机铃声、XML-RPC移动博客发布器、手机RSS阅读器、地震速报、网页快照等。 完备的Google网络服务:Google...

    SQLServer2008查询性能优化 2/2

    书名: SQLServer2008查询性能优化 作者: 弗里奇(Grant Fritchey) 出版社: 人民邮电出版社 出版日期: 2010年8月1日 ISBN: 9787115230294 编辑推荐 《SQL Server 2008查询性能...12.4.5 快照(Snapshot) 343 12.5...

    SQLServer2008查询性能优化 1/2

    书名: SQLServer2008查询性能优化 作者: 弗里奇(Grant Fritchey) 出版社: 人民邮电出版社 出版日期: 2010年8月1日 ISBN: 9787115230294 编辑推荐 《SQL Server 2008查询性能...12.4.5 快照(Snapshot) 343 12.5...

Global site tag (gtag.js) - Google Analytics