代码如下

package demo;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageGenerater {
	public static void main(String[] args) throws FileNotFoundException, IOException {
		//1构造BufferedImage对象,用于表示内存中的头像数据;
		BufferedImage image = new BufferedImage(60,32,BufferedImage.TYPE_BYTE_BINARY);
		
		//2编辑图像
		//2.1获取Graphics(绘图)对象;
		Graphics g = image.getGraphics();
		
		//2.2设置颜色
		g.setColor(Color.WHITE);
		
		g.setFont(new Font("SimSong", Font.ITALIC,20));
		
		
		String text = "";
		for(int i = 0;i < 4; i++) {
			text += (int) (Math.random() * 10);
		}

		g.drawString(text, 5, 24);
		//3输出图像
		//arg0:im,图像对象;
		//arg1:formatName,图像格式,如png,jpg,bmp等;
		ImageIO.write(image, "png",new FileOutputStream("C:/Temp/a.png"));
	}
}

简单效果图

java校验码生成器 java验证码生成_Math