java图片验证码源码(图片验证码api)
本文目录一览:
- 1、求java语言随机生成图片验证码的程序代码,有详细注释最好,本人初学者,谢谢各位的帮忙!!!
- 2、求一个Java语言编写的gif动态验证码 就是百度注册用户的那种
- 3、验证码怎么用java代码实现验证
- 4、怎么用Java代码实现一个验证码,求具体实现方法
- 5、求一个java写的验证码源码和一个MD5加密源码 最好粘贴过来就可以直接运行的 谢谢大神么,辛苦了
求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);
}
希望能帮助java图片验证码源码你
求一个Java语言编写的gif动态验证码 就是百度注册用户的那种
java图片验证码源码你好,
Servlet类java图片验证码源码:
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");//设置相应类型,告诉浏览器输出java图片验证码源码的内容为图片
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);
}
}
生成图片java图片验证码源码的类:
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中java图片验证码源码的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代码实现验证
package util; import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Random;import javax.imageio.ImageIO; public final class ImageUtil { // 验证码字符集 private static final char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; // 字符数量 private static final int SIZE = 4; // 干扰线数量 private static final int LINES = 5; // 宽度 private static final int WIDTH = 80; // 高度 private static final int HEIGHT = 40; // 字体大小 private static final int FONT_SIZE = 30; /** * 生成随机验证码及图片 * 返回的数组中,第1个值是验证码,第2个值是图片 */ public static Object[] createImage() { StringBuffer sb = new StringBuffer(); // 1.创建空白图片 BufferedImage image = new BufferedImage( WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); // 2.获取图片画笔 Graphics graphic = image.getGraphics(); // 3.设置画笔颜色 graphic.setColor(Color.LIGHT_GRAY); // 4.绘制矩形背景 graphic.fillRect(0, 0, WIDTH, HEIGHT); // 5.画随机字符 Random ran = new Random(); for (int i = 0; i SIZE; i++) { // 取随机字符索引 int n = ran.nextInt(chars.length); // 设置随机颜色 graphic.setColor(getRandomColor()); // 设置字体大小 graphic.setFont(new Font( null, Font.BOLD + Font.ITALIC, FONT_SIZE)); // 画字符 graphic.drawString( chars[n] + "", i * WIDTH / SIZE, HEIGHT / 2); // 记录字符 sb.append(chars[n]); } // 6.画干扰线 for (int i = 0; i LINES; i++) { // 设置随机颜色 graphic.setColor(getRandomColor()); // 随机画线 graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH), ran.nextInt(HEIGHT)); } // 7.返回验证码和图片 return new Object[]{sb.toString(), image}; } /** * 随机取色 */ public static Color getRandomColor() { Random ran = new Random(); Color color = new Color(ran.nextInt(256), ran.nextInt(256), ran.nextInt(256)); return color; } public static void main(String[] args) throws IOException { Object[] objs = createImage(); BufferedImage image = (BufferedImage) objs[1]; OutputStream os = new FileOutputStream("d:/1.png"); ImageIO.write(image, "jpeg", os); os.close(); } }
怎么用Java代码实现一个验证码,求具体实现方法
package util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public final class ImageUtil {
// 验证码字符集
private static final char[] chars = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
// 字符数量
private static final int SIZE = 4;
// 干扰线数量
private static final int LINES = 5;
// 宽度
private static final int WIDTH = 80;
// 高度
private static final int HEIGHT = 40;
// 字体大小
private static final int FONT_SIZE = 30;
/**
* 生成随机验证码及图片
* 返回的数组中,第1个值是验证码,第2个值是图片
*/
public static Object[] createImage() {
StringBuffer sb = new StringBuffer();
// 1.创建空白图片
BufferedImage image = new BufferedImage(
WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 2.获取图片画笔
Graphics graphic = image.getGraphics();
// 3.设置画笔颜色
graphic.setColor(Color.LIGHT_GRAY);
// 4.绘制矩形背景
graphic.fillRect(0, 0, WIDTH, HEIGHT);
// 5.画随机字符
Random ran = new Random();
for (int i = 0; i SIZE; i++) {
// 取随机字符索引
int n = ran.nextInt(chars.length);
// 设置随机颜色
graphic.setColor(getRandomColor());
// 设置字体大小
graphic.setFont(new Font(
null, Font.BOLD + Font.ITALIC, FONT_SIZE));
// 画字符
graphic.drawString(
chars[n] + "", i * WIDTH / SIZE, HEIGHT / 2);
// 记录字符
sb.append(chars[n]);
}
// 6.画干扰线
for (int i = 0; i LINES; i++) {
// 设置随机颜色
graphic.setColor(getRandomColor());
// 随机画线
graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
}
// 7.返回验证码和图片
return new Object[]{sb.toString(), image};
}
/**
* 随机取色
*/
public static Color getRandomColor() {
Random ran = new Random();
Color color = new Color(ran.nextInt(256),
ran.nextInt(256), ran.nextInt(256));
return color;
}
public static void main(String[] args) throws IOException {
Object[] objs = createImage();
BufferedImage image = (BufferedImage) objs[1];
OutputStream os = new FileOutputStream("d:/1.png");
ImageIO.write(image, "jpeg", os);
os.close();
}
}
求一个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"));
}
}