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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| package top.lrshuai.blog.util;
import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.util.Random;
public class CodeUtil {
private static final String[] CODE = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
public static Object[] CreateCode() { int imgW = 120; int imgH = 42; int r, g, b; Color color; String result = ""; Random random = new Random(); BufferedImage img = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); graphics.setFont(new Font("MicroSoft YaHei", Font.PLAIN, 30));
r = random.nextInt(20) + 230; g = random.nextInt(20) + 230; b = random.nextInt(20) + 230; color = new Color(r, g, b); graphics.setColor(color); graphics.fillRect(0, 0, imgW, imgH);
for (int i = 0; i < 3; i++) { r = random.nextInt(50) + 200; g = random.nextInt(50) + 200; b = random.nextInt(50) + 200; color = new Color(r, g, b); graphics.setColor(color); graphics.setStroke(new BasicStroke(2.0f)); graphics.drawLine(random.nextInt(20), random.nextInt(imgH), random.nextInt(20) + 80, random.nextInt(imgH)); }
if (random.nextInt(100) >= 50) { for (int i = 0; i < 4; i++) { String str = CODE[random.nextInt(CODE.length)]; r = random.nextInt(180) + 50; g = random.nextInt(180) + 50; b = random.nextInt(180) + 50; color = new Color(r, g, b); graphics.setColor(color); graphics.drawString(str, (i * 20) + 20, random.nextInt(4) + 30); result += str; } } else { int is = random.nextInt(100); int num1 = 0, num2 = 0; if (is >= 50) { r = random.nextInt(180) + 50; g = random.nextInt(180) + 50; b = random.nextInt(180) + 50; color = new Color(r, g, b); graphics.setColor(color); num1 = random.nextInt(9) + 1; graphics.drawString(num1 + "", 20, random.nextInt(4) + 30);
r = random.nextInt(180) + 50; g = random.nextInt(180) + 50; b = random.nextInt(180) + 50; color = new Color(r, g, b); graphics.setColor(color); graphics.drawString("+", 40, random.nextInt(4) + 30);
r = random.nextInt(180) + 50; g = random.nextInt(180) + 50; b = random.nextInt(180) + 50; color = new Color(r, g, b); graphics.setColor(color); num2 = random.nextInt(9) + 1; graphics.drawString(num2 + "", 60, random.nextInt(4) + 30);
r = random.nextInt(180) + 50; g = random.nextInt(180) + 50; b = random.nextInt(180) + 50; color = new Color(r, g, b); graphics.setColor(color); graphics.drawString("=", 80, random.nextInt(4) + 30);
result = (num1 + num2) + ""; } else { r = random.nextInt(180) + 50; g = random.nextInt(180) + 50; b = random.nextInt(180) + 50; color = new Color(r, g, b); graphics.setColor(color); num1 = random.nextInt(9) + 1; graphics.drawString(num1 + "", 20, random.nextInt(4) + 30);
r = random.nextInt(180) + 50; g = random.nextInt(180) + 50; b = random.nextInt(180) + 50; color = new Color(r, g, b); graphics.setColor(color); graphics.drawString("×", 40, random.nextInt(4) + 30);
r = random.nextInt(180) + 50; g = random.nextInt(180) + 50; b = random.nextInt(180) + 50; color = new Color(r, g, b); graphics.setColor(color); num2 = random.nextInt(9) + 1; graphics.drawString(num2 + "", 60, random.nextInt(4) + 30);
r = random.nextInt(180) + 50; g = random.nextInt(180) + 50; b = random.nextInt(180) + 50; color = new Color(r, g, b); graphics.setColor(color); graphics.drawString("=", 80, random.nextInt(4) + 30);
result = (num1 * num2) + ""; } }
for (int i = 0; i < 3; i++) { r = random.nextInt(50) + 200; g = random.nextInt(50) + 200; b = random.nextInt(50) + 200; color = new Color(r, g, b); graphics.setColor(color); graphics.setStroke(new BasicStroke(1.0f)); graphics.drawLine(0, random.nextInt(imgH), imgW, random.nextInt(imgH)); } return new Object[] { img, result }; }
}
|