Java生成验证码图片

2021/6/11 12:50:54

本文主要是介绍Java生成验证码图片,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

效果图



import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 生成4个字符的验证码,背景颜色、字体颜色、字体类型都随机生成,另外加上干扰线条
 */

public class VerifyCode {

    private Random random = new Random();
    private int width = 100;// 生成验证码图片的宽度
    private int height = 30;// 生成验证码图片的高度
    //    字体
    private String[] fontNames = {"宋体", "楷体", "隶书", "微软雅黑"};
    //    背景颜色
    private Color bgColor = new Color(255, 255, 255);
    //    从下面的字符串中挑选字符放入验证码集中
    private String codes = "0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
    //    记录随机字符串
    private String text;

    public VerifyCode() {
        getVerifyCode();
    }

    /**
     * 获得一个随机颜色
     *
     * @return
     */
    private Color randomColor() {
        int red = random.nextInt(150);
        int green = random.nextInt(150);
        int blue = random.nextInt(150);
        return new Color(red, green, blue);
    }

    /**
     * 获得一个随机字体
     *
     * @return
     */
    private Font randomFont() {
        String name = fontNames[random.nextInt(fontNames.length)];
        int style = random.nextInt(4);
        int size = random.nextInt(5) + 24;
        return new Font(name, style, size);
    }

    /**
     * 获得一个随机字符
     *
     * @return
     */
    private char randomChar() {
        return codes.charAt(random.nextInt(codes.length()));
    }

    /**
     * BufferedImage
     * 验证码背景图
     *
     * @return
     */
    private BufferedImage createImage() {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        g2.setColor(bgColor);// 设置验证码图片的背景颜色
        g2.fillRect(0, 0, width, height);
        return image;
    }

    /**
     * 给图片添加干扰线条
     *
     * @param image
     */
    private void drawLine(BufferedImage image) {
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        int num = 5;
        for (int i = 0; i < num; i++) {
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            g2.setColor(randomColor());
            g2.setStroke(new BasicStroke(1.5f));
            g2.drawLine(x1, y1, x2, y2);
        }
    }

    /**
     * 得到验证码图片
     *
     * @return
     */
    public BufferedImage getVerifyCode() {
        BufferedImage image = createImage();
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 4; i++) {
//            字符
            String s = randomChar() + "";
            sb.append(s);
//            字符属性
            g2.setColor(randomColor());
            g2.setFont(randomFont());
            float x = i * width * 1.0f / 4;
            g2.drawString(s, x, height - 8);
        }
        this.text = sb.toString();
        drawLine(image);
        return image;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }


}



这篇关于Java生成验证码图片的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程