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

来自 , 2020-12-22, 写在 Java, 查看 108 次.
URL http://www.code666.cn/view/020c8bfa
  1. package Util;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.image.BufferedImage;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.OutputStream;
  10. import java.util.Random;
  11. import javax.imageio.ImageIO;
  12.  
  13. public final class ImageUtil {
  14.        
  15.         // 验证码字符集
  16.         private static final char[] chars = {
  17.                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  18.                 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  19.                 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  20.         // 字符数量
  21.         private static final int SIZE = 4;
  22.         // 干扰线数量
  23.         private static final int LINES = 5;
  24.         // 宽度
  25.         private static final int WIDTH = 80;
  26.         // 高度
  27.         private static final int HEIGHT = 40;
  28.         // 字体大小
  29.         private static final int FONT_SIZE = 30;
  30.  
  31.         /**
  32.          * 生成随机验证码及图片
  33.          */
  34.         public static Object[] createImage() {
  35.                 StringBuffer sb = new StringBuffer();
  36.                 // 1.创建空白图片
  37.                 BufferedImage image = new BufferedImage(
  38.                         WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  39.                 // 2.获取图片画笔
  40.                 Graphics graphic = image.getGraphics();
  41.                 // 3.设置画笔颜色
  42.                 graphic.setColor(Color.LIGHT_GRAY);
  43.                 // 4.绘制矩形背景
  44.                 graphic.fillRect(0, 0, WIDTH, HEIGHT);
  45.                 // 5.画随机字符
  46.                 Random ran = new Random();
  47.                 for (int i = 0; i <SIZE; i++) {
  48.                         // 取随机字符索引
  49.                         int n = ran.nextInt(chars.length);
  50.                         // 设置随机颜色
  51.                         graphic.setColor(getRandomColor());
  52.                         // 设置字体大小
  53.                         graphic.setFont(new Font(
  54.                                 null, Font.BOLD + Font.ITALIC, FONT_SIZE));
  55.                         // 画字符
  56.                         graphic.drawString(
  57.                                 chars[n] + "", i * WIDTH / SIZE, HEIGHT / 2);
  58.                         // 记录字符
  59.                         sb.append(chars[n]);
  60.                 }
  61.                 // 6.画干扰线
  62.                 for (int i = 0; i < LINES; i++) {
  63.                         // 设置随机颜色
  64.                         graphic.setColor(getRandomColor());
  65.                         // 随机画线
  66.                         graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
  67.                                         ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
  68.                 }
  69.                 // 7.返回验证码和图片
  70.                 return new Object[]{sb.toString(), image};
  71.         }
  72.  
  73.         /**
  74.          * 随机取色
  75.          */
  76.         public static Color getRandomColor() {
  77.                 Random ran = new Random();
  78.                 Color color = new Color(ran.nextInt(256),
  79.                                 ran.nextInt(256), ran.nextInt(256));
  80.                 return color;
  81.         }
  82.        
  83.         public static void main(String[] args) throws IOException {
  84.                 Object[] objs = createImage();
  85.                 BufferedImage image =
  86.                         (BufferedImage) objs[1];
  87.                 // /home/soft01/1.png
  88.                 OutputStream os =
  89.                         new FileOutputStream("d:/1.png");
  90.                 ImageIO.write(image, "png", os);
  91.                 os.close();
  92.         }
  93.  
  94. }

回复 "验证码"

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

captcha