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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| package com.example.codedemo;
import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.util.Random;
public class VerifyUtil { private static final char[] chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; private final Integer SIZE; private final int LINES; private final int WIDTH; private final int HEIGHT; private final int FONT_SIZE; private final boolean TILT;
private final Color BACKGROUND_COLOR;
private VerifyUtil(Builder builder) { SIZE = builder.size; LINES = builder.lines; WIDTH = builder.width; HEIGHT = builder.height; FONT_SIZE = builder.fontSize; TILT = builder.tilt; BACKGROUND_COLOR = builder.backgroundColor; }
public static Builder newBuilder() { return new Builder(); }
public Object[] createImage() { StringBuffer sb = new StringBuffer(); BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D graphic = image.createGraphics(); graphic.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphic.setColor(BACKGROUND_COLOR); graphic.fillRect(0, 0, WIDTH, HEIGHT); Random ran = new Random();
int codeWidth = WIDTH / (SIZE + 1); int y = HEIGHT * 3 / 4;
for (int i = 0; i < SIZE; i++) { graphic.setColor(getRandomColor()); Font font = new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE);
if (TILT) { int theta = ran.nextInt(45); theta = (ran.nextBoolean() == true) ? theta : -theta; AffineTransform affineTransform = new AffineTransform(); affineTransform.rotate(Math.toRadians(theta), 0, 0); font = font.deriveFont(affineTransform); } graphic.setFont(font);
int x = (i * codeWidth) + (codeWidth / 2);
int n = ran.nextInt(chars.length); String code = String.valueOf(chars[n]); graphic.drawString(code, x, y);
sb.append(code); } for (int i = 0; i < LINES; i++) { graphic.setColor(getRandomColor()); graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH), ran.nextInt(HEIGHT)); } return new Object[]{sb.toString(), image}; }
private Color getRandomColor() { Random ran = new Random(); Color color = new Color(ran.nextInt(256), ran.nextInt(256), ran.nextInt(256)); return color; }
public static class Builder { private int size = 4; private int lines = 10; private int width = 80; private int height = 35; private int fontSize = 25; private boolean tilt = true; private Color backgroundColor = Color.LIGHT_GRAY;
public Builder setSize(int size) { this.size = size; return this; }
public Builder setLines(int lines) { this.lines = lines; return this; }
public Builder setWidth(int width) { this.width = width; return this; }
public Builder setHeight(int height) { this.height = height; return this; }
public Builder setFontSize(int fontSize) { this.fontSize = fontSize; return this; }
public Builder setTilt(boolean tilt) { this.tilt = tilt; return this; }
public Builder setBackgroundColor(Color backgroundColor) { this.backgroundColor = backgroundColor; return this; }
public VerifyUtil build() { return new VerifyUtil(this); } } }
|