需要下载的包
两个lib
https://sourceforge.net/projects/djproject/files/DJ%20Native%20Swing/1.0.3%20preview/
还有eclipse的plugin里面的swt,可以版本不一样,略有区别
org.eclipse.swt.win32.win32.x86_64_3.106.0.v20170608-0516.jar
代码如下,这个是命令行的工具,可以再用java调用命令行,实现批量转换,目前只能支持文件与文件的格式转换..
package djnativeswing;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
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 MainView extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
final static StringBuffer jsDimension;
static {
jsDimension = new StringBuffer();
jsDimension.append("var width = 0;");
jsDimension.append("var height = 0;");
jsDimension.append("if(document.body.scrollWidth) {");
jsDimension.append(" width = Math.max(width, document.body.scrollWidth);");
jsDimension.append(" height = Math.max(height, document.body.scrollHeight);");
jsDimension.append("}");
jsDimension.append("return width + ':' + height;");
}
public MainView(String htmlFileName, String pngFileName) throws UnsupportedEncodingException, IOException {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1024, 768);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JWebBrowser webbrowser = new JWebBrowser();
webbrowser.setBounds(0, 0, 1024, 768);
String html=new String(Files.readAllBytes(Paths.get(htmlFileName)),"utf-8");
webbrowser.setHTMLContent(html);
//webbrowser.navigate("https://blog.csdn.net/redlevin");
webbrowser.setButtonBarVisible(false);
webbrowser.setMenuBarVisible(false);
webbrowser.setBarsVisible(false);
webbrowser.setStatusBarVisible(false);
webbrowser.addWebBrowserListener(new WebBrowserAdapter() {
public void loadingProgressChanged(WebBrowserEvent e) {
JWebBrowser b = e.getWebBrowser();
if (b.getLoadingProgress() == 100) {
String result = (String) b.executeJavascriptWithResult(jsDimension.toString());
String[] resultWidthHeight = result.split(":");
NativeComponent nativeComponent = b.getNativeComponent();
Dimension imageSize = new Dimension(Integer.parseInt(resultWidthHeight[0]),
Integer.parseInt(resultWidthHeight[1]));
nativeComponent.setSize(imageSize);
BufferedImage image = new BufferedImage(imageSize.width, imageSize.height,
BufferedImage.TYPE_INT_RGB);
nativeComponent.paintComponent(image);
try {
ImageIO.write(image, "png", new File(pngFileName));
} catch (IOException ex) {
ex.printStackTrace();
}
System.exit(0);
}
}
});
contentPane.add(webbrowser);
}
public static void main(String[] args) {
if(args.length>=2) {
NativeInterface.open();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
MainView frame = new MainView(args[0], args[1]);
frame.invalidate();
frame.pack();
frame.setVisible(false);
} catch (Exception e) {
e.printStackTrace();
}
}
});
NativeInterface.runEventPump();
}
}
}
参考了一些内容