图形验证码——Java

源码


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

/**
 * @author Yawei Xi
 * @date 2018-10-24
 */
public class ImageCaptcha {
    /**
     * 随机数
     */
    private static final Random RANDOM = new Random();
    /**
     * 字体
     */
    private static final String[] FONT_NAMES = {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312"};
    /**
     * 字母库
     */
    private static final String CODE = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
    /**
     * 字符串的长度
     */
    private int length;
    /**
     * 验证码图片的高度
     */
    private int height;
    /**
     * 验证码图片的宽度
     */
    private int width;
    /**
     * 默认背景颜色
     */
    private Color bgColor;

    public ImageCaptcha() {
        this.bgColor = new Color(255, 255, 255);
        this.length = 4;
        this.height = 35;
        this.width = 70;
    }

    public ImageCaptcha(int length) {
        this.bgColor = new Color(255, 255, 255);
        this.length = length;
        this.height = 35;
        this.width = 70;
    }

    public ImageCaptcha(int length, int width, int height) {
        this.bgColor = new Color(255, 255, 255);
        this.length = length;
        this.height = height;
        this.width = width;
    }

    /**
     * 获取指定长度的随机字符串
     *
     * @return 随机字符串
     */
    private String getRandomString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(CODE.charAt(RANDOM.nextInt(CODE.length())));
        }
        return sb.toString();
    }

    /**
     * 将验证码图片保存为图片
     *
     * @param filePath 需要保存的文件路径
     */
    public void output(String filePath) {
        try {
            OutputStream os = new FileOutputStream(filePath);
            ImageIO.write(getCaptcha(), "JPEG", os);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 随机生成文字的颜色
     *
     * @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() {
        int index = RANDOM.nextInt(FONT_NAMES.length);
        String fontName = FONT_NAMES[index];
        int style = RANDOM.nextInt(3);
        int size = RANDOM.nextInt(5) + 24;
        return new Font(fontName, style, size);
    }

    /**
     * 给验证码画干扰的线条
     *
     * @param image 验证码图片
     * @param w     验证码图片的宽度
     * @param h     验证码图片的高度
     */
    private void drawLine(BufferedImage image, int w, int h) {
        int num = 3;// 画三条
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        for (int i = 0; i < num; i++) {
            int x1 = RANDOM.nextInt(w);
            int y1 = RANDOM.nextInt(h);
            int x2 = RANDOM.nextInt(w);
            int y2 = RANDOM.nextInt(h);
            g2.setStroke(new BasicStroke(1.5F));
            g2.setColor(Color.BLUE);
            g2.drawLine(x1, y1, x2, y2);
        }
    }

    /**
     * 生成验证码的背景图片
     *
     * @param c 颜色
     * @param w 宽度
     * @param h 高度
     * @return 生成的图片
     */
    private BufferedImage createBGImage(Color c, int w, int h) {
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        g2.setColor(c);
        g2.fillRect(0, 0, w, h);
        return image;
    }

    /**
     * 将文字写入图片
     *
     * @param s 需要写进图片的字符串
     * @param c 图片背景颜色
     * @param w 宽度
     * @param h 高度
     * @return 验证码
     */
    private BufferedImage createCaptcha(String s, Color c, int w, int h) {
        BufferedImage image = createBGImage(c, w, h);
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        // 向图片中画字符
        for (int i = 0; i < s.length(); i++) {
            float x = i * 1.0F * w / s.length();
            g2.setFont(randomFont());
            g2.setColor(randomColor());
            // 首字符的基线位于用户空间的 (x, h-5) 位置处 原点在左上角,X轴递增的方向是从左向右;Y轴是从上到下
            // 在提供的坐标位于基线上最左边字符的情况下,可以从右到左呈现字形 h-5表示y轴方向,向上偏移了5
            g2.drawString(String.valueOf(s.charAt(i)), x, h - 5);
        }
        drawLine(image, w, h);
        return image;
    }

    /**
     * 获取验证码图片
     *
     * @return 验证码图片对象
     */
    public BufferedImage getCaptcha() {
        return createCaptcha(getRandomString(), bgColor, width, height);
    }
}


测试用例


/**
 * @author Yawei Xi
 * @date 2018-10-24
 */
public class Test {
    public static void main(String[] args) throws Exception {
        String path = "D:\\test.jpg";
        ImageCaptcha ic = new ImageCaptcha();
        ic.output(path);
    }
}


测试结果

1275166-20181025152817733-644205537.jpg

转载于:https://www.cnblogs.com/freelancy/p/9850140.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值