废话少说,直接进去正题:(也可以直接下载附件进行运行)
/**
*
*/
package htmlToJpg;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
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.cxsoft.rap.ed.util.ManualUtil;
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 = 1024;
//默认值最好设置大点,因为我们再导之前,不知道这个流有多大,如果过小,则生成的图片后面的为黑色,因为流没有读取完整
public static int DEFAULT_IMAGE_HEIGHT = 10000;
/**
* 将BufferedImage转换为图片的信息
*
* @param image
* @return
*/
public static String toJpeg(BufferedImage image, String nsrlsh, String year) {
// 获取图片文件的在服务器的路径
// String imgPath = ManualUtil.getImgPath(nsrlsh, year);
// String imageName = imgPath+UUID.randomUUID().toString() + ".jpg";
String imageName = "E:\\"+UUID.randomUUID().toString() + ".jpg";
System.out.println(imageName);
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);
System.out.println("保存成功!....");
} 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,
String nsrlsh, String year) 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);
Dimension d = ((BasicEditorPaneUI) tp.getUI()).getPreferredSize(tp);
//此处是将一个页面生成一张图片,如果要人为控制图片大小进行分页生成,则再自行进行高度设置,但是分页之后可能出现的情况就是分页的时候字体被截断的可能,因为在截断的之后,他不知道流是否刚好将一行字显示完成
if(d.height>DEFAULT_IMAGE_HEIGHT){
height=d.height;
}
PrintView m_printView = new PrintView(tp);
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 = m_printView.paintPage(g, height, pageIndex);
g.dispose();
String path = toJpeg(image, nsrlsh, year);
ret.add(path);
pageIndex++;
}
} catch (Exception ex) {
throw ex;
}
return ret;
}
/**
* 将一個html转换为图片
*
* @param htmls
* @return
* @throws Exception
*/
public static ArrayList<String> toImages(String html, String nsrlsh, String year) throws Exception {
return html2jpeg(null, html, DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT, new EmptyBorder(0, 0, 0, 0), nsrlsh, year);
}
public static void main(String[] args) {
try {
// FileReader fr = new FileReader("D:\\DEV\\o\\现金流量表.html");
// BufferedReader bfr = new BufferedReader(fr);
String htmlstr = "<p>/**<br /> * <br /> */<br />package com.cxsoft.rap.ed.util;</p>";
String nextLine = null;
// while ((nextLine = bfr.readLine()) != null) {
// htmlstr += nextLine;
// }
System.out.println(htmlstr);
GraphUtils.toImages(htmlstr, "000000003126754", "2009");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
另一个辅助类:
/**
*
*/
/**
*
*/
package htmlToJpg;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JTextPane;
import javax.swing.plaf.basic.BasicEditorPaneUI;
public class PrintView {
public JTextPane panel = null;
public PrintView(JTextPane panel) {
this.panel = panel;
}
/**
* 绘制图片的方法
*
* @param g
* @param hPage
* @param pageIndex
* @return
*/
public boolean paintPage(Graphics g, int hPage, int pageIndex) {
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;
}
}
直接可以运行!