- 只有这个支持js,css,但是网页超过一定高度,就会截取一半;
- 如果是接口调用的话,因为是线程,所以这个只能运行一次就自动关闭了;使用main方法运行就没有问题
- 引入依赖包:
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>DJNativeSwing</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>DJNativeSwing-SWT</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.swt.org.eclipse.swt.win32.win32.x86_64.4.3.swt</groupId>
<artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
<version>4.3</version>
</dependency>
- 案例:
1.首先在springboot启动页上重写方法:
@SpringBootApplication
public class DjnativeswingApplication {
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(DjnativeswingApplication.class);
ApplicationContext applicationContext = builder.headless(false).run(args);
applicationContext.getBean(TestController.class);
// SpringApplication.run(DjnativeswingApplication.class, args);
}
}
2.写一个工具类:
public class DjNativeSwingUtil 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", "//");
// 当网页超出目标大小时 截取
final static public int maxWidth = 2000;
final static public int maxHeight = 1600;
// private static class ExitTrappedException extends SecurityException { }
// private static void forbidSystemExitCall() {
// final SecurityManager securityManager = new SecurityManager() {
// public void checkPermission( Permission permission ) {
// if( "exitVM".equals( permission.getName() ) ) {
// throw new ExitTrappedException() ;
// }
// }
// } ;
// System.setSecurityManager( securityManager ) ;
// }
public DjNativeSwing(final String file, final String url, final String WithResult) {
super(new BorderLayout());
JPanel webBrowserPanel = new JPanel(new BorderLayout());
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());
webBrowser.addWebBrowserListener(new WebBrowserAdapter() {
// 监听加载进度
public void loadingProgressChanged(WebBrowserEvent e) {
// 当加载完毕时
if (e.getWebBrowser().getLoadingProgress() == 100) {
String result = (String) webBrowser.executeJavascriptWithResult(WithResult);
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, "png", new File(file));
} catch (IOException ex) {
ex.printStackTrace();
}
// 退出操作
System.exit(0);//关闭jvm同时也关闭了tomcat
}
}
});
add(panel, BorderLayout.SOUTH);
}
// 以javascript脚本获得网页全屏后大小
public static String getScreenWidthHeight() {
StringBuffer 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;");
return jsDimension.toString();
}
}
3. 截取的代码:
@Configuration
public class TestController {
public static boolean printUrlScreen2jpg(final String file, final String url, final int width, final int height) {
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String withResult = "var width = " + width + ";var height = " + height + ";return width +':' + height;";
if (width == 0 || height == 0)
withResult = DjNativeSwing.getScreenWidthHeight();
JFrame frame = new JFrame("网页截图");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 加载指定页面,最大保存为640x480的截图
frame.getContentPane().add(new DjNativeSwing(file, url, withResult), BorderLayout.CENTER);
frame.setSize(640, 800);
// 仅初始化,但不显示
frame.invalidate();
frame.pack();
frame.setVisible(false);
}
});
NativeInterface.runEventPump();
return true;
}
}
4.接口调用:
@RequestMapping(value = "/htmltest", produces = "application/json;charset=UTF-8")
@ResponseBody
public String htmlGenerator() throws Exception{
TestController.printUrlScreen2jpg("G:/网页截图.png", "G:/index.html", 1000, 1300);
return null;
}