java项⽬加减乘除验证码_Java⽣成图⽚验证码3种⽅法(字
母、加减乘除、中⽂)
第⼀种⽅法 全是字母和数字组成
FirstSpring.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @Description ⽣成验证码
* @author waa
* @version V1.0, ⽇期2011-7-4
*/
public class ValidateCodeUtils extends HttpServlet {
private static final long serialVersionUID = -1409007752285164213L;
private static int width = 75;
private static int height = 35;
// private static char[] ch =
// "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789".toCharArray();
private static char[] ch = "abcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
// ⽣成数字和字母的验证码
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg");
response.setHeader("Pragma", "No-cache");
java replace方法response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 在内存中创建图象
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下⽂
Graphics g = Graphics();
// ⽣成随机类
Random random = new Random();
// 设定背景⾊
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
// 设定字体
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
// 画边框
// g.setColor(new Color());
/
/ g.drawRect(0,0,width-1,height-1);
// 随机产⽣155条⼲扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = Int(width);
int y = Int(height);
int xl = Int(12);
int yl = Int(12);
}
// 取随机产⽣的认证码(4位数字)
StringBuffer sb = new StringBuffer();
int index, len = ch.length;
for (int i = 0; i < 4; i++) {
index = Int(len);
g.setColor(new Int(88), Int(188),
g.setFont(new Font("Arial", Font.ITALIC, 28));// 输出的字体和⼤⼩
g.drawString("" + ch[index], (i * 15) + 6, 26);// 写什么数字,在图⽚的什么位置画sb.append(ch[index]);
}
// 将认证码存⼊SESSION
g.dispose();
ServletOutputStream responseOutputStream = OutputStream(); // 输出图象到页⾯
ImageIO.write(image, "JPEG", responseOutputStream);
// 以下关闭输⼊流!
responseOutputStream.flush();
responseOutputStream.close();
}
protected void ValidateCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 在内存中创建图象
BufferedImage img = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 得到该图⽚的绘图对象
Graphics g = Graphics();
Random r = new Random();
Color c = new Color(200, 150, 255);
// 填充整个图⽚的颜⾊
g.fillRect(0, 0, width, height);
// 向图⽚中输出数字和字母
StringBuffer sb = new StringBuffer();
int index, len = ch.length;
for (int i = 0; i < 4; i++) {
index = r.nextInt(len);
g
.setColor(new Int(88), r.nextInt(188), r
.nextInt(255)));
g.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 22));// 输出的字体和⼤⼩g.drawString("" + ch[index], (i * 15) + 6, 18);// 写什么数字,在图⽚的什么位置画sb.append(ch[index]);
}
responseOutputStream.flush();
responseOutputStream.close();
}
Color getRandColor(int fc, int bc) {// 给定范围获得随机颜⾊
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + Int(bc - fc);
int g = fc + Int(bc - fc);
int b = fc + Int(bc - fc);
return new Color(r, g, b);
}
/
**
* 得到某⼀个类的路径
* @param name
* @return
*/
public static String getPath(Class name) {
String strResult = null;
if (Property("os.name").toLowerCase().indexOf("window") > -1) { strResult = Resource("/").toString().replace("file:/", "")
.replace("%20", " ");
} else {
strResult = Resource("/").toString().replace("file:", "")
.replace("%20", " ");
}
return strResult;
}
}
第⼆种 中⽂字符
FirstSpring.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* ⽣成验证码(汉字)
*
* @author 冰⾬凌⾵

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。