Java工具--文字验证码生成

1概述

针对简单的验证,我们可以使用文字验证码,而没必要使用滑块验证的方式。

2工具源码

生成的验证码图片直接以二进制的形式进行返回,如果前端获取到该资源需要进行二进制图片展示。

package com.liutao.util;

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

/**
 * 验证码工具类
 *
 * @author: LIUTAO
 * @Description:
 * @Date: Created in 10:57 2018/6/25
 * @Modified By:
 */
public class VerifyCodeUtil {
    private static int width = 60;  //验证码宽度
    private static int height = 20;  //验证码高度
    private static int codeCount = 4;  //验证码字符个数
    private static int fontHeight;  //字体高度

    /**
     * 初始化图片属性
     */
    public static void init() {
        String strWidth ="800";
        String strHeight ="300";
        String strCodeCount = "4";
        // 将配置的信息转换成数值
        try {
            if (strWidth != null && strWidth.length() != 0) {
                width = Integer.parseInt(strWidth);
            }
            if (strHeight != null && strHeight.length() != 0) {
                height = Integer.parseInt(strHeight);
            }
            if (strCodeCount != null && strCodeCount.length() != 0) {
                codeCount = Integer.parseInt(strCodeCount);
            }
        } catch (NumberFormatException e) {
        }
        fontHeight = height/2;
    }


    /**
     * 获取验证码图片
     * @return
     * @throws IOException
     */
    public static byte[] getVerifyImg() throws IOException {
        init();
        Random random = new Random();

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics graphic = image.getGraphics();

        graphic.setColor(Color.getColor("F8F8F8"));
        graphic.fillRect(0, 0, width, height);

        Color[] colors = new Color[] { Color.BLUE, Color.GRAY, Color.GREEN, Color.RED, Color.BLACK, Color.ORANGE,
                Color.CYAN };
        // 在 "画板"上生成干扰线条 ( 50 是线条个数)
        for (int i = 0; i < 50; i++) {
            graphic.setColor(colors[random.nextInt(colors.length)]);
            final int x = random.nextInt(width);
            final int y = random.nextInt(height);
            final int w = random.nextInt(20);
            final int h = random.nextInt(20);
            final int signA = random.nextBoolean() ? 1 : -1;
            final int signB = random.nextBoolean() ? 1 : -1;
            graphic.drawLine(x, y, x + w * signA, y + h * signB);
        }

        // 在 "画板"上绘制字母
        graphic.setFont(new Font("Comic Sans MS", Font.BOLD, fontHeight));
        for (int i = 0; i < codeCount; i++) {
            final int temp = random.nextInt(26) + 97;
            String s = String.valueOf((char) temp);
            graphic.setColor(colors[random.nextInt(colors.length)]);
            graphic.drawString(s, i * (width / 6), height - (height / 3));
        }
        graphic.dispose();

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, "png", os);
        byte b[] = os.toByteArray();
        return b;
    }
}

3测试代码

   private static void testCode() throws IOException {
        byte[] newImages = VerifyCodeUtil.getVerifyImg();
        FileOutputStream fout = new FileOutputStream("C:/Users/Administrator/Desktop/verigy.png");
        fout.write(newImages);
        fout.close();
    }

源码可以参考:图片验证码

你可能感兴趣的:(TOOL)