2019独角兽企业重金招聘Python工程师标准>>>

第一次尝试:用awt 包将HTML源码转换为图片

优点:不依赖任何外部JAR包,缺点:对CSS的支持比较差,复杂点的样式就无法展示,且不支持外部引入的CSS和写在style中的CSS,只能写在标签上

Eg:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.UUID;import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicEditorPaneUI;import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;public class GraphUtils {private final static Log log                  = LogFactory.getLog(GraphUtils.class);public static int        DEFAULT_IMAGE_WIDTH  = 1000;//默认值最好设置大点,因为我们再导之前,不知道这个流有多大,如果过小,则生成的图片后面的为黑色,因为流没有读取完整public static int        DEFAULT_IMAGE_HEIGHT = 200;public static boolean paintPage(Graphics g, int hPage, int pageIndex, JTextPane panel) {Graphics2D g2 = (Graphics2D) g;Dimension d = ((BasicEditorPaneUI) panel.getUI()).getPreferredSize(panel);double panelHeight = d.height;double pageHeight = hPage;int totalNumPages = (int) Math.ceil(panelHeight / pageHeight);g2.translate(0f, -(pageIndex - 1) * pageHeight);panel.paint(g2);boolean ret = true;if (pageIndex >= totalNumPages) {ret = false;return ret;}return ret;}/*** 将BufferedImage转换为图片的信息*/public static String toJpeg(BufferedImage image) {// 获取图片文件的在服务器的路径String imageName = "G:\\" + UUID.randomUUID().toString() + ".jpg";try {ByteArrayOutputStream baos = new ByteArrayOutputStream();JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos);JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);param.setQuality(1.0f, false);encoder.setJPEGEncodeParam(param);encoder.encode(image);byte[] buff = baos.toByteArray();baos.close();// 将字节流写入文件保存为图片FileUtils.writeByteArrayToFile(new File(imageName), buff);} catch (Exception ex) {log.error("保存删除图片失败:" + ex.getMessage());}return imageName;}/*** html转换为jpeg文件* @param bgColor 图片的背景色* @param html html的文本信息* @param width 显示图片的Text容器的宽度* @param height 显示图片的Text容器的高度* @param eb 設置容器的边框* @return* @throws Exception*/private static ArrayList<String> html2jpeg(Color bgColor, String html, int width, int height, EmptyBorder eb)throws Exception {ArrayList<String> ret = new ArrayList<String>();try {JTextPane tp = new JTextPane();tp.setSize(width, height);if (eb == null) {eb = new EmptyBorder(0, 50, 0, 50);}if (bgColor != null) {tp.setBackground(bgColor);}if (width <= 0) {width = DEFAULT_IMAGE_WIDTH;}if (height <= 0) {height = DEFAULT_IMAGE_HEIGHT;}tp.setBorder(eb);tp.setContentType("text/html");tp.setText(html);int pageIndex = 1;boolean bcontinue = true;while (bcontinue) {BufferedImage image = new java.awt.image.BufferedImage(width, height,java.awt.image.BufferedImage.TYPE_INT_RGB);Graphics g = image.getGraphics();g.setClip(0, 0, width, height);bcontinue = paintPage(g, height, pageIndex, tp);g.dispose();String path = toJpeg(image);ret.add(path);pageIndex++;}} catch (Exception ex) {throw ex;}return ret;}/*** 将一個html转换为图片* @param htmls* @return* @throws Exception*/public static ArrayList<String> toImages(String html) throws Exception {return html2jpeg(Color.white, html, DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT, new EmptyBorder(0, 0, 0, 0));}

TestCase:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath*:spring/application.xml", "classpath*:spring/plugin-*.xml" })
public class GraphUtilsTest extends TestCase {@Autowiredprivate TaskService            taskService;@Autowiredprivate VelocitySimpleRelolver velocitySimpleRelolver;@Testpublic void testHtml2Image() {try {String imageTemplateFile = "mail/weekly/mile_schedule_task.vm";//邮件内容Map<String, Object> velocityData = new HashMap<String, Object>();String projId = "6a9fb54e3439453e86e819a4d46fdafb";MileSchedule mileSchedule = taskService.getMileSchedule(projId);velocityData.put("mileSchedule", mileSchedule);String htmlstr = velocitySimpleRelolver.relolve(imageTemplateFile, velocityData);Assert.assertNotNull(htmlstr);GraphUtils.toImages(htmlstr);} catch (Exception e) {e.printStackTrace();}}
}

Second: 利用html2image组件

优点:方便,代码简单  缺点:CSS支持极差,比不上Java自身的转化

maven引入:

<dependency> <groupId>gui.ava</groupId> <artifactId>html2image</artifactId> <version>0.9</version>
</dependency>

TestCase:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath*:spring/application.xml", "classpath*:spring/plugin-*.xml" })
public class Html2ImageTest extends TestCase {@Autowiredprivate TaskService            taskService;@Autowiredprivate VelocitySimpleRelolver velocitySimpleRelolver;@Testpublic void testHtml2Image() {try {String imageTemplateFile = "mail/weekly/mile_schedule_task.vm";//邮件内容Map<String, Object> velocityData = new HashMap<String, Object>();String projId = "6a9fb54e3439453e86e819a4d46fdafb";MileSchedule mileSchedule = taskService.getMileSchedule(projId);velocityData.put("mileSchedule", mileSchedule);String htmlstr = velocitySimpleRelolver.relolve(imageTemplateFile, velocityData);System.out.println(htmlstr);Assert.assertNotNull(htmlstr);String imageName = "G:/weekly_mile_" + projId + ".png";HtmlImageGenerator imageGenerator = new HtmlImageGenerator();imageGenerator.setSize(new Dimension(1000, 200));imageGenerator.loadHtml(htmlstr);imageGenerator.getBufferedImage();imageGenerator.saveAsImage(imageName);} catch (Exception e) {e.printStackTrace();}}@Testpublic void testImage() {HtmlImageGenerator imageGenerator = new HtmlImageGenerator();imageGenerator.setSize(new Dimension(1000, 200));imageGenerator.loadUrl("http://www.baidu.com");imageGenerator.getBufferedImage();imageGenerator.saveAsImage("G:/aaa.png");}
}

Third:DJNativeSwing

参考:http://blog.csdn.net/cping1982/article/details/5353049

其中:JAR包下载地址:

http://sourceforge.net/projects/djproject/?source=navbar  此地址下down DJNativeSwing.jar 和DJNativeSwing-SWT.jar

http://maven-repository.com/artifact/org.eclipse.swt.org.eclipse.swt.win32.win32.x86_64.4.3.swt/org.eclipse.swt.win32.win32.x86_64/4.3

此地址下down org.eclipse.swt 包,选择适合你的开发环境

注意:swt 包针对不同的环境有不同的要求,windows与Linux下的不同,使用maven引入时采用properties方式,将下载的包放入你的maven库,然后在POM中进行maven引入

eg:

<!-- DJNativeSwing --><dependency> <groupId>chrriis.dj.nativeswing</groupId> <artifactId>DJNativeSwing</artifactId> <version>1.0.2</version> </dependency> <dependency> <groupId>chrriis.dj.nativeswing.swt</groupId> <artifactId>DJNativeSwing-SWT</artifactId> <version>1.0.2</version> </dependency><dependency><groupId>${swt.groupId}</groupId><artifactId>${swt.artifactId}</artifactId><version>4.3</version></dependency>
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Semaphore;import javax.imageio.ImageIO;
import javax.swing.JPanel;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 DJNativeSwingUtils extends JPanel {private static final long  serialVersionUID = 8675106494733722832L;// 行分隔符  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;");}public static JPanel createContent(String htmlStr, final String fileName, final Semaphore semp) {//浏览器面板JPanel webBrowserPanel = new JPanel(new BorderLayout());//swing的内嵌浏览器final JWebBrowser webBrowser = new JWebBrowser();//隐藏所有的栏webBrowser.setBarsVisible(false);//向浏览器写入html内容webBrowser.setHTMLContent(htmlStr);//将浏览器嵌入浏览器面板webBrowserPanel.add(webBrowser, BorderLayout.CENTER);//向浏览器增加监听事件webBrowser.addWebBrowserListener(new WebBrowserAdapter() {// 监听加载进度  public void loadingProgressChanged(WebBrowserEvent e) {// 当加载完毕时  if (e.getWebBrowser().getLoadingProgress() == 100) {try {//执行JS获取图片的宽度,高度String result = (String) webBrowser.executeJavascriptWithResult(jsDimension.toString());int index = result == null ? -1 : result.indexOf(":");NativeComponent nativeComponent = webBrowser.getNativeComponent();//获取图片的原始尺寸Dimension originalSize = nativeComponent.getSize();//根据JS返回结果设定新的图片尺寸Dimension imageSize = new Dimension(Integer.parseInt(result.substring(0, index)), Integer.parseInt(result.substring(index + 1)));//计算图片的新尺寸imageSize.width = Math.max(originalSize.width, imageSize.width);imageSize.height = Math.max(originalSize.height, imageSize.height);nativeComponent.setSize(imageSize);//创建一个不带透明色的BufferedImage对象BufferedImage image = new BufferedImage(imageSize.width, imageSize.height,BufferedImage.TYPE_INT_RGB);//对浏览器中图片进行绘色nativeComponent.paintComponent(image);//截图image = image.getSubimage(0, 0, imageSize.width - 25, imageSize.height);try {// 输出图像  ImageIO.write(image, "jpg", new File(fileName));} catch (IOException ex) {ex.printStackTrace();}} finally {//  退出操作  :释放线程NativeInterface.close();semp.release();}}}});return webBrowserPanel;}
}

service调用:

private String createImage(final String htmlStr, final Semaphore semaphore) {final String fileName = FileUtils.getNewFileName("jpg");NativeInterface.open();UIUtils.setPreferredLookAndFeel();SwingUtilities.invokeLater(new Runnable() {public void run() {// SWT组件转Swing组件,不初始化父窗体将无法启动webBrowser  JFrame frame = new JFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 加载指定页面frame.getContentPane().add(DJNativeSwingUtils.createContent(htmlStr, fileName, semaphore),BorderLayout.CENTER);// 仅初始化,但不显示  frame.invalidate();frame.pack();frame.setVisible(false);}});// 在MAC上使用时,因为MAC不支持双进程,仅支持进程内模式,调用此方法进行事件调度。windows和Linux可以忽略此事件调度//NativeInterface.runEventPump();return new File(fileName).getAbsolutePath();}
 private int fillPicture(HSSFSheet sheet, Report report, int startRow) throws IOException {if(null == weekly.getMileSchedule()){return startRow + 1;}// 将高度设置为图片的高度HSSFRow pictureRow = sheet.getRow(startRow);pictureRow.setHeightInPoints(162);String htmlStr = this.createHtml(report);String imagePath = null;final Semaphore semp = new Semaphore(1);//设置信号量try {semp.acquire();// 子线程生成图片imagePath = this.createImage(htmlStr, semp);// 等待子线程释放信号量semp.acquire();} catch (InterruptedException e) {LOG.error("", e);} finally {semp.release();}try {ExcelExportUtils.insertPicture(sheet, startRow, (short) 0, (short) 15, imagePath);} catch (Exception e) {e.printStackTrace();} finally {// delete 文件new File(imagePath).delete();}return ++startRow;}

windows 下截图正常,Linux下异常,Linux下必须安装Mollizia内核的浏览器,在服务器上安装了一个Firefox后,也必须安装图形界面,刚开始采用安装虚拟x-server,但是在CentOS 下安装各种依赖,尝试安装图形界面后tomcat也必须在图形界面下启动才能正常截图,后废弃此种方式,采用java原生绘图方式,下一篇博文介绍

转载于:https://my.oschina.net/u/246522/blog/280806

Java HTML转换为图片相关推荐

  1. java html 转图片_Java HTML转换为图片

    第一次尝试:用awt 包将HTML源码转换为图片 优点:不依赖任何外部JAR包,缺点:对CSS的支持比较差,复杂点的样式就无法展示,且不支持外部引入的CSS和写在style中的CSS,只能写在标签上 ...

  2. Java中将base64编码字符串转换为图片

    前一段时间,在做摄像头拍照上传,摄像头拍的照片为base64编码格式的字符串,需要上传至项目中,则需要使用到将base64编码字符串转换为图片 1.将base64编码字符串转换为图片的代码如下 Ima ...

  3. java pdf转图片拼接_java实现pdf按页转换为图片

    本文实例为大家分享了java实现pdf按页转换为图片的具体代码,供大家参考,具体内容如下 本程序是利用jacob.jar包实现的,关于jacob.jar的配置见我上一篇文章,程序中可配置参数选择图片清 ...

  4. java pptx转图_Java如何将PPT的幻灯片转换为图片?

    在Java编程中,如何将PPT的幻灯片转换为图片? 注意:需要访问网址: , 下载一个Apache POI软件包.这里下载最新版本:poi-bin-3.17-20170915.tar.gz解压并全部导 ...

  5. Ubuntu下Java使用pdfbox将pdf转换为图片的方法及问题

    Ubuntu下Java使用pdfbox将pdf转换为图片的方法及问题 使用pdfbox-2.0.3和fontbox-2.0.3,实现pdf转图片功能. 官方手册链接: http://pdfbox.ap ...

  6. java+icepdf+下载,Java中使用icepdf轻松把pdf转换为图片

    Java中使用icepdf轻松把pdf转换为图片 icepdf简介: icepdf是java的一个专门处理pdf的外置的扩展包,使用它可以方便的把pdf转换为图片,当然它的功能不止如此,大家如果想要深 ...

  7. Java踩坑笔记 —— base64转换为图片后图片显示不全

    Java踩坑笔记 -- base64转换为图片后图片显示不全 前言 解决思路 案例代码 结束语 前言 最近在开发项目中,发现base64转换图片时,出现了图片显示不全,例如如下这样 可以明显看到图片部 ...

  8. java pdf转换为png图片(1)

    注意: 更为优化的版本(实现多页pdf):https://blog.csdn.net/datouniao1/article/details/110866883 首先需要引入两个jar pdfbox.j ...

  9. JAVA实现Excel照相机功能_Excel如何将工作表转换为图片,并随着数据的变化自动更新?...

    随着Microsoft Office版本的更新,越来越多的功能汇集到软件之中.一些常用的功能放在"台面"上,方便用户使用.一些新增的.不常使用的功能却被隐藏起来,需要用户根据需要进 ...

  10. php生成图片文件流,php如何将base64数据流文件转换为图片文件?

    2017-03-07 在开发中,自己遇到一个前端在上传图片的时候,使用的base64数据流文件显示的图片. 也就是说 ***image/后面的jpg是我们的图片文件格式,(base64,)后面的很大一 ...

最新文章

  1. linux ln(link) 命令详解
  2. 跌宕起伏之windows 7安装
  3. emoji表情过滤处理
  4. leetcode - 646. 最长数对链
  5. 安装默认报表服务器虚拟目录,报表服务器虚拟目录
  6. 如何用Sql更新默认值
  7. 如何升级linux内核
  8. 2020前端开发面试题总结(最新)
  9. 知乎文章导出完整PDF(简单易上手)
  10. 【Vim】No write since last change
  11. px4初探-qgroundcontrol安装
  12. Python新手都知道而你却不知的经典案例
  13. 国内外物联网和AI平台
  14. JAVA计算机毕业设计学术会议信息网站Mybatis+源码+数据库+lw文档+系统+调试部署
  15. CSP-S 2021 游记
  16. Activiti实现会签功能程序Demo
  17. python如何将excel非首行作为dataframe的列名
  18. (几何学:五六边形面积)编写一个程序,提示用户输入五边形顶点到中心的距离r,然后算出五边形的面积。
  19. C语言概述以及开发工具
  20. python-pygame:实现飞机大战详情(含源码)

热门文章

  1. c语言语句结束的标准,C语言的语句要求以哪种符号结束?
  2. heatmap绘制热图时出现样本列名顺序调换
  3. 【2016-2017 ACM-ICPC (ECNA 2016) G】That's one Hanoi-ed Teacher【汉诺塔问题】
  4. 实数系的完备性的含义
  5. Hvv近期0day总结四
  6. LINUX中文件的移动,Linux中文件移动文件的操作命令为 ()
  7. 简单的Swagger生产环境中屏蔽
  8. 常用软件版本查看Windows下
  9. PhpStorm-2017.3 注册激活
  10. 图论:Gale-Shapley算法