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

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          友情鏈接

          學習網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 西和县| 舞钢市| 永善县| 临武县| 奈曼旗| 南溪县| 惠东县| 磐石市| 合阳县| 利川市| 和硕县| 富裕县| 利津县| 岑巩县| 手游| 镇宁| 泰宁县| 嘉祥县| 平乐县| 盈江县| 平塘县| 安阳县| 莆田市| 保德县| 满洲里市| 康保县| 志丹县| 邵阳市| 东辽县| 南部县| 丹阳市| 旬阳县| 宁津县| 芦山县| 平安县| 牡丹江市| 教育| 兴隆县| 磐石市| 新民市| 广宁县|