JSP/JAVA

          JAVA SERVER PAGES

          用JAVA生成圖片的簡單例子

           

          import java.awt.*;    
          import java.awt.image.*;    
          import java.io.*;    
          import javax.imageio.*;    
          import java.awt.font.*;    
          import java.awt.geom.*;    
             
          public class CreateImage     
          {    
              
          public static void main(String[] args) throws Exception    
              
          {    
                  
          int width = 100;    
                  
          int height = 100;    
                  String s 
          = "你好";    
                      
                  File file 
          = new File("c:/image.jpg");    
                      
                  Font font 
          = new Font("Serif", Font.BOLD, 10);    
                  BufferedImage bi 
          = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);    
                  Graphics2D g2 
          = (Graphics2D)bi.getGraphics();    
                  g2.setBackground(Color.WHITE);    
                  g2.clearRect(
          00, width, height);    
                  g2.setPaint(Color.RED);    
                      
                  FontRenderContext context 
          = g2.getFontRenderContext();    
                  Rectangle2D bounds 
          = font.getStringBounds(s, context);    
                  
          double x = (width - bounds.getWidth()) / 2;    
                  
          double y = (height - bounds.getHeight()) / 2;    
                  
          double ascent = -bounds.getY();    
                  
          double baseY = y + ascent;    
                      
                  g2.drawString(s, (
          int)x, (int)baseY);    
                      
                  ImageIO.write(bi, 
          "jpg", file);    
              }
              
          }
          結果:

          posted on 2009-06-27 12:19 C雞雞 閱讀(1526) 評論(0)  編輯  收藏 所屬分類: java


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          My Links

          Blog Stats

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          友情鏈接

          學習網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 荣昌县| 普宁市| 滁州市| 新绛县| 宁明县| 边坝县| 军事| 彰化市| 环江| 昔阳县| 清原| 长宁县| 上杭县| 桐城市| 平陆县| 比如县| 桐乡市| 崇文区| 镇平县| 寿阳县| 孟州市| 镇沅| 姜堰市| 黄平县| 普洱| 兴山县| 乐至县| 华亭县| 彩票| 伊金霍洛旗| 普洱| 河东区| 金坛市| 郧西县| 湖南省| 吉安县| 砀山县| 扎赉特旗| 哈密市| 安康市| 剑阁县|