zoukankan      html  css  js  c++  java
  • 使用Java绘制验证码

    效果图:

    JDemo.java

    import java.io.File;
    import java.io.IOException;
    import static java.lang.System.out;
    import javax.imageio.ImageIO;
    
    public class JDemo {
    
        public static void main(String[] args) throws IOException {
            VerificationCode verificationCode = new VerificationCode(7);
            ImageIO.write(verificationCode.getImage(), "png", new File("C:\Users\BuYishi\Desktop\New folder\JDemo.png"));
            out.println(verificationCode.getContent());
        }
    }

    VerificationCode.java

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    
    public class VerificationCode {
    
        private StringBuilder verificationCodeContent;
    
        public VerificationCode(int length) {
            verificationCodeContent = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < length; ++i) {
                int charType = random.nextInt(3);  //随机的验证码字符类型,0表示数字,1表示小写字母,2表示大写字母
                char c;
                switch (charType) {
                    case 0:
                        c = (char) (random.nextInt(10) + '0');
                        break;
                    case 1:
                        c = (char) (random.nextInt(26) + 'a');
                        break;
                    default:
                        c = (char) (random.nextInt(26) + 'A');
                }
                verificationCodeContent.append(c);
            }
        }
    
        public String getContent() {
            return verificationCodeContent.toString();
        }
    
        public BufferedImage getImage() {
            int width = 200, height = 50;
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics graphics = image.getGraphics();
            graphics.setColor(Color.red);
            graphics.fillRect(0, 0, width, height);  //绘制验证码图像背景为红色
            Font currentFont = graphics.getFont();
            Font newFont = new Font(currentFont.getFontName(), currentFont.getStyle(), 30);
            FontMetrics fontMetrics = graphics.getFontMetrics(newFont);
            String string = verificationCodeContent.toString();
            graphics.setColor(Color.green);
            graphics.setFont(newFont);
            //绘制绿色的验证码,并使其居中
            graphics.drawString(string, (width - fontMetrics.stringWidth(string)) / 2, (height - fontMetrics.getHeight()) / 2 + fontMetrics.getAscent());
            graphics.setColor(Color.blue);
            Random random = new Random();
            for (int i = 0; i < 20; ++i) {  //绘制20条干扰线,其颜色为蓝
                int x1 = random.nextInt(width), y1 = random.nextInt(height);
                int x2 = random.nextInt(width), y2 = random.nextInt(height);
                graphics.drawLine(x1, y1, x2, y2);
            }
            return image;
        }
    }
  • 相关阅读:
    笔记(二) C#sql语句
    [叩响C#之门]写给初学者:多线程系列(七)——互锁(Interlocked类)
    C# Async与Await的使用
    C#线程锁使用全功略
    一个C#的加锁解锁示例
    【分析】浅谈C#中Control的Invoke与BeginInvoke在主副线程中的执行顺序和区别(SamWang)
    Control.BeginInvoke()和delegate的BeginInvoke()的区别
    crm04 action操作 和 多级过滤
    VIM和sed 替换字符串方法
    解决Centos关闭You have new mail in /var/spool/mail/root提示(转)
  • 原文地址:https://www.cnblogs.com/buyishi/p/9736146.html
Copyright © 2011-2022 走看看