fwq
2023-01-04 a7fa7db6281875687c3e2c90275d4676ac5869c2
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 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.ByteArrayOutputStream;
8 import java.io.IOException;
9 import java.util.Base64;
10
11 import javax.imageio.ImageIO;
12
13 /**
14  * 验证码生成器
15  * @author ChenJiaHe
16  * @Date 2020-06-17
17  */
18 public class CheckCodeImageUtil {
19     
20     private final static int WIDTH = 60;
21     private final static int HEIGHT = 20;
22     
23     private static char[] generateCheckCode() {
24         // ������֤����ַ��
25         String chars = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
26         char[] rands = new char[4];
27         for (int i = 0; i < 4; i++) {
28             int rand = (int) (Math.random() * 35);
29             rands[i] = chars.charAt(rand);
30         }
31         return rands;
32     }
33
34     private static void drawRands(Graphics g, char[] rands) {
35         g.setColor(Color.BLACK);
36         g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));
37         // �ڲ�ͬ�ĸ߶��������֤���ÿ���ַ�
38         g.drawString("" + rands[0], 1, 17);
39         g.drawString("" + rands[1], 16, 15);
40         g.drawString("" + rands[2], 31, 18);
41         g.drawString("" + rands[3], 46, 16);
42     }
43
44     private static void drawBackground(Graphics g) {
45         // ������
46         g.setColor(new Color(0xDCDCDC));
47         g.fillRect(0, 0, WIDTH, HEIGHT);
48         // ������120�����ŵ�
49         for (int i = 0; i < 120; i++) {
50             int x = (int) (Math.random() * WIDTH);
51             int y = (int) (Math.random() * HEIGHT);
52             int red = (int) (Math.random() * 255);
53             int green = (int) (Math.random() * 255);
54             int blue = (int) (Math.random() * 255);
55             g.setColor(new Color(red, green, blue));
56             g.drawOval(x, y, 1, 0);
57         }
58     }
59     
60     public static String[] createImage(String id)
61     {
62         ByteArrayOutputStream bos = null;
63         String reStr = "";
64         char[] rands = null;
65         try {
66             BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
67             Graphics g = image.getGraphics();
68     
69             rands = generateCheckCode();
70     
71             drawBackground(g);
72             drawRands(g, rands);
73     
74             g.dispose();
75     
76             bos = new ByteArrayOutputStream();
77             ImageIO.write(image, "JPEG", bos);
78             byte[] buf = bos.toByteArray();
79             reStr = Base64.getEncoder().encodeToString(buf);
80             
81         }catch (Exception e) {
82             e.printStackTrace();
83         }finally {
84             if(bos != null)
85             {
86                 try {
87                     bos.close();
88                 } catch (IOException e) {
89                     e.printStackTrace();
90                 }
91                 bos = null;
92             }
93         }
94         return new String[] {new String(rands),reStr};
95     }
96 }