用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;

/**
 * <p>Project: JavaStudy - Image1</p>
 * <p>Powered by scl On 2023-07-25 09:44:07</p>
 * <p>描述:<p>
 *
 * @author scl [1846080280@qq.com]
 * @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);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值