用Java教你写验证码。

/*
 * Copyright (c) 2020, 2023,  All rights reserved.
 *
 */
package cn.scl1;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;

/**
 * 

Project: JavaStudy - Image1

*

Powered by scl On 2023-07-25 09:44:07

*

描述:

* * @author scl [[email protected]] * @version 1.0 * @since 17 */ public class Image1 { public static void main(String[] args) throws Exception { //建立图像 int w = 160; int h = 50; int len = 4; BufferedImage i = new BufferedImage(w, h, 2); //获取画笔 Graphics2D g = i.createGraphics(); //设置线条何文件的,抗锯齿,平滑 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT); //设置画笔颜色 g.setColor(new Color(242, 242, 242)); //绘制填充矩形 g.fillRect(0, 0, w, h); String letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; StringBuilder str = new StringBuilder(); Font font = new Font("宋体", Font.BOLD, 10); Random random = new Random(); //验证码文字 g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256), random.nextInt(190, 230))); for (int j = 0; j < len; j++) { font = font.deriveFont(Font.BOLD, random.nextInt(20, 40)); g.setFont(font); int x = w / 4 * j + 15; int y = random.nextInt(35, 50); //设置每个字符的随机旋转 double radianPercent = Math.PI * (random.nextInt(35) / 180D); if (random.nextBoolean()) radianPercent = -radianPercent; g.rotate(radianPercent, x, y); String c = String.valueOf(letter.charAt(random.nextInt(letter.length()))); str.append(c); g.drawString(c, x, y); g.rotate(-radianPercent, x, y); } //干扰文字 int dot = random.nextInt(5, 30); for (int j = 0; j < dot; j++) { g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256), random.nextInt(80, 100))); font = font.deriveFont(Font.BOLD, random.nextInt(10, 20)); g.setFont(font); int x = random.nextInt(w); int y = random.nextInt(h); //设置每个字符的随机旋转 double radianPercent = Math.PI * (random.nextInt(35) / 180D); if (random.nextBoolean()) radianPercent = -radianPercent; g.rotate(radianPercent, x, y); String c = String.valueOf(letter.charAt(random.nextInt(letter.length()))); g.drawString(c, x, y); g.rotate(-radianPercent, x, y); } System.out.println(str); //干扰线 for (int i1 = 0; i1 < 15; i1++) { g.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256), random.nextInt(50, 90))); if (Math.random() > .8) { g.setColor(getRandColor(true)); } g.setStroke(new BasicStroke(random.nextInt(1, 6))); int x1 = random.nextInt(w); int y1 = random.nextInt(h); int x2 = random.nextInt(w); int y2 = random.nextInt(h); g.drawLine(x1, y1, x2, y2); } //释放画笔对象占用的内存资源 g.dispose(); //保存图像 ImageIO.write(i, "png", new File("ck.png")); } public static Color getRandColor(boolean c) { Random rand = new Random(); Color color = c ? new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)) : new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)); return color; } public static Color getRandColor() { return getRandColor(false); } }

用Java教你写验证码。_第1张图片

你可能感兴趣的:(java,开发语言,算法)