Java 验证码生成,随机颜色字体
程序员文章站
2022-03-19 16:05:43
验证码生成工具类package com.kongjs.utils;import java.awt.*;import java.awt.image.BufferedImage;import java.util.Random;public class YanCode { private static int width; private static int height; private static int codeLen; private int getR...
验证码生成工具类
package com.kongjs.utils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class YanCode {
private static int width;
private static int height;
private static int codeLen;
private int getRandomInt(int n1, int n2) {
return (int) Math.abs((Math.random() * (n2 - n1)));
}
private Font getRandomFont() {
String[] fonts = {"Georgia", "Verdana", "Arial", "Tahoma", "Time News Roman", "Courier New", "Arial Black", "Quantzite"};
int fontIndex = (int) Math.round(Math.random() * (fonts.length - 1));
int fontSize = (int) Math.round(Math.random() * 4 + 36);
return new Font(fonts[fontIndex], Font.PLAIN, fontSize);
}
private Color getColor() {
Random random = new Random();
int r = 0, g = 0, b = 0;
for (int i = 1; i < 255; i++) {
for (int j = 1; j < 255; j++) {
r = random.nextInt(i) + 1;
g = random.nextInt(j) + 1;
b = random.nextInt((i + j) / 2) + 1;
}
}
return new Color(r, g, b);
}
private String getStrCode() {
char[] ch1 = new char[26];
char[] ch2 = new char[26];
int a = 97;
int A = 65;
for (int i = 0; i < ch1.length; i++) {
ch1[i] = (char) (a++);
ch2[i] = (char) (A++);
}
StringBuilder sb = new StringBuilder();
Random r = new Random();
for (int i = 2; i < 33; i++) {
sb.append(r.nextInt(i - 1));
sb.append(ch1[r.nextInt(ch1.length)]);
sb.append(r.nextInt(i));
sb.reverse();
sb.append(ch2[r.nextInt(ch2.length)]);
sb.append(r.nextInt(i - 1));
sb.reverse();
}
String str = String.valueOf(sb);
StringBuilder code = new StringBuilder();
for (int i = 0; i < codeLen; i++) {
code.append(str.charAt(r.nextInt(33)));
}
return new String(code);
}
private BufferedImage bufferedImage = getBufferedImage();
synchronized private BufferedImage getBufferedImage() {
if (bufferedImage == null) {
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}
return bufferedImage;
}
private BufferedImage getImage(String strCode) {
Graphics graphics = bufferedImage.getGraphics();
graphics.setColor(getColor());
graphics.fillRect(0, 0, width, height);
graphics.setFont(getRandomFont());
graphics.setColor(getColor());
int x = (int) Math.round(width * 0.2);
int y = (int) Math.round(height * 0.7);
graphics.drawString(strCode, x, y);
graphics.setColor(getColor());
graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
graphics.setColor(getColor());
graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
graphics.setColor(getColor());
graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
graphics.setColor(getColor());
graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
graphics.setColor(getColor());
graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
graphics.setColor(getColor());
graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
graphics.setColor(getColor());
graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
graphics.setColor(getColor());
graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
graphics.setColor(getColor());
graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
graphics.setColor(getColor());
// 可以用循环替代重复的
for (int i = 0; i <10 ; i++) {
graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
graphics.setColor(getColor());
}
return bufferedImage;
}
private YanCode() {
}
static {
width = 150;
height = 50;
codeLen = 4;
}
static synchronized private YanCode getInstance() {
return new YanCode();
}
public static BufferedImage getImageCode(String strCode) {
return getInstance().getImage(strCode);
}
public static String getStringCode() {
return getInstance().getStrCode();
}
public static void setWidth(int width) {
YanCode.width = width;
}
public static void setHeight(int height) {
YanCode.height = height;
}
public static void setCodeLen(int codeLen) {
YanCode.codeLen = codeLen;
}
}
测试代码如下
public class TestCode {
@Test
public void Cofr(){
YanCode.setCodeLen(3);
String stringCode = YanCode.getStringCode();
System.out.println("stringCode = " + stringCode);
BufferedImage imageCode = YanCode.getImageCode(stringCode);
try {
ImageIO.write(imageCode, "png", new FileOutputStream("code.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
servlet 请求
@WebServlet("/code")
public class CodeServlet extends BaseServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", -1);
response.setContentType("image/png");
ServletOutputStream sos;
try {
sos = response.getOutputStream();
String code = YanCode.getStringCode();
System.out.println("code = " + code);
ImageIO.write(YanCode.getImageCode(code),"png", sos);
sos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
本文地址:https://blog.csdn.net/by2233/article/details/111937899