• 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;
        }
    
    
    }
    
    
  • 相关阅读:
    Blender/UE4骨骼转换插件:Uefy v1.2.1 For UE4 BL 2.8+使用教程
    01【Daz模型】DazStudio 高品质可爱的小姐姐角色模型包 Lancy Character with dForce Hair and Expressions for Genesis 8 Female (含角色、头发、扩展)
    创作设计抖音免版权高清图库整理
    GPU三维地形生成软件 World Creator v2.1.0 Win
    [人物存档]【AI少女】【捏脸数据】【捏人数据】【人物卡】:女巫八重樱
    ubuntu Linux下C语言open函数打开或创建文件与read,write函数详细讲解
    OO第二次博客作业(第二单元总结)
    OO第一次博客作业(第一单元总结)
    Beta阶段 第四次Scrum Meeting
    Beta阶段 第三次Scrum Meeting
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/14874614.html
Copyright © 2020-2023  润新知