CaptchaUtil.java

验证码工具类

package captcha;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;

/**
 * 验证码工具类
 *
 * @author ZengWenFeng
 * @date 2023.09.16
 * @email [email protected]
 * @mobile 13805029595
 */
public class CaptchaUtil
{
	/**
	 * 数字、大写英文字母、小写英文字母
	 */
	public static final String ARR_CHAR = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	
	/**
	 * 数字、大写英文字母、
	 */
	public static final String ARR_CHAR_1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	/**
	 * 数字、大写英文字母、去掉了1,0,i,o几个容易混淆的字符
	 */
	public static final String ARR_CHAR_2 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
	
	
	/**
	 * 生成随机码
	 * 
	 * @author ZengWenFeng
	 * @date 2023.09.16
	 * @email [email protected]
	 * @mobile 13805029595
	 * @param length 随机码的长度
	 * @return
	 */
	public static String getRandomCode(String source, int length)
	{
		if (source == null || source.length() <= 0)
		{
			source = ARR_CHAR;
		}
		
		StringBuilder code = new StringBuilder();
		Random random = new Random();

		//
		for (int i = 0; i < length; i++)
		{
			int index = random.nextInt(source.length());
			code.append(source.charAt(index));
		}

		//
		return code.toString();
	}
	
	
	/**
	 * 生成随机码
	 * 
	 * @author ZengWenFeng
	 * @date 2023.09.16
	 * @email [email protected]
	 * @mobile 13805029595
	 * @param length 随机码的长度
	 * @return
	 */
	public static String getRandomCode(int length)
	{
		return getRandomCode(null, length);
	}

	/**
	 * 生成随机码
	 * 
	 * @author ZengWenFeng
	 * @date 2023.09.16
	 * @email [email protected]
	 * @mobile 13805029595
	 * @param length 随机码的长度
	 * @return
	 */
	public static String getRandomCode2(int length)
	{
		StringBuilder code = new StringBuilder();
		Random r = new Random();

		for (int i = 0; i < length; i++)
		{
			//随机类型:0-数字、1-小写字母、2-大写字母
			int type = r.nextInt(3);

			// 数字
			if (type == 0)
			{
				code.append(r.nextInt(10));
			}
			// 小写字母
			else if (type == 1)
			{
				char ch2 = (char) (r.nextInt(26) + 97);
				code.append(ch2);
			}
			// 大写字母
			else if (type == 2)
			{
				char ch3 = (char) (r.nextInt(26) + 65);
				code.append(ch3);
			}
			// 如果出现异常
			else
			{
				code.append("?");
			}

		}

		//
		return code.toString();
	}
	
	
	/**
	 * 生成图片
	 * 
	 * @author ZengWenFeng
	 * @date 2023.09.16
	 * @email [email protected]
	 * @mobile 13805029595
	 * @param length 随机码的长度
	 * @return
	 */
	public static BufferedImage getImage()
	{
		int width = 200; // 图片宽度
		int height = 50; // 图片高度
		String captchaText = getRandomCode(4); // 生成4位随机验证码

		// 创建一个BufferedImage对象
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2d = image.createGraphics();

		// 设置背景色
		g2d.setColor(Color.WHITE);
		g2d.fillRect(0, 0, width, height);

		// 设置字体和颜色
		g2d.setFont(new Font("Arial", Font.BOLD, 36));
//		g2d.setColor(Color.BLACK);//黑色有时候还是看不清楚
		g2d.setColor(Color.RED);

		// 将验证码文本绘制到图片上,调整位置竟然居中,根据
		//width = 200 height = 50 x = 50 Y = 35
		//width = 200 height = 80 x = 50 Y = 50
		g2d.drawString(captchaText, 50, 35);

		// 添加噪点,干扰点
		// width = 200 height = 50
		// max 200 * 50 = 10000
//		int max = 1000;
//		int max = 700;
		int max = 500;
		Random random = new Random();
		for (int i = 0; i < max; i++)
		{
			//
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			
			//
			g2d.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
			g2d.fillRect(x, y, 2, 2);
		}

		g2d.dispose();

		return image;
	}

	/**
	 * 仅仅提供测试
	 * 
	 * @author ZengWenFeng
	 * @date 2023.09.16
	 * @email [email protected]
	 * @mobile 13805029595
	 * @param args 
	 * @return
	 */
	public static void main(String[] args)
	{
//		test();
		
		BufferedImage image = getImage();
		try
		{
			String filename = UUID.randomUUID().toString().replace("-", "");
			
			
			ImageIO.write(image, "png", new File("d://" + filename + ".png"));
			System.out.println("ok!! --  " + filename);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 测试
	 * 
	 * @author ZengWenFeng
	 * @date 2023.09.16
	 * @email [email protected]
	 * @mobile 13805029595
	 * @param args 
	 * @return
	 */
	public static void test()
	{
		int width = 200; // 图片宽度
		int height = 50; // 图片高度
		String captchaText = getRandomCode(4); // 生成4位随机验证码

		// 创建一个BufferedImage对象
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2d = image.createGraphics();

		// 设置背景色
		g2d.setColor(Color.WHITE);
		g2d.fillRect(0, 0, width, height);

		// 设置字体和颜色
		g2d.setFont(new Font("Arial", Font.BOLD, 36));
//		g2d.setColor(Color.BLACK);//黑色有时候还是看不清楚
		g2d.setColor(Color.RED);

		// 将验证码文本绘制到图片上,调整位置竟然居中,根据
		//width = 200 height = 50 x = 50 Y = 35
		//width = 200 height = 80 x = 50 Y = 50
		g2d.drawString(captchaText, 50, 35);

		// 添加噪点,干扰点
		// width = 200 height = 50
		// max 200 * 50 = 10000
//		int max = 1000;
//		int max = 700;
		int max = 500;
		Random random = new Random();
		for (int i = 0; i < max; i++)
		{
			//
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			
			//
			g2d.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
			g2d.fillRect(x, y, 2, 2);
		}

		g2d.dispose();

		try
		{
			String filename = UUID.randomUUID().toString().replace("-", "");
			
			
			ImageIO.write(image, "png", new File("d://" + filename + ".png"));
			System.out.println("ok!! --  " + filename);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}

CaptchaUtil.java_第1张图片

CaptchaUtil.java_第2张图片

CaptchaUtil.java_第3张图片

kaptcha-2.3.2.jar-CSDN博客

你可能感兴趣的:(java,java)