[Java] 生成验证码 →→→→→进入此内容的聊天室

来自 , 2020-10-24, 写在 Java, 查看 136 次.
URL http://www.code666.cn/view/7250eb93
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.image.BufferedImage;
  5. import java.io.IOException;
  6. import java.util.Random;
  7.  
  8. import javax.imageio.ImageIO;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. public class RandImgCreater {
  12.         //默认字符列表
  13.         private static final String CODE_LIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
  14.         //验证码图片高度
  15.         private static final int HEIGHT = 20;
  16.         //默认字符数
  17.         private static final int FONT_NUM = 4;
  18.         //生成验证码图片存放位置
  19.         private HttpServletResponse response;
  20.         //宽度
  21.         private int width = 0;
  22.         //实际字符个数
  23.         private int iNum = 0;
  24.         //实际字符列表
  25.         private String codeList = "";
  26.         //绘制背景
  27.         private boolean drawBgFlag = false;
  28.         //R
  29.         private int rBg = 0;
  30.         //G
  31.         private int gBg = 0;
  32.         //B
  33.         private int bBg = 0;
  34.  
  35.         //构造函数(自定义存放位置)
  36.         public RandImgCreater(HttpServletResponse response) {
  37.                 this.response = response;
  38.                 this.width = 13 * FONT_NUM + 12;
  39.                 this.iNum = FONT_NUM;
  40.                 this.codeList = CODE_LIST;
  41.         }
  42.         //构造函数(自定义存放位置,字符数)
  43.         public RandImgCreater(HttpServletResponse response,int iNum) {
  44.                 this.response = response;
  45.                 this.width = 13 * iNum + 12;
  46.                 this.iNum = iNum;
  47.                 this.codeList = CODE_LIST;
  48.         }
  49.        
  50.         //构造函数(自定义存放位置,字符数,字符列表)
  51.         public RandImgCreater(HttpServletResponse response, int iNum,
  52.                         String codeList) {
  53.                 this.response = response;
  54.                 this.width = 13 * iNum + 12;
  55.                 this.iNum = iNum;
  56.                 this.codeList = codeList;
  57.         }
  58.  
  59.         //绘制验证码
  60.         public String createRandImage() {
  61.                 //1.生成BufferedImage对象,存放图片
  62.                 BufferedImage image = new BufferedImage(width, HEIGHT,
  63.                                 BufferedImage.TYPE_INT_RGB);
  64.  
  65.                 //2.获得BufferedImage对象的画笔
  66.                 Graphics g = image.getGraphics();
  67.                
  68.                 //生成随机数生成器
  69.                 Random random = new Random();
  70.  
  71.                 //开始绘制背景
  72.                 if (drawBgFlag) {
  73.                         //设置画笔颜色
  74.                         g.setColor(new Color(rBg, gBg, bBg));
  75.                         //绘制矩形
  76.                         g.fillRect(0, 0, width, HEIGHT);
  77.                 } else {
  78.                         //设置画笔颜色(随机)
  79.                         g.setColor(getRandColor(200, 250));
  80.                         //绘制矩形
  81.                         g.fillRect(0, 0, width, HEIGHT);
  82.                         //循环155次生成随机干扰线条
  83.                         for (int i = 0; i < 155; i++) {
  84. //                              设置颜色
  85.                                 g.setColor(getRandColor(140, 200));
  86.                                 //起始位置x
  87.                                 int x = random.nextInt(width);
  88.                                 //起始位置y
  89.                                 int y = random.nextInt(HEIGHT);
  90.                                 //结束位置x1
  91.                                 int xl = random.nextInt(12);
  92.                                 //结束位置y1
  93.                                 int yl = random.nextInt(12);
  94.                                 //绘制线条
  95.                                 g.drawLine(x, y, x + xl, y + yl);
  96.                         }
  97.                 }
  98.                 //设置画笔字体、样式、大小
  99.                 g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
  100.                
  101.                 //要返回的字符串
  102.                 String sRand = "";
  103.                 //循环内随机生成字符
  104.                 for (int i = 0; i < iNum; i++) {
  105.                         //随机生成字符起始位置
  106.                         int rand = random.nextInt(codeList.length());
  107.                         //从字符起始位置截取1位字符
  108.                         String strRand = codeList.substring(rand, rand + 1);
  109.                         //把新生成的字符与旧字符连接(返回用)
  110.                         sRand += strRand;
  111.                         //设置前景色(字体颜色)
  112.                         g.setColor(new Color(20 + random.nextInt(110), 20 + random
  113.                                         .nextInt(110), 20 + random.nextInt(110)));
  114.                         //绘制字符,第一个字符从0位置开始,每个字符相隔13像素
  115.                         g.drawString(strRand, 13 * i + 6, 16);
  116.                 }
  117.                 //释放资源
  118.                 g.dispose();
  119.                 try {
  120.                         //把图片输出到相应位置
  121.                         ImageIO.write(image, "JPEG", response.getOutputStream());
  122.                 } catch (IOException e) {
  123.  
  124.                 }
  125.                 //返回验证码的字符
  126.                 return sRand;
  127.         }
  128.  
  129.         //设置背景色(R,G,B颜色)
  130.         public void setBgColor(int r, int g, int b) {
  131.                 drawBgFlag = true;
  132.                 this.rBg = r;
  133.                 this.gBg = g;
  134.                 this.bBg = b;
  135.         }
  136.  
  137.         //随机生成颜色
  138.         private Color getRandColor(int fc, int bc) {
  139.                 Random random = new Random();
  140.                 if (fc > 255)
  141.                         fc = 255;
  142.                 if (bc > 255)
  143.                         bc = 255;
  144.                 int r = fc + random.nextInt(bc - fc);
  145.                 int g = fc + random.nextInt(bc - fc);
  146.                 int b = fc + random.nextInt(bc - fc);
  147.                 return new Color(r, g, b);
  148.         }
  149. }

回复 "生成验证码"

这儿你可以回复上面这条便签

captcha