public class Utils_Image {
static int result_num = 0;
// line_number : 干扰线的数量
public static BufferedImage createImage(Integer line_number) {
result_num = 0;
int width = 120;
int height = 30;
// 获取图片对象
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取画图工具
Graphics graphics = bufferedImage.getGraphics();
// 设置画笔颜色 并填满整个画板
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, width, height);
// 设置边框
graphics.setColor(Color.RED);
graphics.drawRect(0, 0, width - 1, height - 1);
// 随机生成一个加法公式
for (int i = 1; i <= 5; i++) {
// 生成随机的Y轴数值
int height_ran = new Random().nextInt(height / 4) + height / 2;
// 生成随机的X轴数值
int width_ran = new Random().nextInt((width / 5) / 2) + width / 5 * (i - 1);
// 将 + 号写入图片
if (i == 2) graphics.drawString("+", width_ran, height_ran);
// 将加法所需的数字写入
if (i == 1 || i == 3) {
int i1 = new Random().nextInt(50) + 1;
graphics.drawString(i1 + "", width_ran, height_ran);
// 产生图片中加法的和
result_num += i1;
}
// 将生成的 = 加入图片
if (i == 4) graphics.drawString("=", width_ran, height_ran);
if (i == 5) graphics.drawString("?", width_ran, height_ran);
}
// 生成随机干扰线
for (int i = 0; i < line_number; i++) {
// 随机生成随机的坐标点
int start_width = 0;
int start_height = 0;
int end_width = 0;
int end_height = 0;
for (int j = 1; j <= 4; j++) {
if (j % 2 == 0) {
start_width = new Random().nextInt(width);
end_width = new Random().nextInt(width);
}
if (j % 2 != 0) {
start_height = new Random().nextInt(height);
end_height = new Random().nextInt(height);
}
}
// 将干扰线写入图片
graphics.drawLine(start_width, start_height, end_width, end_height);
}
return bufferedImage;
}
}
Java >验证码生成
猜你喜欢
转载自blog.csdn.net/weixin_43309893/article/details/117856316
今日推荐
周排行