验证码的的生成

[size=medium]
这个类是暑假在实验室的时候老师让我改的,我只修改了一些东西使生成的图片中的验证码随图片大小变化时不会看不到
下面是代码,可直接使用

package com.tian.main;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

public class Image {
private String[] numberAndCharacter = { "1", "2", "3", "4", "5", "6", "7",
"8", "9", "0", "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" };

/**
*
* @param width图片宽度
* @param height图片高度
* @param path图片保存路径
* @param fileExtend图片文件的后缀
* @return
*/
public Boolean generateImageFirst(int width, int height, String path,
String fileName, String fileExtend/*, HttpSession session*/) {
int heig = height*3/4;//字体大小
Boolean bresultBoolean = true;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 生成随机类
Random random = new Random();
// 获取图形上下文
Graphics2D g2 = image.createGraphics();
// Color c1 = getRandColor(random,254,255);
Color c1 = new Color(180, 217, 201);
//Color c2 = c1.darker().darker();
// g2.setPaint(new GradientPaint(0f,0f,c1,width,height,c2));
g2.setPaint(new GradientPaint(0f, 0f, c1, width, height, c1));
g2.fillRect(0, 0, width, height);
// g.setPaint(pat);
// 设定字体
g2.setFont(new Font("Times New Roman", Font.BOLD, heig));
// 画小格子,先水平,后垂直
g2.setColor(new Color(219, 255, 238));
for (int i = 0; i < 100; i++) {
g2.draw(new Line2D.Double(0, i * 2, 150, i * 2));
}
// 垂直线31条
for (int i = 0; i < 300; i++) {
g2.draw(new Line2D.Double(i * 2, 0, i * 2, 50));
}
// 随即产生验证码的随机线
g2.setColor(getRandColor(random, 200, 250));
for (int i = 0; i < 1; i++) {
//int x = random.nextInt(width);
int y = random.nextInt(20) + 10;
//int xl = random.nextInt(width);
//int yl = random.nextInt(height);
g2.setColor(new Color(255, 255, 102));
// g2.drawLine(0,y+i,140,y+i);
g2.fillRect(0, y, width, 10);
}
// 取随机产生的认证码(4位数字)
String sRand = "";
for (int i = 0; i < 4; i++) {
String rand = numberAndCharacter[random
.nextInt(numberAndCharacter.length)];
sRand += rand;
// 将认证码显示到图象中
g2.setColor(new Color(random.nextInt(235), random.nextInt(222),
random.nextInt(200)));
// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g2.drawString(rand, width/4 * i + 5, height - random.nextInt(15)); //在这做了修改
}
// 随即产生验证码的像素点
g2.setColor(getRandColor(random, 200, 250));
for (int i = 0; i < 300; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
//int xl = random.nextInt(width);
//int yl = random.nextInt(height);
g2.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g2.drawLine(x, y, x, y);
}

// 将认证码存入SESSION
//session.setAttribute("rand", sRand);
// 图象生效
g2.dispose();
File imageFile = new File(path + File.separator + fileName + "." +fileExtend );
try {
bresultBoolean = ImageIO.write(image, fileExtend, imageFile);
} catch (IOException e) {
bresultBoolean = false;
e.printStackTrace();
}
return bresultBoolean;
}

/**
* 第二种产生验证码的方法
*
* @param width图片宽度
* @param height图片高度
* @param path 图片保存路径
* @param fileExtend图片文件的后缀
* @return
*/
public Boolean generateImageSecond(int width, int height, String path,
String fileName, String fileExtend/*, HttpSession session*/) {
int heig = height*3/4;//字体大小
Boolean bresultBoolean = true;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 生成随机类
Random random = new Random();
// 获取图形上下文
Graphics2D g2 = image.createGraphics();
Color c1 = getRandColor(random, 200, 255);
Color c2 = c1.darker();
g2.setPaint(new GradientPaint(0f, 0f, c1, width, height, c2));
g2.fillRect(0, 0, width, height);
// g.setPaint(pat);

// 设定字体
g2.setFont(new Font("Times New Roman", Font.PLAIN, 35));

// 随机产生5条干扰线,使图象中的认证码不易被其它程序探测到
g2.setColor(getRandColor(random, 200, 250));
for (int i = 0; i < 5; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(width);
int yl = random.nextInt(height);
g2.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g2.drawLine(x, y, x + xl, y + yl);
}

// 取随机产生的认证码(4位数字)
String sRand = "";
for (int i = 0; i < 4; i++) {
String rand = numberAndCharacter[random
.nextInt(numberAndCharacter.length)];
sRand += rand;
// 将认证码显示到图象中
g2.setColor(new Color(20 + random.nextInt(100), 20 + random
.nextInt(100), 20 + random.nextInt(100)));
// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g2.drawString(rand, width/4 * i + 5, height - random.nextInt(15)); //在这做了修改
}

// 将认证码存入SESSION
//session.setAttribute("rand", sRand);

// 图象生效
g2.dispose();
File imageFile = new File(path + File.separator + fileName);
try {
bresultBoolean = ImageIO.write(image, fileExtend, imageFile);
} catch (IOException e) {
bresultBoolean = false;
e.printStackTrace();
}

return bresultBoolean;
}

public Color getRandColor(Random random, int fc, int bc) {
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}

public static void main(String [] sgrs)
{
Image i = new Image();
i.generateImageFirst(100, 35, "E:\\", "a", "png");
}
}


[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值