3 changed files with 211 additions and 33 deletions
@ -0,0 +1,150 @@
|
||||
package org.springblade.common.utils; |
||||
|
||||
import freemarker.cache.ByteArrayTemplateLoader; |
||||
import freemarker.template.Configuration; |
||||
import freemarker.template.Template; |
||||
import freemarker.template.TemplateExceptionHandler; |
||||
import org.apache.commons.io.IOUtils; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.w3c.dom.Document; |
||||
import org.xhtmlrenderer.swing.Java2DRenderer; |
||||
|
||||
import javax.imageio.ImageIO; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import javax.xml.parsers.DocumentBuilder; |
||||
import javax.xml.parsers.DocumentBuilderFactory; |
||||
import java.awt.image.BufferedImage; |
||||
import java.io.ByteArrayInputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.io.StringWriter; |
||||
import java.net.HttpURLConnection; |
||||
import java.net.URL; |
||||
import java.util.Map; |
||||
import java.util.logging.Logger; |
||||
|
||||
public class TemplateUtil { |
||||
|
||||
|
||||
/** |
||||
* 通过远程URL地址获取模板 |
||||
* 此方法可以通过URL加载存储在远程服务器上的模板 |
||||
* |
||||
* @param template |
||||
* @param map |
||||
* @param url |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static String getTemplateByUrl(String template, Map<String, Object> map, String url) throws Exception { |
||||
Configuration cfg = new Configuration(Configuration.VERSION_2_3_30); |
||||
ByteArrayTemplateLoader byteArrayTemplateLoader = new ByteArrayTemplateLoader(); |
||||
InputStream initialStream = getInputStreamByGet(url); |
||||
byteArrayTemplateLoader.putTemplate(template, IOUtils.toByteArray(initialStream)); |
||||
cfg.setTemplateLoader(byteArrayTemplateLoader); |
||||
cfg.setDefaultEncoding("UTF-8"); |
||||
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); |
||||
cfg.setLogTemplateExceptions(false); |
||||
cfg.setClassicCompatible(true); |
||||
Template temp = cfg.getTemplate(template); |
||||
StringWriter stringWriter = new StringWriter(); |
||||
temp.process(map, stringWriter); |
||||
stringWriter.flush(); |
||||
stringWriter.close(); |
||||
String result = stringWriter.getBuffer().toString(); |
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* 通过get请求得到读取器响应数据的数据流 |
||||
* |
||||
* @param url |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static InputStream getInputStreamByGet(String url) throws Exception { |
||||
InputStream inputStream = null; |
||||
HttpURLConnection conn = (HttpURLConnection) new URL(url) |
||||
.openConnection(); |
||||
conn.setReadTimeout(5000); |
||||
conn.setConnectTimeout(5000); |
||||
conn.setConnectTimeout(5000); |
||||
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { |
||||
inputStream = conn.getInputStream(); |
||||
} |
||||
return inputStream; |
||||
} |
||||
|
||||
/** |
||||
* ftl模板生成图片 |
||||
* @param template |
||||
* @param map |
||||
* @param url |
||||
* @throws Exception |
||||
*/ |
||||
public static BufferedImage turnImage(String template, Map<String,Object> map, String url,Integer width,Integer height) { |
||||
//方式一:指定模板文件路径加载模板
|
||||
// String html = getTemplateByTemplatePath(template, map);
|
||||
//方式二:指定类加载器加载模板
|
||||
// String html = getTemplateByClassLoader(template, map);
|
||||
//方式三:指定远程模板文件存储路径加载模板
|
||||
String html = null; |
||||
BufferedImage img =null; |
||||
try { |
||||
html = getTemplateByUrl(template, map,url); |
||||
byte[] bytes=html.getBytes(); |
||||
ByteArrayInputStream bin=new ByteArrayInputStream(bytes); |
||||
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); |
||||
DocumentBuilder builder=factory.newDocumentBuilder(); |
||||
Document document=builder.parse(bin); |
||||
Java2DRenderer renderer = new Java2DRenderer(document,width,height); |
||||
img = renderer.getImage(); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return img; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 待合并的两张图必须满足这样的前提,如果水平方向合并,则高度必须相等;如果是垂直方向合并,宽度必须相等。 |
||||
* mergeImage方法不做判断,自己判断。 |
||||
* |
||||
* @param img1 |
||||
* 待合并的第一张图 |
||||
* @param img2 |
||||
* 带合并的第二张图 |
||||
* @param isHorizontal |
||||
* 为true时表示水平方向合并,为false时表示垂直方向合并 |
||||
* @return 返回合并后的BufferedImage对象 |
||||
* @throws IOException |
||||
*/ |
||||
public static BufferedImage mergeImage(BufferedImage img1, |
||||
BufferedImage img2, boolean isHorizontal) throws IOException { |
||||
int w1 = img1.getWidth(); |
||||
int h1 = img1.getHeight(); |
||||
int w2 = img2.getWidth(); |
||||
int h2 = img2.getHeight(); |
||||
|
||||
// 从图片中读取RGB
|
||||
int[] ImageArrayOne = new int[w1 * h1]; |
||||
ImageArrayOne = img1.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
|
||||
int[] ImageArrayTwo = new int[w2 * h2]; |
||||
ImageArrayTwo = img2.getRGB(0, 0, w2, h2, ImageArrayTwo, 0, w2); |
||||
|
||||
// 生成新图片
|
||||
BufferedImage DestImage = null; |
||||
if (isHorizontal) { // 水平方向合并
|
||||
DestImage = new BufferedImage(w1+w2, h1, BufferedImage.TYPE_INT_RGB); |
||||
DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
|
||||
DestImage.setRGB(w1, 0, w2, h2, ImageArrayTwo, 0, w2); |
||||
} else { // 垂直方向合并
|
||||
DestImage = new BufferedImage(w1, h1 + h2, BufferedImage.TYPE_INT_RGB); |
||||
DestImage.setRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
|
||||
DestImage.setRGB(0, h1, w2, h2, ImageArrayTwo, 0, w2); // 设置下半部分的RGB
|
||||
} |
||||
|
||||
return DestImage; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue