代码如下
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 {
BufferedImage image = new BufferedImage(60,32,BufferedImage.TYPE_BYTE_BINARY);
Graphics g = image.getGraphics();
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);
ImageIO.write(image, "png",new FileOutputStream("C:/Temp/a.png"));
}
}
简单效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/144ae7254738cddb88fb18963302b379.png)