当前位置: 移动技术网 > IT编程>开发语言>Java > java验证码生成具体代码

java验证码生成具体代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了java验证码生成的示例代码,供大家参考,具体内容如下

package com.gonvan.component.captcha;
 
import java.awt.*;
import java.awt.image.bufferedimage;
import java.io.ioexception;
import java.util.hashmap;
import java.util.map;
import java.util.random;
 
import javax.imageio.imageio;
import javax.servlet.servletoutputstream;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
 
/**
 * created by yuerzm on 2016/3/14.
 */
public class captchafactory {
 
  private static final char[]   code_sequence    = "abcdefghijklmnopqrstuvwxyz0123456789"
    .tochararray();
  private static final int    default_width    = 60;
  private static final int    default_height   = 20;
  private static final int    default_code_len  = 4;
  private static final int    default_code_x   = 13;
  private static final int    default_code_y   = 16;
  private static final int    default_font_size  = 18;
  private static final string   default_font_family = "times new roman";
  private static captchafactory  instance      = new captchafactory();
  private int           width        = default_width;            // 定义图片的width
  private int           height       = default_height;            // 定义图片的height
  private int           length       = default_code_len;           // 定义图片上显示验证码的个数
  private int           xx         = default_code_x;            // 定义图片上显示验证码x坐标
  private int           yy         = default_code_y;            // 定义图片上显示验证码y坐标
  private int           fontsize      = default_font_size;          // 定义图片上显示验证码的字体大小
  private string         fontfamily     = default_font_family;         // 定义图片上显示验证码的个数
 
  private captchafactory() {
  }
 
  public static captchafactory getinstance() {
    return instance;
  }
 
  /**
   * 配置宽高
   *
   * @param w
   * @param h
   * @return
   */
  public captchafactory configwidthandheight(int w, int h) {
    instance.width = w;
    instance.height = h;
    return instance;
  }
 
  /**
   * 配置坐标
   *
   * @param x
   * @param y
   * @return
   */
  public captchafactory configxy(int x, int y) {
    instance.xx = x;
    instance.yy = y;
    return instance;
  }
 
  /**
   * 配置字体大小
   *
   * @param fontsize
   * @return
   */
  public captchafactory configfontsize(int fontsize) {
    instance.fontsize = fontsize;
    return instance;
  }
 
  /**
   * 配置字体
   *
   * @param fontfamily
   * @return
   */
  public captchafactory configfontsize(string fontfamily) {
    instance.fontfamily = fontfamily;
    return instance;
  }
 
  public void write(httpservletrequest request, httpservletresponse response) throws ioexception {
    // 将四位数字的验证码保存到session中。
    map captcha = generate();
    string randomcode = (string) captcha.get("captchacode");
    bufferedimage buffimg = (bufferedimage) captcha.get("captchaimg");
 
    httpsession session = request.getsession();
    session.setattribute("code", randomcode);
 
    // 禁止图像缓存。
    response.setheader("pragma", "no-cache");
    response.setheader("cache-control", "no-cache");
    response.setdateheader("expires", 0);
    response.setcontenttype("image/jpeg");
 
    // 将图像输出到servlet输出流中。
    servletoutputstream outputstream = response.getoutputstream();
    imageio.write(buffimg, "jpeg", outputstream);
    outputstream.close();
  }
 
  public map<string, object> generate() throws ioexception {
    // 定义图像buffer
    bufferedimage buffimg = new bufferedimage(width, height, bufferedimage.type_int_rgb);
    graphics gd = buffimg.getgraphics();
    // 设定背景色
    gd.setcolor(getrandcolor(200, 250));
    gd.fillrect(0, 0, width, height);
    // 设定字体,字体的大小应该根据图片的高度来定。
    gd.setfont(new font(fontfamily, font.plain, fontsize));
 
    // 创建一个随机数生成器类
    random random = new random();
 
    // 随机产生40条干扰线,使图象中的认证码不易被其它程序探测到。
    gd.setcolor(getrandcolor(160, 200));
    for (int i = 0; i < 155; i++) {
      int x = random.nextint(width);
      int y = random.nextint(height);
      int xl = random.nextint(12);
      int yl = random.nextint(12);
      gd.drawline(x, y, x + xl, y + yl);
    }
 
    // randomcode用于保存随机产生的验证码,以便用户登录后进行验证。
    stringbuffer randomcode = new stringbuffer();
    int red = 0, green = 0, blue = 0;
 
    // 随机产生 length 个验证码。
    for (int i = 0; i < length; i++) {
      // 得到随机产生的验证码数字。
      string code = string.valueof(code_sequence[random.nextint(36)]);
      // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
      red = random.nextint(110);
      green = random.nextint(110);
      blue = random.nextint(110);
 
      // 用随机产生的颜色将验证码绘制到图像中。
      gd.setcolor(new color(red + 20, green + 20, blue + 20));
      gd.drawstring(code, i * xx + 6, yy);
 
      // 将产生的随机数组合在一起。
      randomcode.append(code);
    }
    map<string, object> retval = new hashmap<>();
    retval.put("captchacode", randomcode.tostring());
    retval.put("captchaimg", buffimg);
    return retval;
  }
 
  /**
   * 给定范围获得随机颜色
   *
   * @param fc
   *      最小值
   * @param bc
   *      最大值
   * @return color
   */
  private color getrandcolor(int fc, int bc) {
    random random = new random();
    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);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网