admin 管理员组

文章数量: 887021


2024年1月24日发(作者:mouse的中文意思)

将HTML文件转换为图片应用开发

将HTML文件转换为图片应用开发

文章分类:Java编程 关键字: 将html文件转换为图片应用开发

项目使用的基本思路如下:

在项目中发送传真的,传真的信息是由freemarker模板框架生成形成的html文件,通过freemarker获取html文件的內容,通过SWing中组件html信息转换为图片基本.

import ion;

import cs;

import cs2D;

import ane;

import ditorPaneUI;

/**

* 通过JTextPane目的显示html信息并绘制图片信息

*

* @author longgangbai

*

*/

public class PrintView {

public JTextPane panel = null;

public PrintView(JTextPane 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) ())

.getPreferredSize(panel);

double panelHeight = ;

double pageHeight = hPage;

int totalNumPages = (int) (panelHeight / pageHeight);

ate(0f, -(pageIndex - 1) * pageHeight);

(g2);

boolean ret = true;

if (pageIndex >= totalNumPages) {

ret = false;

return ret;

}

return ret;

}

}

/**

* HTML转换图片的方式

*

* @author longgangbai

*

*/

public class GraphUtils {

private final static Logger logger = ger();

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 = ServerFileDir() + tor

+ emFormatDate() + DEFAULT_IMAGE_FORMATSYTLE;

try {

ByteArrayOutputStream baos = new ByteArrayOutputStream();

JPEGImageEncoder encoder = JPEGEncoder(baos);

JPEGEncodeParam param = aultJPEGEncodeParam(image);

lity(1.0f, false);

GEncodeParam(param);

(image);

byte[] buff = Array();

();

// 将字节流写入文件保存为图片

yteArrayToFile(new File(imageName), buff);

n("保存成功!....");

} catch (Exception ex) {

("保存删除图片失败:" + sage());

}

return imageName;

}

/**

* html转换为jpeg文件

*

* @param bgColor

* 图片的背景色

* @param html

* html的文本信息

* @param width

* 显示图片的Text容器的宽度

* @param height

* 显示图片的Text容器的高度

* @param eb

* 設置容器的边框

* @return

* @throws Exception

*/

private static ArrayList html2jpeg(Color bgColor, String html,

int width, int height, EmptyBorder eb) throws Exception {

ArrayList ret = new ArrayList();

try {

JTextPane tp = new JTextPane();

e(width, height);

if (eb == null) {

eb = new EmptyBorder(0, 50, 0, 50);

}

if (bgColor != null) {

kground(bgColor);

}

if (width <= 0) {

width = DEFAULT_IMAGE_WIDTH;

}

if (height <= 0) {

height = DEFAULT_IMAGE_HEIGHT;

}

der(eb);

tentType("text/html");

t(html);

PrintView m_printView = new PrintView(tp);

int pageIndex = 1;

boolean bcontinue = true;

while (bcontinue) {

BufferedImage image = new edImage(width,

height, _INT_RGB);

Graphics g = phics();

p(0, 0, width, height);

bcontinue = m_age(g, height, pageIndex);

e();

String path = toJpeg(image);

(path);

pageIndex++;

}

} catch (Exception ex) {

throw ex;

}

return ret;

}

/**

*

* @param bgColor

* @param html

* @param width

* @param height

* @return

* @throws Exception

*/

public static ArrayList toImages(Color bgColor, String[] htmls,

int width, int height) throws Exception {

ArrayList imglist = new ArrayList();

for (int i = 0; i < ; i++) {

(html2jpeg(bgColor, htmls, width, height,

new EmptyBorder(0, 0, 0, 0)));

}

return imglist;

}

/**

*

* @param bgColor

* @param html

* @param width

* @param height

* @return

* @throws Exception

*/

public static ArrayList 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 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 toImages(String[] htmls) throws Exception {

ArrayList imglist = new ArrayList();

for (int i = 0; i < ; i++) {

(html2jpeg(null, htmls, DEFAULT_IMAGE_WIDTH,

DEFAULT_IMAGE_WIDTH, new EmptyBorder(0, 0, 0, 0)));

}

return imglist;

}

}


本文标签: 图片 文件 转换 信息 容器