|
| 1 | +package cn.edu.swu.ffdy.JavaWeb.Utils; |
| 2 | + |
| 3 | +import javax.imageio.ImageIO; |
| 4 | +import java.awt.*; |
| 5 | +import java.awt.image.BufferedImage; |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.Random; |
| 9 | + |
| 10 | +public class ValidateCode { |
| 11 | + private static final String CHARS = "1234567890abcdefghijklmnopqrstuvwxyz"; |
| 12 | + private static final int CODE_SIZE = 4; // 验证码个数 |
| 13 | + private static final int PIC_WIDTH = 300; // 图片宽度 |
| 14 | + private static final int PIC_HEIGHT = 100; // 图片高度 |
| 15 | + private static final int FONT_COLOR_RGB = 100; // 字符颜色,0-255,数值越小颜色越深 |
| 16 | + private static final int LINE_COLOR_RGB = 150; // 干扰线颜色,0-255,数值越小颜色越深 |
| 17 | + private static final Random random = new Random(); |
| 18 | + |
| 19 | + /** |
| 20 | + * 获取随机字符串 |
| 21 | + * |
| 22 | + */ |
| 23 | + public static String getCodeNum() { |
| 24 | + StringBuilder sb = new StringBuilder(); |
| 25 | + for (int i = 0; i < CODE_SIZE; i++) { |
| 26 | + int index = random.nextInt(CHARS.length()); |
| 27 | + char c = CHARS.charAt(index); |
| 28 | + sb.append(c); |
| 29 | + } |
| 30 | + return sb.toString(); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * 得到验证码图片 |
| 35 | + * |
| 36 | + */ |
| 37 | + public static BufferedImage getCodePic(String codeNum) { |
| 38 | + BufferedImage bi = new BufferedImage(PIC_WIDTH, PIC_HEIGHT, |
| 39 | + BufferedImage.TYPE_INT_BGR); // 画布 |
| 40 | + Graphics g = bi.getGraphics(); // 画笔 |
| 41 | + g.fillRect(0, 0, PIC_WIDTH, PIC_HEIGHT); // 绘制矩形 |
| 42 | + // 绘制干扰线 |
| 43 | + drowLine(g); |
| 44 | + // 绘制字符串 |
| 45 | + drawCode(g, codeNum); |
| 46 | + g.dispose(); // 释放资源 |
| 47 | + return bi; |
| 48 | + } |
| 49 | + |
| 50 | + /* |
| 51 | + * 绘制字符串 |
| 52 | + */ |
| 53 | + private static void drawCode(Graphics g, String codeNum) { |
| 54 | + int fontSize; |
| 55 | + fontSize = PIC_WIDTH / CODE_SIZE; |
| 56 | + g.setFont(new Font("Fixedsys", Font.BOLD, fontSize)); // 字体 |
| 57 | + |
| 58 | + for (int i = 0; i < codeNum.length(); i++) { |
| 59 | + g.setColor(getRandomColor()); |
| 60 | + int rightX = (PIC_WIDTH / CODE_SIZE - fontSize / 2) / 2; |
| 61 | + int deviationX = random.nextInt(3); |
| 62 | + int deviationY = random.nextInt(3); |
| 63 | + g.translate(deviationX, deviationY); |
| 64 | + g.drawString(codeNum.charAt(i) + "", (PIC_WIDTH / CODE_SIZE) * i |
| 65 | + + rightX - deviationX, (PIC_HEIGHT + fontSize / 2) / 2 |
| 66 | + - deviationY); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * 获取随机颜色 |
| 72 | + * |
| 73 | + */ |
| 74 | + private static Color getRandomColor() { |
| 75 | + return new Color(random.nextInt(ValidateCode.FONT_COLOR_RGB), random.nextInt(ValidateCode.FONT_COLOR_RGB), |
| 76 | + random.nextInt(ValidateCode.FONT_COLOR_RGB)); |
| 77 | + } |
| 78 | + |
| 79 | + /* |
| 80 | + * 绘制干扰线 |
| 81 | + */ |
| 82 | + private static void drowLine(Graphics g) { |
| 83 | + g.setColor(new Color(LINE_COLOR_RGB, LINE_COLOR_RGB, LINE_COLOR_RGB)); // 画笔颜色 |
| 84 | + for (int i = 0; i < PIC_WIDTH * PIC_HEIGHT / 50; i++) { |
| 85 | + int x = random.nextInt(PIC_WIDTH); // 起点横坐标 |
| 86 | + int y = random.nextInt(PIC_HEIGHT); |
| 87 | + int xl = random.nextInt(13); // 终点横坐标 |
| 88 | + int yl = random.nextInt(15); |
| 89 | + g.drawLine(x, y, x + xl, y + yl); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + public static void main(String[] args) { |
| 95 | + System.out.println(getCodeNum()); |
| 96 | + File file = new File("a.png"); |
| 97 | + try { |
| 98 | + ImageIO.write(getCodePic(getCodeNum()), "png", file); |
| 99 | + System.out.println("成功 : \t" + file.getAbsolutePath()); |
| 100 | + } catch (IOException e) { |
| 101 | + e.printStackTrace(); |
| 102 | + System.out.println("失败"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments