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

gefeifei2006314 gefeifei2006314 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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也退出了,怎么解决。
...全文

相关推荐

高校技术转移办公室人员如何高效开展技术需求挖掘工作?.docx

科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。

BIZKEEN

科技中介服务机构如何提升服务的专业性与用户粘性?.docx

科技中介服务机构如何提升服务的专业性与用户粘性?

BIZKEEN

高校技术转移办公室人员在组织科研成果展示时,如何提升推介材料的专业性与吸引力?.docx

科易网基于AI模型+行业大数据,在技术转移、成果转化、技术经纪、知识产权、产业创新、科技招商等垂直领域的多样化应用场景,推出科技创新领域的AI+工具与数智化解决方案,推动科技创新与产业创新智能化发展。

BIZKEEN

三相并网逆变器无功功率扰动+孤岛检测控制算法仿真【参考文献+仿真】

内容概要:本文系统梳理了一套面向电力电子与新能源并网领域的仿真研究资源,聚焦三相并网逆变器的关键控制技术,涵盖无功功率扰动、孤岛检测算法、低电压穿越(LVRT)、中点电位平衡、虚拟同步发电机(VSG)、下垂控制、模型预测控制(MPC)、准PR控制及LCL滤波器设计等核心技术。资源包含多种逆变器拓扑结构(如T型三电平、Z源、ANPC、四桥臂等)的Simulink仿真模型,并结合扫频法进行正负序阻抗建模与稳定性分析,强调通过复现经典科研案例提升实际科研能力,适用于电力系统稳定性、微电网运行与可再生能源并网等方向的研究与验证。; 适合人群:电力电子、电气工程及其自动化、新能源科学与工程等相关专业的高校研究生、科研人员及从事光伏、储能逆变器开发的工程技术人员,需具备MATLAB/Simulink仿真基础和一定的电力系统控制理论知识。; 使用场景及目标:① 掌握三相并网逆变器在孤岛检测、无功调节、不平衡电网等复杂工况下的先进控制策略设计与仿真方法;② 借助提供的仿真模型与参考文献快速搭建科研平台,用于论文复现、课题开发或项目验证;③ 深入理解VSG、MPC、阻抗建模与下垂控制等关键技术的实现机制与工程应用路径。; 阅读建议:建议结合文中提供的网盘链接与公众号“荔枝科研社”获取完整仿真模型与参考资料,按照目录顺序循序渐进学习,注重理论推导与仿真实践相结合,优先复现基础模型再进行参数优化与创新拓展。

程序辅导帮

香橙派5-uefi-edk2-spi-flash镜像

香橙派5-uefi-edk2-spi-flash镜像

星际海的蜗牛
评论 1
收藏 已收藏
分享
返回顶部
gefeifei2006314
Java 51,407 成员 86,130 内容