码迷,mamicode.com
首页 > 编程语言 > 详细

Java生成验证码图片

时间:2021-06-11 18:45:09      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:tin   code   har   dom   set   oid   ring   get   private   

效果图

技术图片




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生成验证码图片

标签:tin   code   har   dom   set   oid   ring   get   private   

原文地址:https://www.cnblogs.com/lyd447113735/p/14874614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!