今天做了一个Jsp页面合成图标的例子。
起因每次网上去合成Gmail图片,觉得不爽(偶这里联网慢拉)。然后就找了一份合成Gmail图片的代码,http://playtime.uni.cc/downloads.php?page=gmail。下下来一看,php的,而且还需要一个GD的库。不过代码倒是看明白了。很短,也很无趣。
于是我就想能不能用Jsp合成图标阿。
然后搜了一下,网上果然有介绍。于是就自己照猫画虎做了一个爱因斯坦的图片合成的例子。
下边是代码。总共不到100行。
通过测试,响应速度还挺快的。这里是核心部分
/*
=====ImageGen.jsp========
*/
<%
@ page language
=
"
java
"
contentType
=
"
image/jpeg
"
%>
<%
@ page
import
=
"
java.io.*
"
%>
<%
@ page
import
=
"
java.awt.*
"
%>
<%
@ page
import
=
"
java.awt.image.*
"
%>
<%
@ page
import
=
"
com.sun.image.codec.jpeg.*
"
%>
<%
!
static
Font fnt
=
null
;
%>
<%
!
static
BufferedImage bg
=
null
;
%>
<%
//
DEBUG: -> long st = System.currentTimeMillis();
request.setCharacterEncoding(
"
shift_jis
"
);
String text
=
request.getParameter(
"
text
"
);
if
(text
==
null
) text
=
""
;
//
DEBUG: -> System.err.println( " text: " + text);
//
because font and background need only read once, so make it static.
if
(fnt
==
null
)
//
handwriting font make it looks funny.s
fnt
=
Font.createFont(Font.TRUETYPE_FONT,
new
FileInputStream(getServletContext().getRealPath(
"
/
"
)
+
"
LHANDW.TTF
"
));
if
(bg
==
null
)
{
JPEGImageDecoder decoder
=
JPEGCodec.createJPEGDecoder(
new
FileInputStream(getServletContext().getRealPath(
"
/
"
)
+
"
einstein.jpg
"
));
bg
=
decoder.decodeAsBufferedImage();
}
//
draw background
int
width
=
bg.getWidth();
int
height
=
bg.getHeight();
BufferedImage bi
=
new
BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g
=
bi.createGraphics();
g.drawImage(bg,
0
,
0
,
null
);
//
start draw text that user input
g.setColor(Color.WHITE);
g.setFont(fnt.deriveFont(20f));
int
stringwidth
=
g.getFontMetrics().stringWidth(text);
g.drawString(text,
300
-
stringwidth
/
2
,
120
);
//
end draw
g.dispose();
bi.flush();
//
encode and output result
JPEGImageEncoder encoder
=
JPEGCodec.createJPEGEncoder(response.getOutputStream());
JPEGEncodeParam param
=
encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(
1
.0f,
false
);
encoder.setJPEGEncodeParam(param);
encoder.encode(bi);
//
DEBUG: -> System.err.println( " Time Cost: " + (System.currentTimeMillis() - st));
%>
下边是界面部分
/* ======Image.jsp====== */
<%@ page language="java" contentType="text/html;charset=shift_jis" %>
<%@ page import="java.net.URLEncoder" %>
<html>
<head>
<title>Image Gen</title>
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis" />
</head>
<body>
<table width="100%" border="0" align="center" cellspacing="1">
<tr><td>
<%
request.setCharacterEncoding("shift_jis");
String text = request.getParameter("text");
if(text==null || text.length() == 0) text = "Please input !";
%>
<IMG src=<%= "ImageGen.jsp?text="+URLEncoder.encode(text,"shift_jis") %> border="0" />
</td></tr>
<tr valign="top">
<td>
<form name="gen" action="Image.jsp" method="post">
<!-- textarea name="text" rows="8" cols="70" -->
<input name="text" />
<input type="submit" value="Create Image"/>
</form>
</td>
</tr>
</table>
</body>
</html>
用到的图片和字体在这里:
http://www.blogjava.net/Files/wenzhoou/Resource_JSPImgGen.zip
posted on 2006-03-22 18:48
不曾真实 阅读(1125)
评论(3) 编辑 收藏 所属分类:
JSP