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 javax.imageio.ImageIO;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicEditorPaneUI; /**
* 将html转换成Image图片
*
*
*
*/
public class HtmlToImageUtils { /**
* 画页面的方法
*
* @param g 画笔
* @param panel 画板
* @return
*/
public static void paintPage(Graphics g, JTextPane panel) {
Graphics2D g2 = (Graphics2D) g;
g2.translate(0f, 0f);
panel.paint(g2);
} /**
* html转换为png文件
*
* @param bgColor 图片的背景色
* @param html html的文本信息
* @param width 显示图片的text容器的宽度
* @param height 显示图片的text容器的高度
* @param eb 設置容器的边框
* @return
* @throws Exception
*/
public static byte[] html2png(Color bgColor, String html, EmptyBorder eb) throws Exception {
JTextPane tp = new JTextPane();
if (eb == null) {
eb = new EmptyBorder(0, 50, 0, 50);
}
if (bgColor != null) {
tp.setBackground(bgColor);
}
tp.setBorder(eb);
tp.setContentType("text/html");
tp.setText(html);
byte[] bytes = null;
Dimension d = ((BasicEditorPaneUI) tp.getUI()).getPreferredSize(tp);
int height = d.height + 20;
int width = d.width + 15;
tp.setSize(width, height);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setClip(0, 0, width, height);
paintPage(g, tp);
g.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
baos.flush();
bytes = baos.toByteArray();
baos.close();
return bytes;
}

  调用方法:

byte[] bytes = HtmlToImageUtils.html2png(Color.white, html, new EmptyBorder(0, 0, 0, 0));

  html样例

String htmlstr =
""+"<body style=\"background:#f4f4f4;margin:0;padding:0;font-size:14px;color:#333;line-height: 1;box-sizing:border-box;\">" +
"<div style=\"box-sizing:border-box;width:980px;padding: 32px 40px;overflow: hidden;margin:0 auto;\">" +
"<div style=\"box-sizing:border-box;text-align: right;width:100%;height:16px;font-size: 16px;line-height: 16px;\">住院</div>" +
"<div style=\"box-sizing:border-box;width:100%;height:60px;line-height: 1;overflow:hidden;text-align: center;\">" +
"<div style=\"box-sizing:border-box;display: inline-block;margin:0 auto;\">" +
"<div style=\"box-sizing:border-box;display:block;font-size: 24px;\">首都医科大学附属北京儿童医院</div>" +
"<div style=\"box-sizing:border-box;display:block;font-size: 20px;margin-top:16px;\">影像检查诊断报告单</div>" +
"</div>" +
"</div>" +
"<div style=\"box-sizing:border-box;width:100%;border:solid 2px #000;border-right:none;border-left:none; line-height:28px;height:62px;margin-top:10px;padding:2px 8px;\">" +
"<table border=\"0\" style=\"width:100%;border-spacing:0px ; \">" +
"<tr style=\"width:100%;height:16px;\" border=\"0\">" +
"<td style=\"width:25%;\">姓名:张三丰</td>" +
"<td style=\"width:25%;\">性别:男</td>" +
"<td style=\"width:25%;\">年龄:4岁</td>" +
"<td style=\"width:25%;\">ID号: 0009090990</td>" +
"</tr>" +
"<tr style=\"width:100%;height:16px;\" border=\"0\">" +
"<td style=\"\">科室:内分泌</td>" +
"<td style=\"width:25%;\">HIS号:987922</td>" +
"<td style=\"width:25%;\">检查类别:MR</td>" +
"<td style=\"width:25%;\">检验号:201209090909</td>" +
"</tr>" +
"</table>" +
"</div>" +
"<div style=\"min-height:30px;clear:both;overflow:hidden;margin-top:8px;\">" +
"<table border=\"0\" style=\"width:100%;border-spacing:0px ; \">" +
"<tr style=\"width:100%;height:16px;\" border=\"0\">" +
"<td style=\"width:90px;font-size: 16px;line-height:24px;color:#000;font-weight:700;\">检查部位:</td>" +
"<td style=\"width:810px;padding-left:8px;line-height:20px;padding-top:2px;\">小腿哦UI电话小腿小腿哦U话小腿哦UI电话</td>" +
"</tr>" +
"</table>" +
"</div>" +
"<div style=\"min-height:100px;clear:both;overflow:hidden;margin-top:20px;\">" +
"<table border=\"0\" style=\"width:100%;border-spacing:0px ; \">" +
"<tr style=\"width:100%;\" border=\"0\" valign=\"top\">" +
"<td style=\"width:90px;font-size: 16px;line-height:24px;color:#000;font-weight:700;\">影像表现:</td>" +
"<td style=\"width:810px;padding-left:8px;line-height:20px;padding-top:2px;\">小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UIUI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UIUI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话</td>" +
"</tr>" +
"</table>" +
"</div>" +
"<div style=\"min-height:100px;clear:both;overflow:hidden;margin-top:30px;\">" +
"<table border=\"0\" style=\"width:100%;border-spacing:0px ; \">" +
"<tr style=\"width:100%;\" border=\"0\" valign=\"top\">" +
"<td style=\"width:90px;font-size: 16px;line-height:24px;color:#000;font-weight:700;\">印象:</td>" +
"<td style=\"width:810px;padding-left:8px;line-height:20px;padding-top:2px;\">小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UIUI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UIUI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话</td>" +
"</tr>" +
"</table>" +
"</div>" +
"<div style=\"margin-top:20px;height:24px;line-height:24px;\">" +
"<table border=\"0\" style=\"width:100%;border-spacing:0px ; \">" +
"<tr style=\"width:100%;height:24x;\" border=\"0\">" +
"<td style=\"width:36%;\">报告日期:2017-11-22 11:12:09</td>" +
"<td style=\"width:34%;\">报告医生:唐小小</td>" +
"<td style=\"\">审核医生:李若曦</td>" +
"</tr>" +
"</table>" +
"</div>" +
"<div style=\"border-top:solid 2px #000;margin-top:6px;font-size:10px;font-weight:600;line-height:26px;padding-left:10px;\"> " +
"注:本报告仅供临床医师参考,影像科医师签字 后生效" +
"</div>" +
"" +
"</div>" +
"</body>"
;
try {
// FileUtil.readAscFile(
byte[] bytes = HtmlToImageUtils.html2jpeg(Color.white, htmlstr,
1300, 1200, new EmptyBorder(0, 0,
0, 0));
//写入阿里云
InputStream instreams = new ByteArrayInputStream(bytes);
String resUrl= ossService.uploadObject(instreams, "mrUpload/",
UUID.randomUUID().toString(), com.hecross.common.util.FileUtils.getFileExt(".jpg"));
System.out.println(resUrl);
res.put("resUrl",resUrl);
} catch (Exception e) {
e.printStackTrace();
}

  

  

  

将html转换成image图片png格式的更多相关文章

