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

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          友情鏈接

          學習網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 平度市| 宜丰县| 屏山县| 信阳市| 杂多县| 阳城县| 汪清县| 虎林市| 呼伦贝尔市| 社会| 通渭县| 喜德县| 徐闻县| 哈尔滨市| 秦安县| 柘城县| 图片| 栾川县| 盐池县| 固安县| 莱州市| 南部县| 天水市| 黄浦区| 临桂县| 台南市| 松原市| 邵武市| 普宁市| 合江县| 墨竹工卡县| 吕梁市| 武川县| 成安县| 天水市| 全州县| 铜陵市| 东丽区| 冕宁县| 新余市| 静海县|