`
中华国锋
  • 浏览: 41612 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java代码对浏览器进行快照

 
阅读更多

通过java 代码进行浏览器展示内容进行快照,支持有滚动条的页面截屏


项目下载位置。


1.代码如下:


package test;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;


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 TestJPanel 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("if(document.readyState==\"complete\"){").append(LS);
//jsDimension.append("alert(\"当前页面已加载完成!\");").append(LS);
jsDimension.append("return width + ':' + height;").append(LS);

//jsDimension.append("}").append(LS);


System.out.println("----------jsDimension:"+jsDimension.toString());
}


public TestJPanel(final String url, final int maxWidth, final int maxHeight) {
super(new BorderLayout());
JPanel webBrowserPanel = new JPanel(new BorderLayout());
final String fileName = "D:\\test\\"+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) {
// 当加载完毕时
System.out.println("---------e.getWebBrowser().getLoadingProgress():"+e.getWebBrowser().getLoadingProgress());
if (e.getWebBrowser().getLoadingProgress() == 100) {
try {
String html = webBrowser.getHTMLContent();
System.out.println("-----------------页面代码:"+html);
// 具体的秒数需要根据网速等调整
Thread.sleep(4* 5 * 1000);
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);

System.out.println("----------imageSize.width:"+imageSize.width+"-------------maxWidth:"+maxWidth+"---------imageSize.height:"+imageSize.height+"--------maxHeight:"+maxHeight);
// 当网页超出目标大小时
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 = image.getSubimage(0, 0, width, height);
//缩小图片
//image = op.filter(image, null);
}

// 输出图像
ImageIO.write(image, "jpg", new File(fileName));
} catch (Exception ex) {
ex.printStackTrace();
}
// 退出操作
System.exit(0);
}
}
}


);
add(panel, BorderLayout.SOUTH);


}


public static void init(String host, int port, final String username, final String password) {
/*Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,new String(password).toCharArray());
}
});*/


System.setProperty("http.proxyType", "4");
System.setProperty("http.proxyPort", Integer.toString(port));
System.setProperty("http.proxyHost", host);
System.setProperty("http.proxySet", "true");
}

public static void main(String[] args) {
String url = "http://www.piccnet.com.cn/zhaopin/index.jsp";
String proxy = "172.16.251.58";
int port = 3128;
String username = "71000731";
String password = "DC664750";

init(proxy, port, username, password);


System.out.println("---------------start----------");
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// SWT组件转Swing组件,不初始化父窗体将无法启动webBrowser
JFrame frame = new JFrame("以DJ组件保存指定网页截图");
String url = "";
url = "http://www.piccnet.com.cn/zhaopin/index.jsp";
//url = "http://www.google.com.hk/webhp?hl=zh-CN&tab=Tw";
//url = "http://123.sogou.com/";
// 加载google,最大保存为640x480的截图
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int)d.getWidth();
int height = (int) d.getHeight();
TestJPanel test = new TestJPanel (url, width,height);
//TestJPanel test = new TestJPanel("http://www.csdn.net/", 1024,768)
frame.getContentPane().add(test, BorderLayout.CENTER);
frame.setSize(800, 600);
// 仅初始化,但不显示
frame.invalidate();
frame.pack();
frame.setVisible(false);//设置是否可见

System.out.println("---------------end----------");
}
});
NativeInterface.runEventPump();


}


}

分享到:
评论

相关推荐

    snap.zip网页快照功能代码

    (一)PhantomJS是可使用JavaScript编写脚本的无头Web浏览器。它可以在Windows,macOS,Linux和FreeBSD上运行。使用QtWebKit作为后端,它为各种Web标准提供了快速的本机支持:DOM处理,CSS选择器,JSON,Canvas和SVG...

    webjdb:在浏览器中进行Java调试

    在浏览器中进行Java调试。 使用和websocket,以便您可以从Web浏览器中调试Java程序。 下面的屏幕快照显示了此Java的浏览器内调试会话: public static void main(String[] args) { int test1 = 10; int test2 ...

    Java实现发送邮件.pdf

    电子邮件的应用非常广泛,常见的如在某网站注册了...很显然这些应用不可能和我们自己平时发邮件一样,先打开浏览器,登录邮箱,创建邮件再发送.简单介绍如何通过 Java 代码来创建电子邮件,并连接邮件服务器发送邮件。

    Ranorex用户指南

    在代码模块中访问屏幕快照 代码模块中使用变量 在测试用例中使用代码模块 课程8:报告 阅读Ranorex报告 报告级别 记录特定信息 更新自定义报告格式 创建一个自定义的报告模板 [课程 9: Ranorex Spy] 跟踪UI元素...

    Visual Assist v10.2.1438

    拓展了基本编辑:对编辑器进行了增强,编辑代码更加迅速。含有Surround selections,multiple clipboards. Sort lines。 适合您个人风格的配置特色:细化选项对话框,定义Visual Assist X特性以适应您的编程习惯。...

    calendar源码java-k5ncal:利用现有标准(iCalendar)的基于Java的日历应用程序,以创建开源的跨平台多用户桌面日历应

    要构建源代码,您将需要对提供的build.xml文件使用ant。 要使用ant进行构建: ant 此构建过程将创建以下jar文件: dist/k5nCal-0.9.9-SNAPSHOT.jar 运行应用 要运行k5nCal应用程序,您可以在文件浏览器(Windows Exp

    selenium-webdriver-java-task

    Web自动化测试套件这个自动化的Web测试套件涵盖了代码质询文件中提到的所有测试用例。使用的工具,框架,编程语言: Intellij IDE,maven,Selenium Webdriver,TestNG,Java 8 +,Hamcrest,页面对象模型(POM)...

    Whole.Tomato.Visual.Assist.X.10.7.1925.0 破解补丁 dll Cracked 最新破解版

    vc助手 Visual assist的2012.12.14发行的最新1925版本的补丁和原版的安装文件,这个版本... 拓展了基本编辑:对编辑器进行了增强,编辑代码更加迅速。含有Surround selections,multiple clipboards. Sort lines。

    Visual Assist X 10.4

    拓展了基本编辑:对编辑器进行了增强,编辑代码更加迅速。含有Surround selections,multiple clipboards. Sort lines。 适合您个人风格的配置特色:细化选项对话框,定义Visual Assist X特性以适应您的编程习惯。...

    备忘录java源码-javascript-memory-leakage:检测导致垃圾收集JavaScript应用程序中的内存泄漏的源代码的简单

    备注java原始码寻找JavaScript应用程序中的内存泄漏 垃圾回收是程序通过回收未在程序中使用的对象占用的内存来执行自动内存管理的过程。 内存泄漏是指程序不需要的内存未返回操作系统或空闲内存池时的情况。 有关更...

    百歌搜索引擎tomcat嵌入版(Baioogle-SearchEngine Embed in Tomcat)

    2008年5月份,因学习《信息检索》课程,本人利用java的开源搜索引擎库lucene,以及结合ajax技术google-suggest功能,模仿baidu、google的基本风格做了个“Baioogle-SearchEngine(百歌搜索引擎)”程序。 (源代码见...

    Notepad++ 7.5 Final 中文绿色便携版

    2、对于HTML网页编程代码,可直接选择在不同的浏览器中打开查看,以方便进行调试; 3、自动检测文件类型,根据关键字显示节点,节点可自由折叠/打开,可显示缩进引导线,使代码富有层次感; 4、可打开双窗口,在分...

    Visual assist X 1626(带注册机)

     拓展了基本编辑:对编辑器进行了增强,编辑代码更加迅速。含有Surround selections,multiple clipboards. Sort lines。  适合您个人风格的配置特色:细化选项对话框,定义Visual Assist X特性以适应您的编程习惯...

    Whole.Tomato.Visual.Assist.X.10.6.1823 2010年6月最新破解版

     拓展了基本编辑:对编辑器进行了增强,编辑代码更加迅速。含有Surround selections,multiple clipboards. Sort lines。  适合您个人风格的配置特色:细化选项对话框,定义Visual Assist X特性以适应您的编程习惯...

    Whole.Tomato.Visual.Assist.X.v10.6.1822 2010年4月最新破解补丁

     拓展了基本编辑:对编辑器进行了增强,编辑代码更加迅速。含有Surround selections,multiple clipboards. Sort lines。  适合您个人风格的配置特色:细化选项对话框,定义Visual Assist X特性以适应您的编程习惯...

    Whole.Tomato.Visual.Assist.X.10.7.1925.0 2012年12月14日 破解补丁 dll Cracked 最新破解版

     拓展了基本编辑:对编辑器进行了增强,编辑代码更加迅速。含有Surround selections,multiple clipboards. Sort lines。  适合您个人风格的配置特色:细化选项对话框,定义Visual Assist X特性以适应您的编程...

    Baioogle-SearchEngine(百歌搜索引擎)

    3.如果要对站内文件进行索引,在运行建立索引命令时,最好将相应.bat命令文件中的路径参数改为绝对路径 4.由于参考了部分开源技术,故在开发过程中尽量保留原作者信息,如在版权等问题上有处理不当之处,纯属学习...

    android-basic-sample:本示例说明如何在Android应用程序中使用AnyChart图表库

    在您的Android设备上运行要在您的Android设备上运行AnyChart示例,只需在设备浏览器中打开 URL。 该示例应用程序将自动下载并开始安装过程。 以下屏幕快照显示了安装过程的所有步骤:在桌面上运行要在开发环境中运行...

    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...

Global site tag (gtag.js) - Google Analytics