验证码代码

验证码代码

import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Font;
import java.util.Random;
import java.awt.Color;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.FileOutputStream;
/**
@Author 007liuchao007 --> chenml--->Michael Sun

**/

public class CheckMa{
public static final String typeB="0123456789ABCDEFGHRJKLMNOPQRSTUVWXYZ";
public static final int Font_SIZE=15;
public static final String FONT_NAME="Fixedsys";

//设置图片宽和高
private int width=100;
private int height=30;

//设置随机数类
Random random=new Random();

//设置干扰线数量
private int lineNum=10;

//设置字符个数
private int charNum=4;

//测试一下
public static void main(String args[])throws Exception{
CheckMa check=new CheckMa();
BufferedImage tmpImage=check.getBufferedImage();
ImageIO.write(tmpImage,"JPEG",new FileOutputStream(new File("d:\\javaweb\\008.jpg")));
}


public BufferedImage getBufferedImage(){
BufferedImage imageBuffer= new BufferedImage(this.width,this.height,BufferedImage.TYPE_INT_BGR);

//定义一个Graphise类对封闭图像上绘制
Graphics g= imageBuffer.getGraphics() ;
//设定背景颜色
g.setColor(getColor(200,250));

//设定填充区域的矩形框
g.fillRect(0,0,this.width,this.height);

//设置干扰色的颜色
g.setColor(getColor(100,120));
//绘制干扰线
for(int i=0;i<lineNum;i++){
drawLine(g);
}


g.setFont(new Font(FONT_NAME, Font.ROMAN_BASELINE, Font_SIZE));
//绘制字符串
for(int i=0;i<charNum;i++){
g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
String imageString=String.valueOf(typeB.charAt(random.nextInt(typeB.length()))) ;
g.translate(random.nextInt(3), random.nextInt(3));
g.drawString(imageString, 20*i, 16) ;

}
g.dispose() ;//释放资源

return imageBuffer;

}


public void drawLine(Graphics g){
int x1=random.nextInt(this.width);
int y1=random.nextInt(this.height);
int x2=random.nextInt(15);
int y2=random.nextInt(15);
g.drawLine(x1, y1, x1+ x2,y1+y2);//绘制直线
// g.translate(random.nextInt(3), random.nextInt(3));

}


public Color getColor(int x,int y){
if(x>255)
x=255;
if(y>255)
y=255;

int r=x+random.nextInt(y-x);
int g=x+random.nextInt(y-x);
int b=x+random.nextInt(y-x);

return new Color(r,g,b);
}




}


[img]
[img]http://dl2.iteye.com/upload/attachment/0085/3663/80646226-f315-3794-be0a-eea8d4c62a7e.jpg[/img]
[/img]
追版必究,使用著名来源 007liuchao007
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值