熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java文字轉成圖片顯示

2013-11-23 19:24:36  來源: Java核心技術 

  package rge;
import javaioIOException;
import javaioPrintWriter;
import javaio*;
import javaawt*;
import javaawtimage*;
import javaxservlet*;
import javaxservlethttp*;
import decjpeg*;
/**
 * @author Liao Xue Feng
 */
public class CreateImageServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request HttpServletResponse response)
            throws ServletException IOException
    {
        responsesetContentType(image/jpeg);
        createImage(responsegetOutputStream());
    }
    private void createImage(OutputStream out) {
    
     String text = 測試;
    
        int width = ;
        int height = textlength()/*;
        BufferedImage bi = new BufferedImage(width height BufferedImageTYPE_INT_RGB);
        GraphicsD g = bicreateGraphics();
        // set background:
        gsetBackground(ColorGREEN);
        gclearRect( width height);

  Font mFont = new Font(宋體FontPLAIN);//默認字體
        gsetColor(new Color(IntegerparseInt()));
        gsetFont(mFont);
       
       
       
        int x=;
        int y=;
        int count = textlength();
        int i = ;
        String getone = null;
        while(count >)
        {
         getone = textsubstring(ii+);
         if(i % == )
         {
          y = y + ;
          x = ;
         }
         //gdrawString(IntegertoString(i)xy);
         gdrawString(getonexy);
         x = x + ;
         i++ ;
         count;
        }
       
       
       
        // end draw:
        gdispose();
        biflush();
        // encode:
        JPEGImageEncoder encoder = JPEGCodeccreateJPEGEncoder(out);
        JPEGEncodeParam param = encodergetDefaultJPEGEncodeParam(bi);
        paramsetQuality(f false);
        encodersetJPEGEncodeParam(param);
        try {
            encoderencode(bi);
        }
        catch(IOException ioe) {
            ioeprintStackTrace();
        }
    }
}

  indejsp頁面:

 <%@ page language=java import=javautil* pageEncoding=GB%>
<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN>
<html>
  <head>
   
    <title>系統測試(From:網絡大本營Http://)</title>
   
 <meta httpequiv=pragma content=nocache>
 <meta httpequiv=cachecontrol content=nocache>
 <meta httpequiv=expires content=>   
 <meta httpequiv=keywords content=keywordkeywordkeyword>
 <meta httpequiv=description content=This is my page>
 <!
 <link rel=stylesheet type=text/css >
 >
  <meta httpequiv=ContentType content=text/html; charset=gb><style type=text/css>
<!
body {
 backgroundcolor: #FF;
}
>
</style></head>
 
  <body>
<img src=servlet/CreateImageServlet>
  </body>
</html>


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26817.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.