码迷,mamicode.com
首页 > 编程语言 > 详细

java生成一张图片

时间:2018-02-19 22:23:51      阅读:369      评论:0      收藏:0      [点我收藏+]

标签:rac   data-   gpo   tis   jpg   users   color   gets   clear   

public class CreateImage {
	public static void main(String[] args) throws Exception{
		int width = 100;   
        int height = 100;   
        String s = "你好";   
           
        File file = new File("/Users/tengxin/Pictures/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(0, 0, 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);    
	}
}


java生成一张图片

标签:rac   data-   gpo   tis   jpg   users   color   gets   clear   

原文地址:https://www.cnblogs.com/llguanli/p/8454478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!