{image}

JAVA识别图片验证码

package ;

import .;
import .
;
import .;
import .
;
import .;
import .
;
import .*;

public class CodeFact
extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//设置页面不缓存
("Pragma", "No-cache");
("Cache-Control", "no-cache");
ader("Expires", 0);

// 在内存中创建图象
int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height,
_INT_RGB);

// 获取图形上下文
Graphics g = cs();

//生成随机类
Random random = new Random();

// 设定背景色
(getRandColor(200, 250));
(0, 0, width, height);

//设定字体
(new Font("Times New Roman", , 18));

//画边框
(new Color(33,66,99));
(0,0,width-1,height-1);

// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = (width);
int y = (height);
int xl = (12);
int yl = (12);
(x, y, x + xl, y + yl);
}

// 取随机产生的认证码(4位数字)
String sRand = "";
for (int i = 0; i < 4; i++) {
String rand = ((10));
sRand += rand;
// 将认证码显示到图象中
(new Color(20 + (110), 20 + (110),
20 + (110))); //调用函数出来的颜色相同 ,可能是因为种子太接近,所以只能闹亩谨直接生成
g(rand, 13 * i + 6, 16);
}

// 将认证耐逗码存液基入SESSION
HttpSession session = n();
ute("rand", sRand);

// 图象生效
();

// 输出图象到页面
(image, "JPEG", Stream());
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}

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

}

你试试!!

本文版权归去快排Seo www.SEOgurublog.com 所有,如有转发请注明来出,竞价开户托管,seo优化请联系QQ▷61910465