JAVA EE生成验证码

/**
 * 
 * @author lcl
 *图片辅助类 用于生成验证码或者生成本地图片响应到客户端
 *类 Random 是 伪随机数生成器,要生成各种类型的随机数,必须先得到它的实例对象,然后再生成随机数
 */
public class ImagerHelp {
public static char getChar(Random random){
//48-57 数字  97-122小写字母 65-90大写字母
int res = 0;
while(true){
res = random.nextInt(123); //只产生0--122的数
if((res>=48&&res<=57)||(res>=97&&res<=122)||(res>=65&&res<=90)){
break;
}
}
return (char)res;
}
/**
* @param filePath
* @param response
* @param watermark
* @throws Exception
* 展现本地图片
*/
public static void createImage(String filePath, HttpServletResponse response,String watermark) throws Exception {
File file = new File(filePath);
BufferedImage image = ImageIO.read(file);
response.setDateHeader("Expires", 0);
if(watermark!=null){
Graphics g = image.getGraphics();
g.setColor(Color.white);
FontMetrics m = g.getFontMetrics();
int len = m.stringWidth(watermark);
//System.out.println(m.getHeight());
g.drawString(watermark, image.getWidth()-len-20, image.getHeight()-20);
}
ImageIO.write(image, "JPEG", response.getOutputStream());    
}


/**
* @param response
* @param req
* @param width
* @param height
* @param strLenth
* @throws Exception
* 生成图片验证码
*/
public static void createImage(HttpServletResponse response,HttpServletRequest req,int width,int height,int strLenth)
throws Exception {
response.setHeader("Pragma", "No-cache");           
    response.setHeader("Cache-Control", "no-cache");    //设置页面不缓存
    response.setDateHeader("Expires", 0);    
    
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//在内存中创建图片,自定义宽高
Graphics g = image.getGraphics();    //获取图像上下文
    Random random = new Random();      //生成一个随机类
    g.setColor(getColor(200, 250));   //设定背景色
    g.fillRect(0, 0, width, height);    //用画刷填充矩形,指定宽、高
    g.setFont(new Font("Times New Roman", Font.PLAIN, 18));     //设字体
    
    g.setColor(getColor(160, 200));    
    for (int i = 0; i < 100; i++) {  // 随机产生100条干扰线,使图象中的认证码不易被其它程序探测到
        int x = random.nextInt(width);    
        int y = random.nextInt(height);    
        int xl = random.nextInt(12);    
        int yl = random.nextInt(12);    
        g.drawLine(x, y, x + xl, y + yl);    
    }   
    
    String sRand = "";    
    for (int i = 0; i < strLenth; i++) {    
// String rand = getChar(random)+"";    
    String rand = random.nextInt(10)+"";// 取随机产生的认证码
        sRand += rand; 
        
        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        g.drawString(rand, 13 * i + 6, 16);    // 将认证码显示到图象中 
    }    // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
    
    req.getSession().setAttribute("code", sRand); // 将认证码存入SESSION中
        // 获取方式  request.getSession().getAttribute("checkCode");
    
    g.dispose();   // 释放g所占用的系统资源,图象生效  
    
    ImageIO.write(image, "JPEG", response.getOutputStream());    // 输出图象到页面 
}

public static Color getColor(int fc, int bc){//生成三个0-255之间的随机数,作为参数,就能得到随机的颜色
        Random random = new Random();    
        if (fc > 255)    
            fc = 255;    
        if (bc > 255)    
            bc = 255;    
        int r = fc + random.nextInt(bc - fc); // 随机生成rgb颜色中的r、g、b值  
        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[] args) throws Exception {

System.out.println((int)'Z');
}
}

猜你喜欢

转载自blog.csdn.net/lcl497049972/article/details/80292880