Java生成验证码

Java版生成简易验证码:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;

public final class ImageUtil {
	
	// 验证码字符集
	private static final char[] chars = { 
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
		'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 
		'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
	// 字符数量
	private static final int SIZE = 4;
	// 干扰线数量
	private static final int LINES = 5;
	// 宽度
	private static final int WIDTH = 80;
	// 高度
	private static final int HEIGHT = 40;
	// 字体大小
	private static final int FONT_SIZE = 30;

	/**
	 * 生成随机验证码及图片
	 */
	public static Object[] createImage() {
		StringBuffer sb = new StringBuffer();
		// 1.创建空白图片
		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		// 2.获取图片画笔
		Graphics graphic = image.getGraphics();
		// 3.设置画笔颜色
		graphic.setColor(Color.LIGHT_GRAY);
		// 4.绘制矩形背景
		graphic.fillRect(0, 0, WIDTH, HEIGHT);
		// 5.画随机字符
		Random ran = new Random();
		for (int i = 0; i <SIZE; i++) {
			// 取随机字符索引
			int n = ran.nextInt(chars.length);
			// 设置随机颜色
			graphic.setColor(getRandomColor());
			// 设置字体大小
			graphic.setFont(new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE));
			// 画字符
			graphic.drawString(chars[n] + "", i * WIDTH / SIZE, HEIGHT / 2);
			// 记录字符
			sb.append(chars[n]);
		}
		// 6.画干扰线
		for (int i = 0; i < LINES; i++) {
			// 设置随机颜色
			graphic.setColor(getRandomColor());
			// 随机画线
			graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
					ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
		}
		// 7.返回验证码和图片
		return new Object[]{sb.toString(), image};
	}

	/**
	 * 随机取色
	 */
	public static Color getRandomColor() {
		Random ran = new Random();
		Color color = new Color(ran.nextInt(256),ran.nextInt(256), ran.nextInt(256));
		return color;
	}
	
	public static void main(String[] args) throws IOException {
		Object[] objs = createImage();
		BufferedImage image = (BufferedImage) objs[1];
		// /home/soft01/1.png
		OutputStream os = 
			new FileOutputStream("d:/1.png");
		ImageIO.write(image, "png", os);
		os.close();
	}

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成验证码可以使用 Java 提供的 BufferedImage 类来创建图片,并在图片上绘制随机生成验证码。以下是一个简单的 Java 代码示例: ``` import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.Random; public class CaptchaGenerator { private static final int WIDTH = 120; // 图片宽度 private static final int HEIGHT = 40; // 图片高度 private static final int CODE_LENGTH = 4; // 验证码长度 private static final String CODE_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // 验证码字符集 public static BufferedImage generateCaptcha() { // 创建一个 BufferedImage 对象并设置宽度和高度 BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); // 获取 Graphics 对象以便在图片上绘制 Graphics g = image.getGraphics(); // 设置背景颜色 g.setColor(Color.WHITE); g.fillRect(0, 0, WIDTH, HEIGHT); // 绘制验证码 Random random = new Random(); StringBuilder code = new StringBuilder(); for (int i = 0; i < CODE_LENGTH; i++) { int index = random.nextInt(CODE_CHARS.length()); char c = CODE_CHARS.charAt(index); code.append(c); g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); g.setFont(new Font("Arial", Font.BOLD, 24)); g.drawString(String.valueOf(c), i * 30 + 10, 30); } // 添加噪声点 for (int i = 0; i < 50; i++) { int x = random.nextInt(WIDTH); int y = random.nextInt(HEIGHT); g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); g.fillOval(x, y, 2, 2); } // 添加噪声线 for (int i = 0; i < 3; i++) { int x1 = random.nextInt(WIDTH); int y1 = random.nextInt(HEIGHT); int x2 = random.nextInt(WIDTH); int y2 = random.nextInt(HEIGHT); g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256))); g.drawLine(x1, y1, x2, y2); } // 释放 Graphics 对象 g.dispose(); return image; } } ``` 该代码会生成一个宽度为 120、高度为 40 的验证码图片。验证码由 4 个随机字符组成,字符集包括大小写字母和数字。在图片上会添加一些噪声点和噪声线以增加验证码的复杂度。可以调整字符集、验证码长度、噪声点数量和噪声线数量等参数以满足需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值