使用DJNativeSwing实现网页截图System.exit(0);把tomcat也退出了

gefeifei2006314 2015-06-26 11:15:26
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;");
}
//DJNativeSwing组件请于http://djproject.sourceforge.net/main/index.html下载
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组件保存指定网页截图");
// 加载指定页面,最大保存为640x480的截图
frame.getContentPane().add(
new Main("http://blog.csdn.net/cping1982", 640, 480),
BorderLayout.CENTER);
frame.setSize(800, 600);
// 仅初始化,但不显示
frame.invalidate();
frame.pack();
frame.setVisible(false);
}
});
NativeInterface.runEventPump();
}
}

System.exit(0);把tomcat也退出了,怎么解决。
...全文
283 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
gefeifei2006314 2015-06-26
  • 打赏
  • 举报
回复
我是把这个网页截图功能放在一个线程里的,但是发现把System.exit(0);注释后,这个java进程一直存在,线程也不再执行了。
内容概要:本文研究了基于Q-Learning自适应强化学习的PID控制器在自主水下航行器(AUV)运动控制中的应用,旨在提升其在复杂海洋环境下的控制性能。通过建立AUV的动力学模型,将Q-Learning算法与传统PID控制相结合,实现控制器参数的在线自适应优化,有效增强了系统的响应速度、稳定性和抗干扰能力。研究采用Matlab进行仿真验证,结果表明该方法在轨迹跟踪与姿态控制方面具有优越表现,为水下机器人智能控制提供了可行的技术路径与理论支持。; 适合人群:具备自动控制理论、强化学习或水下机器人建模背景,从事相关科研工作的研究人员、高校研究生及工程技术开发者。; 使用场景及目标:①应用于高精度、强鲁棒性的水下机器人运动控制系统设计;②开展智能PID控制器在非线性、时变系统中的参数自整定研究;③作为强化学习在复杂工业控制场景中落地的典型案例参考,推动算法与工程实践融合。; 阅读建议:建议结合提供的Matlab代码进行仿真实验,重点剖析Q-Learning与PID融合的架构设计、奖励函数设定及参数收敛过程,可进一步拓展至与其他智能优化算法(如DQN、Actor-Critic)的对比研究,深化对自适应控制机制的理解。
已经博主授权,源码转载自 https://pan.quark.cn/s/a4b39357ea24 Matlab 被视为一种功能卓越的编程平台,特别是在数值运算和数据分析方面展现出广泛的应用价值。在应对多元非线性回归的挑战时,Matlab 拥备多种工具和函数,支持用户对复杂的数据进行构建模型和适配。本指南将重点阐释在 Matlab 环境下如何执行多元非线性回归分析。 我们需要掌握三种关键的回归指令: 1. `polyfit(x,y,n)`:此函数适用于拟合一元幂函数,能够导出一个多项式模型。例如,若知晓数据呈现二次函数形态,可选择`n=2`来适配一个二次曲线。 2. `regress(y,x)`:这是一个多元线性回归指令,能够处理多个自变量对因变量的影响。即便数据并非完全呈现线性特征,该函数也能提供一个线性近似。 3. `nlinfit(x,y,’fun’,beta0)`:这是最为通用的非线性回归指令,适用于适配任何类型的函数,无论是一元或多元,前提是用户能够定义函数形式(`fun`参数)。 回归分析的核心在于寻找一组最优的系数,使得模型能够最恰当地描述数据。这通常涉及选择一个合适的函数形态,随后通过最小化误差平方和来估算系数。在Matlab中,`regress`指令主要用于处理线性模型,而`nlinfit`则处理更为复杂的非线性模型。 对于多元线性回归模型,我们可以用如下形式进行表述: \[ y = \beta_0 + \beta_1x_1 + \beta_2x_2 + \cdots + \beta_px_p + \epsilon \] 其中,\( y \)是因变量,\( x_1, x_2, \ldots, x_p \)是自变量,\( \beta_0, \beta_1, ...
内容概要:本文系统阐述了基于BP神经网络的语音特征信号分类方法,并提供了完整的Matlab代码实现。研究围绕语音信号的数据预处理、特征提取、神经网络结构设计、模型训练与分类测试等核心环节展开,详细展示了BP神经网络在语音识别任务中的应用流程。通过构建多层前馈网络模型,利用误差反向传播算法优化权重,实现了对不同语音类别特征的高效分类,具有较强的工程实践价值和技术可复现性。; 适合人群:具备信号处理与机器学习基础知识,熟悉Matlab编程语言,从事语音识别、模式识别、人工智能等相关领域研究的学生及科研人员;特别适用于开展课程设计、毕业设计或科研项目的初级与中级研究人员。; 使用场景及目标:①深入理解BP神经网络的基本原理及其在语音信号分类中的具体实现过程;②掌握Matlab环境下语音信号特征提取与分类模型构建的全流程技术;③为语音识别、人机交互、智能听觉系统等实际应用场景提供可靠的算法基础与代码参考。; 阅读建议:建议读者结合所提供的Matlab代码逐行分析其实现逻辑,重点关注数据输入格式、网络参数初始化、训练迭代过程及分类性能评估;可通过调整隐含层节点数、学习率、激活函数等超参数,观察模型收敛性与分类准确率的变化,从而深化对神经网络调参策略的理解。

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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