0% found this document useful (0 votes)
4 views4 pages

Java Captcha Image Generator Code

The CaptchaController class generates graphical captchas for web applications using Java and Spring framework. It creates a random string of characters, draws it onto an image, and optionally applies distortion effects before sending it to the client. The generated captcha is stored in the user's session for verification purposes.

Uploaded by

akaiilevente
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Java Captcha Image Generator Code

The CaptchaController class generates graphical captchas for web applications using Java and Spring framework. It creates a random string of characters, draws it onto an image, and optionally applies distortion effects before sending it to the client. The generated captcha is stored in the user's session for verification purposes.

Uploaded by

akaiilevente
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

package [Link].

controller;

import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];

@Controller
public class CaptchaController extends BaseController {
//字符源
private String text = "0123456789abcdefghijklmnopqrstuvwxyz";
private int length = 4;
private int width = 200;
private int height = 64;
private Font font = new Font("Arial", [Link] | [Link], (int)
(height*0.8));
private boolean crossLine = false;
private boolean twistImage = true;

public String getText() {


return text;
}

public int getLength() {


return length;
}

public int getWidth() {


return width;
}

public int getHeight() {


return height;
}

public void setText(String text) {


[Link] = text;
}

public void setLength(int length) {


[Link] = length;
}

public void setWidth(int width) {


[Link] = width;
}

public void setHeight(int height) {


[Link] = height;
}
/**
* 请求生成图形验证码
* @throws IOException
*/
@RequestMapping("/captcha")
public void genCaptcha(HttpServletRequest request, HttpServletResponse
response) throws IOException {
// 在内存中创建图象
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = [Link]();
// 生成随机类
Random random = new Random();
// 设定背景色
[Link](new Color(255, 255, 255));
[Link](0, 0, width, height);
// 设定字体
[Link](font);
// 取随机产生的验证码
String sRand = "";
//文字 X 坐标
int codeX = width/(length+1);
for (int i = 0; i < length; i++) {
String rand = [Link](getRandomText());
sRand += rand;
//将验证码输出到图象中
[Link](getRandColor(100, 150));// 调用函数出来的颜色相同
[Link](rand, codeX * i + 10, height-20);
}
// 生成干扰线
if (crossLine) {
for (int i = 0; i < ([Link](5) + 5); i++) {
[Link](new Color([Link](255) + 1,
[Link](255) + 1, [Link](255) + 1));
[Link]([Link](width), [Link](height),
[Link](width),
[Link](height));
}
}
if(twistImage){
image = twistImage(image);
}
String pageId = request(request,"cid");
String key = "CAPTCHA_" + pageId;
// 将验证码存入页面 KEY 值的 SESSION 里面
HttpSession session = [Link]();
[Link](key, sRand);
[Link](key + "_time", [Link]());

// 图象生效
[Link]();
ServletOutputStream responseOutputStream = [Link]();
// 输出图象到页面
[Link](image, "JPEG", responseOutputStream);
// 以下关闭输入流!
[Link]("Cache-Control", "no-store");
[Link]("Pragma", "no-cache");
[Link]("Expires", 0);
[Link]();
[Link]();
}

/**
*
* @Description:正弦曲线 Wave 扭曲图片
* @return BufferedImage
*/
private BufferedImage twistImage(BufferedImage buffImg) {
Random random = new Random();
double dMultValue = [Link](10) + 5;// 波形的幅度倍数,越大扭曲的程序越高,一般为 3
double dPhase = [Link](6);// 波形的起始相位,取值区间(0-2*PI)
BufferedImage destBi = new BufferedImage([Link](),
[Link](), BufferedImage.TYPE_INT_RGB);
Graphics g = [Link]();
[Link]([Link]);
[Link](0, 0, [Link](), [Link]());
for (int i = 0; i < [Link](); i++) {
for (int j = 0; j < [Link](); j++) {
int nOldX = getXPosition4Twist(dPhase,
dMultValue,[Link](), i, j);
int nOldY = j;
if (nOldX >= 0 && nOldX < [Link]() && nOldY >= 0
&& nOldY < [Link]()) {
[Link](nOldX, nOldY, [Link](i, j));
}
}
}
return destBi;
}

/**
*
* @Description:获取扭曲后的 x 轴位置
* @param dPhase
* @param dMultValue
* @param height
* @param xPosition
* @param yPosition
* @return int
*/
private int getXPosition4Twist(double dPhase, double dMultValue,
int height, int xPosition, int yPosition) {
double PI = 3.1415926535897932384626433832799; // 此值越大,扭曲程度越大
double dx = (double) (PI * yPosition) / height + dPhase;
double dy = [Link](dx);
return xPosition + (int) (dy * dMultValue);
}

public Font getFont() {


return font;
}

public void setFont(Font font) {


[Link] = font;
}
char getRandomText() {
Random random = new Random();
return [Link]([Link]([Link]()));
}

/**
* 给定范围获得随机颜色
*
* @param fc
* @param bc
* @return
*/
Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255) {
fc = 255;
}
if (bc > 255) {
bc = 255;
}
int r = fc + [Link](bc - fc);
int g = fc + [Link](bc - fc);
int b = fc + [Link](bc - fc);
return new Color(r, g, b);
}
}

You might also like