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雞雞 閱讀(1533) 評論(0)  編輯  收藏 所屬分類: java


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


          網站導航:
           

          My Links

          Blog Stats

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          友情鏈接

          學習網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 洪湖市| 睢宁县| 乐昌市| 甘肃省| 耒阳市| 安义县| 综艺| 无为县| 上林县| 延边| 柳河县| 将乐县| 苍溪县| 大方县| 玛纳斯县| 韩城市| 六盘水市| 炉霍县| 嘉鱼县| 凭祥市| 湛江市| 莱西市| 庆云县| 通河县| 略阳县| 保康县| 炎陵县| 南投市| 泸溪县| 溧阳市| 江北区| 扶沟县| 宣武区| 唐河县| 迁西县| 永吉县| 大渡口区| 福海县| 农安县| 新郑市| 莱西市|