将HTML文件转换为图片应用开发
项目使用的基本思路如下:
在项目中发送传真的,传真的信息是由freemarker模板框架生成形成的html文件,通过freemarker获取html文件的內容,通过SWing中组件html信息转换为图片基本.
import java.awt.Dimension;
importjava.awt.Graphics;
import java.awt.Graphics2D;import javax.swing.JTextPane;import javax.swing.plaf.basic.BasicEditorPaneUI;
/**
*通过JTextPane目的显示html信息并绘制图片信息
*
*@authorlonggangbai
*
*/
publicclassPrintView{
public JTextPane panel = null;public PrintView(JTextPane panel) {
this.panel=panel;
}/**
*绘制图片的方法
*
*@paramg
*@paramhPage
*@parampageIndex
*@return
*/
publicbooleanpaintPage(Graphicsg,inthPage,intpageIndex){
Graphics2Dg2=(Graphics2D)g;
Dimensiond=((BasicEditorPaneUI)panel.getUI())
.getPreferredSize(panel);
doublepanelHeight=d.height;
doublepageHeight=hPage;
inttotalNumPages=(int)Math.ceil(panelHeight/pageHeight);
g2.translate(0f,-(pageIndex-1)*pageHeight);
panel.paint(g2);
boolean ret = true;if (pageIndex >= totalNumPages) {
ret=false;
returnret;
}
returnret;
}
}
/**
*HTML转换图片的方式
*
*@authorlonggangbai
*
*/
publicclassGraphUtils{
privatefinalstaticLoggerlogger=Logger.getLogger(GraphUtils.class);
publicstaticintDEFAULT_IMAGE_WIDTH=1024;
public static int DEFAULT_IMAGE_HEIGHT = 768;/**
*将BufferedImage转换为图片的信息
*
*@paramimage
*@return
*/
publicstaticStringtoJpeg(BufferedImageimage){
//获取图片文件的在服务器的路径
StringimageName=FaxUtils.getFaxServerFileDir()+File.separator
+FaxUtils.getSytemFormatDate()+DEFAULT_IMAGE_FORMATSYTLE;
try{
ByteArrayOutputStreambaos=newByteArrayOutputStream();
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(baos);
JPEGEncodeParamparam=encoder.getDefaultJPEGEncodeParam(image);
param.setQuality(1.0f,false);
encoder.setJPEGEncodeParam(param);
encoder.encode(image);
byte[]buff=baos.toByteArray();
baos.close();
//将字节流写入文件保存为图片
FileUtils.writeByteArrayToFile(newFile(imageName),buff);
System.out.println("保存成功!....");
}catch(Exceptionex){
logger.error("保存删除图片失败:"+ex.getMessage());
}
returnimageName;
}/**
*html转换为jpeg文件
*
*@parambgColor
*图片的背景色
*@paramhtml
*html的文本信息
*@paramwidth
*显示图片的Text容器的宽度
*@paramheight
*显示图片的Text容器的高度
*@parameb
*設置容器的边框
*@return
*@throwsException
*/
privatestaticArrayList<String>html2jpeg(ColorbgColor,Stringhtml,
intwidth,intheight,EmptyBordereb)throwsException{
ArrayList<String>ret=newArrayList<String>();
try{
JTextPanetp=newJTextPane();
tp.setSize(width,height);
if(eb==null){
eb=newEmptyBorder(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);
PrintViewm_printView=newPrintView(tp);
intpageIndex=1;
booleanbcontinue=true;
while(bcontinue){
BufferedImageimage=newjava.awt.image.BufferedImage(width,
height,java.awt.image.BufferedImage.TYPE_INT_RGB);
Graphicsg=image.getGraphics();
g.setClip(0,0,width,height);
bcontinue=m_printView.paintPage(g,height,pageIndex);
g.dispose();
Stringpath=toJpeg(image);
ret.add(path);
pageIndex++;
}
}catch(Exceptionex){
throwex;
}
returnret;
}/**
*
*@parambgColor
*@paramhtml
*@paramwidth
*@paramheight
*@return
*@throwsException
*/
publicstaticArrayList<String>toImages(ColorbgColor,String[]htmls,
intwidth,intheight)throwsException{
ArrayList<String>imglist=newArrayList<String>();
for(inti=0;i<htmls.length;i++){
imglist.addAll(html2jpeg(bgColor,htmls[i],width,height,
newEmptyBorder(0,0,0,0)));
}
returnimglist;
}/**
*
*@parambgColor
*@paramhtml
*@paramwidth
*@paramheight
*@return
*@throwsException
*/
publicstaticArrayList<String>toImages(ColorbgColor,Stringhtml,
intwidth,intheight)throwsException{
returnhtml2jpeg(bgColor,html,width,height,newEmptyBorder(0,0,0,
0));
}/**
*将一個html转换为图片
*
*@paramhtmls
*@return
*@throwsException
*/
publicstaticArrayList<String>toImages(Stringhtml)throwsException{
returnhtml2jpeg(null,html,DEFAULT_IMAGE_WIDTH,DEFAULT_IMAGE_WIDTH,
newEmptyBorder(0,0,0,0));
}/**
*将多个html转换图片
*
*@paramhtmls
*@return
*@throwsException
*/
publicstaticArrayList<String>toImages(String[]htmls)throwsException{
ArrayList<String>imglist=newArrayList<String>();
for(inti=0;i<htmls.length;i++){
imglist.addAll(html2jpeg(null,htmls[i],DEFAULT_IMAGE_WIDTH,
DEFAULT_IMAGE_WIDTH,newEmptyBorder(0,0,0,0)));
}
returnimglist;
}
}