  1. Excel将秒转换成标准的时间格式HH:MM:SS

    Excel将秒转换成标准的时间格式HH:MM:SS 比如120秒,转换成00:02:00 Excel公式为: =TEXT(A1/86400,"[hh]:mm:ss") A1为秒数据 ...

  2. qrcode.js插件将你的内容转换成二维码格式

    ---qrcode.js插件将你的内容转换成二维码格式--- 我之前一直想知道二维码是怎么生成,所以就了解了一下, 最后还是不知道它的原理, 但是,我知道怎么生成. 现在就让我带你制作一个你喜爱的二维 ...

  3. Ghostscript 将PDF文件转换成PNG图片 问题一二

    由于项目需求,需要将原来的PDF文档转换成图片文件,在网上找了一些PDF转图片的方法:测试了几个后,都有这样或那样的问题 1.PDFLibNet.dll,这个类型最初还是挺好用的,能转图片和HTML, ...

  4. 将psd格式批量转换成jpg或png格式(C#自制软件)

    此项目基于.net framework 4.0 将psd格式批量转换成jpg或png格式. 链接:https://pan.baidu.com/s/16IEjX0sHaY9H3Ah7mv6IPQ 提取码 ...

  5. Openssl生成RSA公私钥以及将公钥转换成C#支持的格式

    Openssl生成RSA公私钥以及将公钥转换成C#支持的格式 1.RSA算法介绍 RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密.RSA ...

  6. 【Spring Boot】Spring Boot之利用Logstash将日志转换成以JSON的格式存储和输出

    一.Logstash的作用 Logstash是一个完全开源的工具,它可以对日志进行收集.过滤,能非常方便地将日志转换成以JSON的格式存储和输出,并将其存储供以后使用. 二.整合Logstash的步骤 ...

  7. JS框架_(Qrcode.js)将你的内容转换成二维码格式

    百度云盘 传送门 密码:304e 输入网址点击按钮生成二维码,默认为我的博客首页 二维码格式演示 <!DOCTYPE html> <html lang="en"& ...

  8. C#使用FFmpeg 将视频格式转换成Gif图片示例

    根据EFmpeg封装的视频转换gif工具:https://my.oschina.net/tianma3798/blog/825317 一.本次使用参数说明 /* * 参数说明: * -i 源文件位置 ...

  9. mpp文件转换成jpg图片,可以用pdf文件做中转站

    用project软件做了一个表,发现不能转换成图片,先把mpp文件转换成pdf文件,然后用PS打开pdf文件,存储为jpg格式就行了

随机推荐

  1. django的用户认证组件

    DataSource:https://www.cnblogs.com/yuanchenqi/articles/9064397.html 代码总结: 用户认证组件: 功能:用session记录登录验证状 ...

  2. 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #21FUSE

    HACK #21FUSE 本节将介绍使用用户进程的文件系统框架—FUSE.FUSE概要FUSE(Filesystem in Userspace,用户空间文件系统),是用来生成用户空间的一般进程的框架. ...

  3. Bogart gGrid.vb

    Namespace BogartMis.Cls Public Class gGrid '設定表格控的列標題的別名 '說明:strItem字符串的格式為"01,02,03,04,05" ...

  4. 不规则ROI的提取

    在网上看到基于opencv3.0之前的API实现不规则ROI的提取,我自己试了一下发现opencv3.0不行,第一想法是我写的有问题,最后发现是API的改版.原理很简单. 目标:提取黑线作为ROI 原 ...

  5. Caused by: java.lang.IllegalStateException: Expected raw type form of org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$Match

    spring 4.0.2,mybatis 3.2.6,aspectjweaver 1.8.10 使用的时候,报错: Caused by: java.lang.IllegalStateException ...

  6. XE Styles不见了

    C:\Users\Public\Documents\Embarcadero\Studio\15.0\Styles 我移动到D盘了 D:\Users\Public\Documents\Embarcade ...

  7. 可视化库-seaborn-单变量绘图(第五天)

    1. sns.distplot 画直方图 import numpy as np import pandas as pd from scipy import stats, integrate impor ...

  8. eclipse插件常用网址链接

    目录 jar下载地址 java maven svn erMaster linux镜像ISO:       http://www.linuxfly.org/post/659/ virtual下载:    ...

  9. Pycharm安装autopep8工具

    参考文当:https://www.cnblogs.com/heenhui2016/p/6802122.html

  10. oracle启动过程

    Oracle  的启动需要经历四个状态,SHUTDOWN .NOMOUNT .MOUNT .OPEN. SHUTDOWN状态                                       ...