Java io生成验证码及easy-captcha实现验证码功能
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import java.util.UUID;
public class CheckDemo1 {
public static void main(String[] args) throws Exception {
//for (int i = 0; i < 100; i++) {
check();
//}
}
public static void check() throws Exception {
int w = 160;
int h = 60;
int len = 5;
Random rand = new Random();
BufferedImage i = new BufferedImage(w, h, 2);
Graphics2D g = i.createGraphics();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
g.setColor(new Color(251, 251, 251));
g.fillRect(0, 0, w, h);
Font font = new Font("宋体", Font.BOLD, 15);
font = font.deriveFont(Font.PLAIN, 55f);
g.setFont(font);
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
double radianPercent = 0D;
for (int n = 0; n < 20; n++) {
font = font.deriveFont(Font.PLAIN, rand.nextFloat() * 15 + 15);
g.setFont(font);
g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(10) + 50));
int x = rand.nextInt(w);
int y = rand.nextInt(h);
g.drawString(String.valueOf(str.charAt(rand.nextInt(str.length()))), x, y);
}
StringBuilder sok = new StringBuilder(len);
g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(25) + 220));
for (int n = 0; n < len; n++) {
font = font.deriveFont(Font.PLAIN, rand.nextFloat() * 35 + 30);
g.setFont(font);
int x = n * 32 + 5;
int y = rand.nextInt(35) + 20;
radianPercent = Math.PI * (rand.nextInt(35) / 180D);
if (rand.nextBoolean()) radianPercent = -radianPercent;
g.rotate(radianPercent, x, y);
String sss = String.valueOf(str.charAt(rand.nextInt(str.length())));
sok.append(sss);
g.drawString(sss, x, y);
g.rotate(-radianPercent, x, y);
}
Point[] points = {new Point(0, 0), new Point(10, 50), new Point(30, 6), new Point(60, 55), new Point(80, 3), new Point(160, 60)};
GeneralPath path = new GeneralPath();
path.moveTo(points[0].x, points[0].y);
for (int i2 = 0; i2 < points.length - 1; ++i2) {
Point sp = points[i2];
Point ep = points[i2 + 1];
Point c1 = new Point((sp.x + ep.x) / 2, sp.y);
Point c2 = new Point((sp.x + ep.x) / 2, ep.y);
path.curveTo(c1.x, c1.y, c2.x, c2.y, ep.x, ep.y);
}
g.setStroke(new BasicStroke(rand.nextInt(6) + 2));
g.draw(path);
g.setColor(new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255), rand.nextInt(25) + 220));
int yy = rand.nextInt(15) + 15;// y
int hh = rand.nextInt(20) + 10;// 高度
int aa = rand.nextInt(60) + 20;//
// x
for (int x = 10; x < w; x++) {
int y = (int) (yy + hh * Math.sin(x * Math.PI / aa));
// g.drawLine(x, (int) y, x, (int) y);
g.fillOval(x, y, 3, 3);
}
// 输出验证码
System.out.println(sok);
g.dispose();
ImageIO.write(i, "png", new File("c:/cc/" + UUID.randomUUID() + ".png"));
Runtime run = Runtime.getRuntime();
}
}
easy-captcha
EasyCaptcha 是一个简单易用的 Java 验证码生成工具,它提供了简单的 API 来生成和验证各种类型的验证码。
要使用 EasyCaptcha,首先你需要添加 EasyCaptcha 的依赖到你的项目中。可以通过 Maven 或 Gradle 进行添加。
在 Maven 中,你可以在 pom.xml
文件中添加以下依赖:
<dependency>
<groupId>com.github.qcloudsms</groupId>
<artifactId>easycaptcha</artifactId>
<version>1.5.1</version>
</dependency>
在 Gradle 中,你可以在 build.gradle
文件的 dependencies
部分中添加以下依赖:
implementation 'com.github.qcloudsms:easycaptcha:1.5.1'
一旦你添加了 EasyCaptcha 的依赖,你就可以开始使用它了。
png类型
@RequestMapping(“/hello”)
public void hello(HttpServletResponse response) throws IOException {// png类型
SpecCaptcha captcha = new SpecCaptcha(130, 48);
String text = captcha.text();// 获取验证码的字符
char[] chars = captcha.textChar();// 获取验证码的字符数组
System.out.println("验证码:"+text);
System.out.println(chars);
// 输出验证码
captcha.out(response.getOutputStream());
}
gif类型
@RequestMapping(“/hello”)
public void hello(HttpServletResponse response) throws IOException {// 三个参数分别为宽、高、位数
GifCaptcha gifCaptcha = new GifCaptcha(100, 48, 4);
// 设置类型:字母数字混合
gifCaptcha.setCharType(Captcha.TYPE_DEFAULT);
//获取验证码
String text = gifCaptcha.text();
System.out.println("验证码为:"+text);
// 输出验证码
gifCaptcha.out(response.getOutputStream());
}
中文类型
@RequestMapping(“/hello”)
public void hello(HttpServletResponse response) throws IOException {// 中文类型
ChineseCaptcha captcha = new ChineseCaptcha(130, 48);
//获取验证码
String text = captcha.text();
System.out.println("验证码为:"+text);
// 输出验证码
captcha.out(response.getOutputStream());
}
算术类型
@RequestMapping(“/hello”)
public void hello(HttpServletResponse response) throws IOException {// 算术类型
ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
captcha.setLen(3); // 几位数运算,默认是两位
captcha.getArithmeticString(); // 获取运算的公式:4-9+1=?
String text = captcha.text();// 获取运算的结果:-4
System.out.println("计算结果为:"+text);
// 输出验证码
captcha.out(response.getOutputStream());
}
用户输入验证码后,你可以通过比较用户输入的验证码和生成的验证码的值来验证其正确性。
EasyCaptcha 还提供了一些其他的配置选项,例如自定义验证码的宽度、高度、字体、干扰线等等。
注意:请确保在使用验证码时遵循适当的安全措施,以防止滥用、暴力破解或其他恶意行为。
还没有评论,来说两句吧...