文字图片

import java.awt.*;
import java.awt.image.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

import javax.imageio.ImageIO;

/**
 * 创建文字图片
 * @author yuki_ho
 *
 */
public class Font2Image {
    // 默认格式
    private static final String FORMAT_NAME = "JPG";
    // 默认 宽度
    private static final int WIDTH = 100;
    // 默认 高度
    private static final int HEIGHT =100;

    /**
     * 创建图片
     * @param content 内容
     * @param font  字体
     * @param width 宽
     * @param height 高
     * @return
     */
    private static BufferedImage createImage(String content,Font font,Integer width,Integer height){
        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.BLACK);

        FontRenderContext context = g2.getFontRenderContext();
        Rectangle2D bounds = font.getStringBounds(content, context);
        double x = (width - bounds.getWidth()) / 2;
        double y = (height - bounds.getHeight()) / 2;
        double ascent = -bounds.getY();
        double baseY = y + ascent;

        g2.drawString(content, (int)x, (int)baseY);

        return bi;
    }

    /**
     * 获取 图片
     * @param content 内容
     * @param font  字体
     * @param width 宽
     * @param height 高
     * @return
     */
    public static BufferedImage getImage(String content,Font font,Integer width,Integer height){
        width=width==null?WIDTH:width;
        height=height==null?HEIGHT:height;
        if(null==font)
            font = new Font("Serif", Font.BOLD, 11);
        return createImage(content, font, width, height);
    }

    /**
     * 获取 图片
     * @param content 内容
     * @param width 宽
     * @param height 高
     * @return
     */
    public static BufferedImage getImage(String content,Integer width,Integer height){

        return getImage(content, null,width, height);
    }

    /**
     * 获取图片
     * @param content 内容
     * @return
     */
    public static BufferedImage getImage(String content){

        return getImage(content, null, null);
    }

    /**
     *  获取图片
     * @param content 内容
     * @param font 字体
     * @param width 宽
     * @param height 高
     * @param destPath 输出路径
     * @throws IOException
     */
    public static void getImage(String content,Font font ,Integer width,Integer height,String destPath) throws IOException{
        mkdirs(destPath);
        String file = UUID.randomUUID().toString()+".jpg";
        ImageIO.write(getImage(content,font,width,height),FORMAT_NAME, new File(destPath+"/"+file));
    }

    /**
     * 获取图片
     * @param content 内容
     * @param font 字体
     * @param width 宽
     * @param height 高
     * @param output 输出流
     * @throws IOException
     */
    public static void getImage(String content,Font font,Integer width,Integer height, OutputStream output) throws IOException{
        ImageIO.write(getImage(content,font,width,height), FORMAT_NAME, output);
    }

    /**
     * 获取图片
     * @param content 内容
     * @param width 宽
     * @param height 高
     * @param destPath 输出路径
     * @throws IOException
     */
    public static void getImage(String content,Integer width,Integer height,String destPath) throws IOException{
        getImage(content, null, width, height, destPath);
    }

    /**
     * 获取图片
     * @param content 内容
     * @param width 宽
     * @param height 高
     * @param output 输出流
     * @throws IOException
     */
    public static void getImage(String content,Integer width,Integer height, OutputStream output) throws IOException{
        getImage(content, null, width, height, output);
    }


    /**
     * 创建 目录
     * @param destPath
     */
    public static void mkdirs(String destPath) {
        File file =new File(destPath);
        //当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
        if (!file.exists() && !file.isDirectory()) {
            file.mkdirs();
        }
    }

    public static void main(String[] args) throws Exception {
        getImage("我是待转换内容", 400, 500, "E:\\data");

    }

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值