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鸡鸡 阅读(1512) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航:
 

My Links

Blog Stats

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

友情链接

学习网站

搜索

最新评论

阅读排行榜

评论排行榜