JAVA将HTML转化图片最靠谱的方法
2017-06-08 09:44
489 查看
经过比对分析发现使用Java内置的浏览器DJNativeSwing做,是最好的解决方法,下面是源码
用到的包如下:
DJNativeSwing-SWT.jar
DJNativeSwing.jar
org.eclipse.swt.win32.win32.x86_3.102.0.v20130605- 1544.jar (32位)
org.eclipse.swt.win32.win32.x86_64_3.100.1.v4236b.jar
(64位)
用到的包如下:
DJNativeSwing-SWT.jar
DJNativeSwing.jar
org.eclipse.swt.win32.win32.x86_3.102.0.v20130605- 1544.jar (32位)
org.eclipse.swt.win32.win32.x86_64_3.100.1.v4236b.jar
(64位)
public class TestMain extends JPanel { /** * DJNativeSwing-SWT.jar */ 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<a href="http://lib.csdn.net/base/dotnet" class='replace_word' title=".NET知识库" target='_blank' style='color:#df3434; font-weight:bold;'>.NET</a>/main/index.html下载 public TestMain(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 TestMain("http://www.huanqiu.com/", 1024, 768), BorderLayout.CENTER); frame.setSize(1024, 768); // 仅初始化,但不显示 frame.invalidate(); frame.pack(); frame.setVisible(false); } }); NativeInterface.runEventPump(); } }
相关文章推荐
- java把html转化为图片,并且和另一张图片合成新的图片
- HTML转图片以及其中的一些坑(用js方法和用java方法)
- Java或其他技术 将html转化成图片?
- java+springmvc+html2cavas转化图片后台处理
- vue 使用html2canvas将DOM转化为图片的方法
- Java里面去掉网页里的HTML标记的方法
- 整理用Java实现数字转化成字符串左边自动补零方法
- Java 实训3-5 接受用户输入0-100的分数 将其转化为1-5的5分记方法
- java 由 html 转化成 text
- JAVA将汉字转化成拼音的方法
- Java图片处理方法——放大、缩小、切割
- Java程序转化为.exe方法
- txt - xml 转化的事例,java main 方法,完整带配置文件,可灵活转化
- javaweb中把数字转换成对应图片的方法
- 解决Outlook 2007中收发邮件HTML文件不能显示图片的方法
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- java解析zip文档,解决解析zip中解析xml和html文档流断的方法
- java中把对象转化为byte数组的方法
- 将图片转换为HTML的神奇方法
- 对HTML 通过GET 方法进行提交后,对信息进行解码JAVA类