Ürün, Orijinal MAN Eş替换为:强 "))); } /** * 生成随机验证码并将其绘制到canvas上 */ public static void generateAndDrawCaptcha(Graphics2D graphics) { String captcha = generateCaptcha(); // 在图片上绘制文字 int x = (WIDTH - captcha.length() * fontSize.width / 3) / 2; // 计算文字的起始x坐标,使其居中 graphics.setColor(Color.WHITE); graphics.setFont(new Font("Arial", Font.BOLD, fontSize.height)); for (int i = 0; i < captcha.length(); i++) { char c = captcha.charAt(i); FontRenderContext frc = graphics.getFontRenderContext(); Rectangle2D bounds = graphics.getFont().getStringBounds(String.valueOf(c), frc); int ascent = (int) bounds.getY(); int baseY = ascent + (int) bounds.getHeight(); int stringWidth = (int) bounds.getWidth(); if (i != 0) { x += stringWidth / 2; // 在每个字符之间添加空隙 } graphics.setColor(randomColor()); graphics.drawString(String.valueOf(c), x, baseY); x += stringWidth + random.nextInt(4); // 每个字符后添加随机宽度的间隔 } } /** * 生成一个随机颜色。 */ private static Color randomColor() { return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)); } } ```
MAN 51.02503-0792