不曾真实
虽然工人可能是锤子的主人,但锤子却依然故我。 工具精确地知道自己应被如何使用,而使用者对此却只会有模糊的概念。
posts - 4,  comments - 4,  trackbacks - 0

  今天做了一个Jsp页面合成图标的例子。
  起因每次网上去合成Gmail图片,觉得不爽(偶这里联网慢拉)。然后就找了一份合成Gmail图片的代码,http://playtime.uni.cc/downloads.php?page=gmail。下下来一看,php的,而且还需要一个GD的库。不过代码倒是看明白了。很短,也很无趣。

  于是我就想能不能用Jsp合成图标阿。
  然后搜了一下,网上果然有介绍。于是就自己照猫画虎做了一个爱因斯坦的图片合成的例子。

   

r_ai.JPG



  下边是代码。总共不到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 不曾真实 阅读(1117) 评论(3)  编辑  收藏 所属分类: JSP

FeedBack:
# re: JSP图标合成的例子
2006-03-23 12:30 | big brother
Just copy some one's pages !
Sucks!  回复  更多评论
  
# re: JSP图标合成的例子
2006-03-23 13:04 | 不曾真实
绝对原创
人头担保
虽然是照猫画虎搞学人家的,不是我自己发明的方法
但是文章和代码都是我自己写的。  回复  更多评论
  
# re: JSP图标合成的例子
2008-04-24 11:59 | 噢噢
you错误
……  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 

<2006年3月>
2627281234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(1)

随笔分类

随笔档案

相册

friend

搜索

  •  

最新评论

阅读排行榜

评论排行榜