`
Action-人生
  • 浏览: 104836 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

验证码代码

    博客分类:
  • JAVA
阅读更多
                       验证码代码
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]
追版必究,使用著名来源     007liuchao007 
  • 大小: 1.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics