java实现验证码功能

使用java实现验证码

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Random;

public class Dish2 {
    public static void main(String[] args) throws Exception {
        int width = 150;
        int height = 50;

        //在内存中生成图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        //绘制图片边框,fillrect是生成一个实心方框,drawrect是生成空心方框
        g.setColor(Color.CYAN);
        g.fillRect(0, 0, width, height);
        //width-1表示减少一个像素,这样才能使线条显示出来,不至于与背景重叠
        g.setColor(Color.BLUE);
        g.drawRect(0, 0, width - 1, height - 1);
        String str = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefgjklmnopqrstuvwxyz";
        Random random = new Random();
        saveString(width, height, str, random, g);
        saveLine(width, height, random, g);
        saveImage(image, "jpg", new FileOutputStream("D:\\javaDemo1\\demo\\a.jpg"));
        System.out.println("生成验证码成功!");
    }

    public static void saveImage(BufferedImage img, String jpg, OutputStream out) throws Exception {
        ImageIO.write(img, "JPEG", out);
    }

    //随机生成颜色
    private static Color getrandomColor(Random random) {
        int colorIndex = random.nextInt(4);
        switch (colorIndex) {
            case 0:
                return Color.BLUE;
            case 1:
                return Color.BLACK;
            case 2:
                return Color.GREEN;
            case 3:
                return Color.red;
            case 4:
                return Color.DARK_GRAY;
            default:
                return Color.YELLOW;
        }
    }

    //绘制验证码的字符
    private static void saveString(int width, int height, String str, Random random, Graphics graphics) {
        for (int i = 0; i < 4; i++) {
            //获取随机的字符
            int index = random.nextInt(str.length());
            char ch = str.charAt(index);
            //获取随机颜色
            Color color = getrandomColor(random);
            graphics.setColor(color);
            //设置字体
            Font font = new Font("宋体", Font.LAYOUT_NO_LIMIT_CONTEXT, height / 2);
            graphics.setFont(font);
            //写入验证码
            graphics.drawString(ch + "", (i == 0) ? width / 4 * i + 2 : width / 4 * i, height - height / 4);
        }
    }

    //绘制干扰线
    private static void saveLine(int width, int height, Random random, Graphics graphics) {
        Color lineColor = getrandomColor(random);
        for (int i = 0; i < 10; i++) {
            int x1 = random.nextInt(width);
            int x2 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int y2 = random.nextInt(height);
            Color randomColor = lineColor;
            graphics.setColor(randomColor);
            graphics.drawLine(x1, x2, y1, y2);
        }
    }
}

效果图如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值