保誠-保戶業務員媒合平台
wayne
2021-12-29 c1c065fdb4b88062236633004e974f54bf6cd67c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.pollex.pam.service.util;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
 
public final class VerifyCodeUtil {
 
    
    public static String createcode() {
        String code = "";
        code = "";
        String randomRange = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";// Randomly generated character
                                                                                    // range (0-9, a-z, A-Z)
 
        // The number of digits for generating the verification code (here 4 digits)
        for (int i = 0; i < 4; i++) {
            int index = (int) (Math.random() * 62);// Will produce a [0,62) number, excluding decimals
            char randomCode = randomRange.charAt(index);
            code = code + randomCode;
        }
        return code;
    }
 
    // 3. Generate pictures
    public static BufferedImage createimage(String code) {
        // The first 2 parameters are: width, height. The back is the image type
        // Create a BufferedImage object without transparent color, TYPE_INT_ARGB is
        // with transparent color
        BufferedImage bi = new BufferedImage(130, 50, BufferedImage.TYPE_INT_RGB);
 
        // 1. Get a canvas
        Graphics g = bi.getGraphics();
        // 2. Add background color
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 130, 50);
 
        // 3. Add interference lines
        for (int i = 0; i < 10; i++) {
            Random r = new Random();
            int red = r.nextInt(256);
            int green = r.nextInt(256);
            int blue = r.nextInt(256);
            Color c = new Color(red, green, blue);
            g.setColor(c);
            int x1 = r.nextInt(131);
            int y1 = r.nextInt(51);
            int x2 = r.nextInt(131);
            int y2 = r.nextInt(51);
            g.drawLine(x1, y1, x2, y2);// Draw a line
            // g.drawOval(x1, y1, x2, y2);//Draw a curve
        }
 
        // 3. Add text
        g.setColor(Color.BLACK);
        g.setFont(new Font(" ", Font.BOLD, 40));
 
        // 4. Fill the text into the artboard
        g.drawString(code, 15, 40);
 
        // 5. Close the canvas
        g.dispose();
        return bi;
    }
 
}