JSP/JAVA

          JAVA SERVER PAGES

          用JAVA生成圖片的簡(jiǎn)單例子

           

          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);    
              }
              
          }
          結(jié)果:

          posted on 2009-06-27 12:19 C雞雞 閱讀(1534) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          My Links

          Blog Stats

          常用鏈接

          留言簿

          隨筆分類(lèi)

          隨筆檔案

          文章分類(lèi)

          文章檔案

          友情鏈接

          學(xué)習(xí)網(wǎng)站

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 德化县| 扶绥县| 宜昌市| 巴南区| 明光市| 读书| 新民市| 治县。| 巴楚县| 温泉县| 枞阳县| 喜德县| 炉霍县| 布拖县| 浦北县| 株洲县| 双鸭山市| 饶平县| 大新县| 郓城县| 桐乡市| 拜泉县| 台湾省| 虞城县| 克什克腾旗| 海林市| 曲阳县| 潜江市| 乌鲁木齐县| 上蔡县| 华宁县| 五台县| 蓬莱市| 巴青县| 榆社县| 曲周县| 广饶县| 曲阜市| 扶绥县| 汉阴县| 麻栗坡县|