java图片验证码源码(图片验证码api)
本文目录一览:
- 1、求一个java写的验证码源码和一个MD5加密源码 最好粘贴过来就可以直接运行的 谢谢大神么,辛苦了
- 2、求一个Java语言编写的gif动态验证码 就是百度注册用户的那种
- 3、求java语言随机生成图片验证码的程序代码,有详细注释最好,本人初学者,谢谢各位的帮忙!!!
求一个java写的验证码源码和一个MD5加密源码 最好粘贴过来就可以直接运行的 谢谢大神么,辛苦了
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/*
* MD5 算法
*/
public class Md5Util
{
// 全局数组
private final static String[] strDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
public Md5Util()
{
}
// 返回形式为数字跟字符串
private static String byteToArrayString(byte bByte)
{
int iRet = bByte;
// System.out.println("iRet="+iRet);
if (iRet 0)
{
iRet += 256;
}
int iD1 = iRet / 16;
int iD2 = iRet % 16;
return strDigits[iD1] + strDigits[iD2];
}
// 转换字节数组为16进制字串
private static String byteToString(byte[] bByte)
{
StringBuffer sBuffer = new StringBuffer();
for (int i = 0; i bByte.length; i++)
{
sBuffer.append(byteToArrayString(bByte[i]));
}
return sBuffer.toString();
}
public static String GetMD5Code(String strObj)
{
String resultString = null;
try
{
resultString = new String(strObj);
MessageDigest md = MessageDigest.getInstance("MD5");
// md.digest() 该函数返回值为存放哈希值结果的byte数组
resultString = byteToString(md.digest(strObj.getBytes()));
} catch (NoSuchAlgorithmException ex)
{
ex.printStackTrace();
}
return resultString;
}
public static void main(String[] args)
{
System.out.println(Md5Util.GetMD5Code("99999999"));
}
}
求一个Java语言编写的gif动态验证码 就是百度注册用户的那种
你好,
Servlet类:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/gif");//设置相应类型,告诉浏览器输出的内容为图片
response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expire", 0);
RandomValidateCode randomValidateCode = new RandomValidateCode();
try {
randomValidateCode.getRandcode(request, response);//输出图片方法
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
生成图片的类:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RandomValidateCode {
public static final String RANDOMCODEKEY = "RANDOMVALIDATECODEKEY";//放到session中的key
private Random random = new Random();
private String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//随机产生的字符串
private int width = 80;//图片宽
private int height = 26;//图片高
private int lineSize = 25;//干扰线数量
private int stringNum = 4;//随机产生字符数量
/**
* 生成随机图片
*/
public void getRandcode(HttpServletRequest request,
HttpServletResponse response) {
HttpSession session = request.getSession();
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphics g = image.getGraphics();
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));
g.setColor(getRandColor(180, 233));
//绘制干扰线
for(int i=0;i=lineSize;i++){
drowLine(g);
}
//绘制随机字符
String randomString = "";
for(int i=1;i=stringNum;i++){
randomString=drowString(g,randomString,i);
}
session.removeAttribute(RANDOMCODEKEY);
session.setAttribute(RANDOMCODEKEY, randomString);
g.dispose();
try {
ImageIO.write(image, "JPEG", response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
// ------------- private methods -------------
/*
* 获得字体
*/
private Font getFont(){
return new Font("Fixedsys",Font.CENTER_BASELINE,18);
}
/*
* 获得颜色
*/
private Color getRandColor(int fc,int bc){
if(fc 255)
fc = 255;
if(bc 255)
bc = 255;
int r = fc + random.nextInt(bc-fc-16);
int g = fc + random.nextInt(bc-fc-14);
int b = fc + random.nextInt(bc-fc-18);
return new Color(r,g,b);
}
/*
* 绘制字符串
*/
private String drowString(Graphics g,String randomString,int i){
g.setFont(getFont());
g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
String rand = String.valueOf(getRandomString(random.nextInt(randString.length())));
randomString +=rand;
g.translate(random.nextInt(3), random.nextInt(3));
g.drawString(rand, 13*i, 16);
return randomString;
}
/*
* 绘制干扰线
*/
private void drowLine(Graphics g){
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(13);
int yl = random.nextInt(15);
g.drawLine(x, y, x+xl, y+yl);
}
/*
* 获取随机的字符
*/
private String getRandomString(int num){
return String.valueOf(randString.charAt(num));
}
}
求java语言随机生成图片验证码的程序代码,有详细注释最好,本人初学者,谢谢各位的帮忙!!!
楼上正解
/**
* 图片验证码
*
* @author HuangYong
*
*/
public class CheckCode extends HttpServlet {
private static final long serialVersionUID = 1532787236715859525L;
/**
* 验证图片宽
*/
private static final int WIDTH = 80;
/**
* 验证图片高度
*/
private static final int HEIGHT = 20;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获得图片
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
// 获取画笔
Graphics g = image.getGraphics();
// 设置边框
setBounds(g);
// // 设置背景
// setBackGround(g);
// 干扰线
drawRandomLine(g);
// 验证码
drawRandomString((Graphics2D) g, request);
// 发送给页面
response.setContentType("image/jpeg");
response.setDateHeader("expries", -1);
response.setHeader("cache-control", "no-cache");
response.setHeader("progma", "no-cache");
ImageIO.write(image, "jpg", response.getOutputStream());
}
/**
* 边框
*
* @param g
*/
private void setBounds(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.WHITE);
g.fillRect(1, 1, WIDTH - 2, HEIGHT - 2);
}
/**
* 背景
*
* @param g
*/
// private void setBackGround(Graphics g) {
//
// g.setColor(Color.WHITE);
// g.drawRect(1, 1, WIDTH-2, HEIGHT-2);
//
// }
/**
* 干扰线
*
* @param g
*/
private void drawRandomLine(Graphics g) {
g.setColor(Color.GREEN);
int x1, y1;
int x2, y2;
Random r = new Random();
for (int i = 0; i 5; i++) {
// 起始xy
x1 = r.nextInt(WIDTH);
y1 = r.nextInt(HEIGHT);
// 结束xy
x2 = r.nextInt(WIDTH);
y2 = r.nextInt(HEIGHT);
g.drawLine(x1, y1, x2, y2);
}
}
/**
* 验证码
*
* @param g
*/
private void drawRandomString(Graphics2D g, HttpServletRequest request) {
g.setColor(Color.RED);
g.setFont(new Font("宋体", Font.BOLD, 20));
String base = "qwertyupasdfghjkzxcvbnm23456789QWERTYUIOPASDFGHJKLZXCVBNM";
StringBuilder sb = new StringBuilder();
char ch = '\u0000';
int degree = 0;
Random r = new Random();
int startX = 8;
for (int i = 0; i 4; i++) {
ch = base.charAt(r.nextInt(base.length()));
// 设置旋转, ±20°
degree = r.nextInt() % 20;
g.rotate(degree * Math.PI / 180, startX, 14);
g.drawString(ch + "", startX, 16);
// 取消旋转
g.rotate(-degree * Math.PI / 180, startX, 14);
startX += 18;
sb.append(ch);
}
request.getSession().setAttribute("checkCode", sb.toString().toLowerCase());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
希望能帮助你