Java通过URL截取网页快照并保存到指定目录--项目中不能连续截取

mingyue168 2014-04-18 02:54:01
[b]问题:1、在项目中只能截一次图,第一次截图后再进行其它操作,项目不是停掉【可能是这个语句引起:System.exit(0);】,就是一直刷新中。。。不能进行操作了。。。,怎么可以在项目中根据输入不同的URL连续的进行截图操作?求高手指点。。。

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 HTMLUtils 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 jsScreemSize;

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 HTMLUtils(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(jsScreemSize.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 {
// 输出图像
System.out.println(fileName);
ImageIO.write(image, "jpg", new File(fileName));
} catch (IOException ex) {
ex.printStackTrace();
}
// 退出操作
System.exit(0);//Myeclipse中第一次调用正常,第二次调用直接把Myeclipse中项目停了
}
}
}

);
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的截图
//实际项目中传入URL参数,根据不同参数截取不同网页快照,保存地址也可以在构造器中多设置一个参数,保存到指定目录
frame.getContentPane().add(new HTMLUtils("http://www.baidu.com", 640, 480), BorderLayout.CENTER);
frame.setSize(1024, 1024);

// 仅初始化,但不显示
frame.invalidate();
frame.pack();
frame.setVisible(false);
}
});
NativeInterface.runEventPump();
}

}
[/b]
...全文
417 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
小斯AN 2016-12-26
  • 打赏
  • 举报
回复
请问楼主解决了吗?
小律律 2014-04-18
  • 打赏
  • 举报
回复
不懂帮顶。。。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