栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现验证码功能

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);
        }
    }
}

效果图如下:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/353855.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号