发表时间:2009-10-21
最后修改:2009-12-19
项目使用的基本思路如下:
在项目中发送传真的,传真的信息是由freemarker模板框架生成形成的html文件,通过freemarker获取html文件的內容,通过SWing中组件html信息转换为图片基本.
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JTextPane;
import javax.swing.plaf.basic.BasicEditorPaneUI;
/**
* 通过JTextPane目的显示html信息并绘制图片信息
*
* @author longgangbai
*
*/
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;
}
}
/**
* HTML转换图片的方式
*
* @author longgangbai
*
*/
public class GraphUtils {
private final static Logger logger = Logger.getLogger(GraphUtils.class);
public static int DEFAULT_IMAGE_WIDTH = 1024;
public static int DEFAULT_IMAGE_HEIGHT = 768;
/**
* 将BufferedImage转换为图片的信息
*
* @param image
* @return
*/
public static String toJpeg(BufferedImage image) {
// 获取图片文件的在服务器的路径
String imageName = FaxUtils.getFaxServerFileDir() + File.separator
+ FaxUtils.getSytemFormatDate() + DEFAULT_IMAGE_FORMATSYTLE;
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) {
logger.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);
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);
ret.add(path);
pageIndex++;
}
} catch (Exception ex) {
throw ex;
}
return ret;
}
/**
*
* @param bgColor
* @param html
* @param width
* @param height
* @return
* @throws Exception
*/
public static ArrayList<String> toImages(Color bgColor, String[] htmls,
int width, int height) throws Exception {
ArrayList<String> imglist = new ArrayList<String>();
for (int i = 0; i < htmls.length; i++) {
imglist.addAll(html2jpeg(bgColor, htmls[i], width, height,
new EmptyBorder(0, 0, 0, 0)));
}
return imglist;
}
/**
*
* @param bgColor
* @param html
* @param width
* @param height
* @return
* @throws Exception
*/
public static ArrayList<String> toImages(Color bgColor, String html,
int width, int height) throws Exception {
return html2jpeg(bgColor, html, width, height, new EmptyBorder(0, 0, 0,
0));
}
/**
* 将一個html转换为图片
*
* @param htmls
* @return
* @throws Exception
*/
public static ArrayList<String> toImages(String html) throws Exception {
return html2jpeg(null, html, DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_WIDTH,
new EmptyBorder(0, 0, 0, 0));
}
/**
* 将多个html转换图片
*
* @param htmls
* @return
* @throws Exception
*/
public static ArrayList<String> toImages(String[] htmls) throws Exception {
ArrayList<String> imglist = new ArrayList<String>();
for (int i = 0; i < htmls.length; i++) {
imglist.addAll(html2jpeg(null, htmls[i], DEFAULT_IMAGE_WIDTH,
DEFAULT_IMAGE_WIDTH, new EmptyBorder(0, 0, 0, 0)));
}
return imglist;
}
}