package com.nantian.sc.cloud.gateway.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class ImageUtil {

    private int width = 130;
    private int height = 35;

    public BufferedImage createImage(String text) {
        BufferedImage image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_BGR);
        Graphics graphics = image.getGraphics();
        //graphics.setColor(Color.lightGray);
        graphics.fillRect(0, 0, this.width, this.height);
        graphics.setColor(Color.BLACK);

        //graphics.setFont(new Font("楷体", Font.BOLD, 30));
        //graphics.drawString(text, 60, 30);
        Random ran = new Random();
        String[] split = text.split("");
        Color[] colors = {Color.BLUE,Color.BLACK,Color.RED,Color.GREEN,Color.ORANGE,Color.DARK_GRAY,Color.MAGENTA,Color.CYAN};
        for(int i=0; i<4; i++) {
            graphics.setColor(colors[ran.nextInt(8)]); //设置画笔随机颜色
            //设置字体、样式、尺寸
            graphics.setFont(new Font("宋体", Font.BOLD, 25+ran.nextInt(5)));
            //设置所写内容、x坐标、y坐标
            graphics.drawString(split[i], 10+30*i, 20+ran.nextInt(10));
        }

        return image;
    }
}
