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
| import java.awt.*; import java.awt.image.BufferedImage; import java.util.HashMap; import java.util.Map; import java.util.Random;
public class VerifyCodeUtil { private static final int width = 100; private static final int height = 40; private static final int codeCount = 4; private static final int xx = 18; private static final int codeY = 27; private static final int fontSize = 20;
private static final char[] codeSequence = { '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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
public static Map<String, Object> generateCodeAndPic() { BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics gd = buffImg.getGraphics(); Random random = new Random(); gd.setColor(Color.WHITE); gd.fillRect(0, 0, width, height); Font font = new Font("思源黑体", Font.BOLD, fontSize); gd.setFont(font); gd.setColor(Color.BLACK); gd.drawRect(0, 0, width - 1, height - 1); gd.setColor(Color.BLACK); for (int i = 0; i < 30; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); gd.drawLine(x, y, x + xl, y + yl); } StringBuffer randomCode = new StringBuffer(); int red, green, blue; for (int i = 0; i < codeCount; i++) { String code = String.valueOf(codeSequence[random.nextInt(30)]); red = random.nextInt(255); green = random.nextInt(255); blue = random.nextInt(255); gd.setColor(new Color(red, green, blue)); gd.drawString(code, (i + 1) * xx, codeY); randomCode.append(code); }
Map<String, Object> map = new HashMap<>(); map.put("code", randomCode); map.put("codePic", buffImg); return map; } }
|