Java 自动生成验证码图片

package com.web.framework.slms.module.login.code;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;

public class VerifyCode {
    private int width = 90;
    private int heigh = 40;
    private Random random = new Random();
    private String[] fonts = {"宋体", "微软雅黑", "TimesRoman", "Cambria"};
   // private String chars = "123456789abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
    private String chars = "12ojklpqVWXYrv5yzACstuDEKLM3FGHJ49abchdef678gimBnNPQRSwxTUZ";
    private Color backColor = Color.white;

    /**
     * 获取验证码,
     *
     * @param length 需要获取的长度
     * @return
     */
    public String randomValue(int length) {
        int randomRang = chars.length();
        StringBuilder stringBuilder = new StringBuilder();
        for (int n = 0; n < length; n++) {
            int index = (int) (Math.random() * randomRang);
            stringBuilder.append(chars.charAt(index));
        }
        return stringBuilder.toString();
    }

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

    //method:获取随机字体
    private Font randomFont() {
        int index = random.nextInt(fonts.length);
        String fontName = fonts[index];
        int style = random.nextInt(4);
        int size = 24 + random.nextInt(4);

        return new Font(fontName, style, size);
    }

    /**
     * 随机宽度在验证码图片内的一个值
     *
     * @return
     */
    private int randomWidth() {
        return (int) (Math.random() * width);
    }

    /**
     * 随机高度在验证码图片内的一个值
     *
     * @return
     */
    private int randomHeight() {
        return (int) (Math.random() * heigh);
    }

    //method:获取随机数字
    private String randomNum() {
        int index = random.nextInt(chars.length());
        return chars.charAt(index) + "";
    }

    //method:添加干扰线
    private void drawLine(BufferedImage image) {
        int num = 4;
        Graphics2D graphic = (Graphics2D) image.getGraphics();
        for (int i = 0; i < num; i++) {
            int x1 = random.nextInt(width);
            int x2 = random.nextInt(width);
            int y1 = random.nextInt(heigh);
            int y2 = random.nextInt(heigh);
            graphic.setColor(this.randomColor());
            graphic.setStroke(new BasicStroke(1.5F));
            graphic.drawLine(x1, y1, x2, y2);
        }
    }

    /**
     * 添加干扰圆点
     *
     * @param image
     */
    private void drawPoint(BufferedImage image) {
        int num = 10;
        Graphics2D graphic = (Graphics2D) image.getGraphics();
        for (int n = 0; n < num; n++) {
            graphic.setColor(this.randomColor());
            graphic.fillOval(randomWidth(), randomHeight(), 2, 2);
        }
    }

    //method:绘制方法
    private void drawString(Graphics2D graphics, String s, float position) {
        graphics.setColor(randomColor());
        graphics.setFont(randomFont());
        graphics.drawString(s, position, heigh - 10);
    }

    //获取图片缓存
    public BufferedImage getImage(String drawValue) {
        BufferedImage bi = new BufferedImage(width, heigh, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setColor(this.backColor);
        g2.fillRect(0, 0, 90, 40);
        StringBuilder sb = new StringBuilder();
        float p1 = 5.0F;
        int distance = width / (drawValue.length()+1);
        for (int n = 0; n < drawValue.length(); n++) {
            drawString(g2, drawValue.substring(n, n + 1), p1);
            p1 += distance;
        }


        //获取操作数s1,绘制
//        String s1 = randomNum();
//        this.no1 = Integer.parseInt(s1);
//        //float p1 = 5.0F;
//        sb.append(s1);
//        drawString(g2,s1,p1);
//
//
//        //获取操作符oper,绘制
//        String oper = randomOperator().trim();
//        this.op = oper;
//        sb.append(oper);
//        float p2 = 1.0F*width/5;
//        drawString(g2,oper,p2);
//
//        //获取操作数s2,绘制
//        String s2 = randomNum();
//        this.no2 = Integer.parseInt(s2);
//        float p3 = 2.0F*width/5;
//        sb.append(s2);
//        drawString(g2,s2,p3);
//
//        //绘制 等号和问号
//        String calculate = "=?";
//        float p4 = 3.0F*width/5;
//        drawString(g2,calculate,p4);

        //绘制干扰线
        drawLine(bi);
        //绘制干扰点
        drawPoint(bi);

        //返回BufferedImage图片
        return bi;
    }

    //将图片缓存bi输出到指定的输出流out
    public static void output(BufferedImage bi, OutputStream out)
            throws FileNotFoundException, IOException {
        ImageIO.write(bi, "JPEG", out);
    }

    public static void main(String[] args) {
        VerifyCode verifyCode = new VerifyCode();


        for (int n = 20; n > 0; n--) {
            String code = verifyCode.randomValue(4);
            BufferedImage bi = verifyCode.getImage(code);
            File file = new File("d:/test/code/" + "n" + n + ".jpeg");
            try {
                output(bi, new FileOutputStream(file));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
**注:需在本地文件中,下载备用的验证码背景图n张,随机切换使用**
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值