src/org/nutz/img/Images.java
public static BufferedImage createText(String content,
                                           int width,
                                           int height,
                                           String fontColor,
                                           String bgColor,
                                           String fontName,
                                           int fontSize,
                                           int fontStyle) {
        // 处理下参数
        if (Strings.isBlank(content)) {
            return null;
        }
        if (width <= 0) {
            width = 256;
        }
        if (height <= 0) {
            height = 256;
        }
        if (Strings.isBlank(fontColor)) {
            fontColor = "#FFF";
        }
        if (Strings.isBlank(bgColor)) {
            bgColor = "#000";
        }
        if (fontSize <= 0) {
            fontSize = height / 2;
        }
        if (fontStyle < 0 || fontStyle > 2) {
            fontStyle = Font.BOLD;
        }
        // 准备
        BufferedImage im;
        Graphics2D gc;
        Color colorFont = Colors.as(fontColor);
        Color colorBg = Colors.as(bgColor);
        // 判断图片格式
        int imageType = BufferedImage.TYPE_INT_RGB;
        if (colorFont.getAlpha() < 255 || colorBg.getAlpha() < 255) {
            imageType = BufferedImage.TYPE_INT_ARGB;
        }
        // 生成背景
        im = new BufferedImage(width, height, imageType);
        gc = im.createGraphics();
        gc.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gc.setBackground(colorBg);
        gc.clearRect(0, 0, width, height);
        // 写入文字
        Font cFont = Fonts.get(fontName, fontStyle, fontSize);
        gc.setColor(colorFont);
        gc.setFont(cFont);
        FontMetrics cFontM = gc.getFontMetrics(cFont);
        int cW = cFontM.stringWidth(content);
        int ascent = cFontM.getAscent(); // 取得Ascent
        int descent = cFontM.getDescent(); // 取得Descent
        int x, y;
        x = width / 2 - cW / 2;
        y = (height - (ascent + descent)) / 2 + ascent;
        gc.drawString(content, x, y);
        return im;
    }
public static BufferedImage createText(String content,
                                           int width,
                                           int height,
                                           String fontColor,
                                           String bgColor,
                                           String fontName,
                                           int fontSize,
                                           int fontStyle) {
        // 处理下参数
        if (Strings.isBlank(content)) {
            return null;
        }
        if (width <= 0) {
            width = 256;
        }
        if (height <= 0) {
            height = 256;
        }
        if (Strings.isBlank(fontColor)) {
            fontColor = "#FFF";
        }
        if (Strings.isBlank(bgColor)) {
            bgColor = "#000";
        }
        if (fontSize <= 0) {
            fontSize = height / 2;
        }
        if (fontStyle < 0 || fontStyle > 2) {
            fontStyle = Font.BOLD;
        }
        // 准备
        BufferedImage im;
        Graphics2D gc;
        Color colorFont = Colors.as(fontColor);
        Color colorBg = Colors.as(bgColor);
        // 判断图片格式
        int imageType = BufferedImage.TYPE_INT_RGB;
        if (colorFont.getAlpha() < 255 || colorBg.getAlpha() < 255) {
            imageType = BufferedImage.TYPE_INT_ARGB;
        }
        // 生成背景
        im = new BufferedImage(width, height, imageType);
        gc = im.createGraphics();
        gc.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gc.setBackground(colorBg);
        gc.clearRect(0, 0, width, height);
        // 写入文字
        Font cFont = Fonts.get(fontName, fontStyle, fontSize);
        gc.setColor(colorFont);
        gc.setFont(cFont);
        FontMetrics cFontM = gc.getFontMetrics(cFont);
        int cW = cFontM.stringWidth(content);
        int ascent = cFontM.getAscent(); // 取得Ascent
        int descent = cFontM.getDescent(); // 取得Descent
        int x, y;
        x = width / 2 - cW / 2;
        y = (height - (ascent + descent)) / 2 + ascent;
        gc.drawString(content, x, y);
        return im;
    }
public static BufferedImage createText ( String content , int width , int height , String fontColor , String bgColor , String fontName , int fontSize , int fontStyle ) { // 处理下参数 if ( Strings . isBlank ( content ) ) { return null ; } if ( width <= 0 ) { width = 256 ; } if ( height <= 0 ) { height = 256 ; } if ( Strings . isBlank ( fontColor ) ) { fontColor = "#FFF" ; } if ( Strings . isBlank ( bgColor ) ) { bgColor = "#000" ; } if ( fontSize <= 0 ) { fontSize = height / 2 ; } if ( fontStyle < 0 || fontStyle > 2 ) { fontStyle = Font . BOLD ; } // 准备 BufferedImage im ; Graphics2D gc ; Color colorFont = Colors . as ( fontColor ) ; Color colorBg = Colors . as ( bgColor ) ; // 判断图片格式 int imageType = BufferedImage . TYPE_INT_RGB ; if ( colorFont . getAlpha ( ) < 255 || colorBg . getAlpha ( ) < 255 ) { imageType = BufferedImage . TYPE_INT_ARGB ; } // 生成背景 im = new BufferedImage ( width , height , imageType ) ; gc = im . createGraphics ( ) ; gc . setRenderingHint ( RenderingHints . KEY_ANTIALIASING , RenderingHints . VALUE_ANTIALIAS_ON ) ; gc . setBackground ( colorBg ) ; gc . clearRect ( 0 , 0 , width , height ) ; // 写入文字 Font cFont = Fonts . get ( fontName , fontStyle , fontSize ) ; gc . setColor ( colorFont ) ; gc . setFont ( cFont ) ; FontMetrics cFontM = gc . getFontMetrics ( cFont ) ; int cW = cFontM . stringWidth ( content ) ; int ascent = cFontM . getAscent ( ) ; // 取得Ascent int descent = cFontM . getDescent ( ) ; // 取得Descent int x , y ; x = width / 2 - cW / 2 ; y = ( height - ( ascent + descent ) ) / 2 + ascent ; gc . drawString ( content , x , y ) ; return im ; }
文字生成图片

@param content
文字内容
@param width
图片宽度，默认256
@param height
图片高度，默认256
@param fontColor
文字颜色 默认白色
@param bgColor
背景颜色 默认黑色
@param fontName
字体名称 需运行环境中已有该字体名称
@param fontSize
字体大小
@param fontStyle
字体样式 Font.PLAIN || Font.BOLD || Font.ITALIC
@return 图像
文字生成图片