Java随机验证码图片生成模块

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

/**
 绘制验证码
 @param  "width:验证码宽度,height:高度,codeCount:字符个数,lineCount:干扰线条数量"
 @return 验证码对象
 @author wh445306
 */

public class CreateImageCode {
    // 图片的宽度。
    private final int width ;
    // 图片的高度。
    private final int height;
    // 验证码字符个数
    private final int codeCount;
    // 验证码干扰线数
    private final int lineCount;
    // 验证码
    private String code = null;
    // 验证码图片Buffer
    private BufferedImage buffImg = null;
    Random random = new Random();

    public CreateImageCode(int width, int height, int codeCount, int lineCount) {
        this.width = width;
        this.height = height;
        this.codeCount = codeCount;
        this.lineCount = lineCount;
        creatImage();
    }

    private void creatImage() {
        int fontWidth = width / codeCount;// 字体的宽度
        int fontHeight = height - 5;// 字体的高度
        int codeY = height - 8;

        // 创建Graphic画板
        buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = buffImg.getGraphics();
        // 绘制背景色
        g.setColor(getRandomColor(200, 250));
        g.fillRect(0, 0, width, height);

        // 绘制字体
        Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
        g.setFont(font);

        // 绘制干扰线
        for (int i = 0; i < lineCount; i++) {
            int xs = random.nextInt(width);
            int ys = random.nextInt(height);
            int xe = xs + random.nextInt(width);
            int ye = ys + random.nextInt(height);
            g.setColor(getRandomColor(1, 255));
            g.drawLine(xs, ys, xe, ye);
        }

        // 绘制噪点
        float fRate = 0.01f;// 噪声率0.01f
        int area = (int) (fRate * width * height);
        for (int i = 0; i < area; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            buffImg.setRGB(x, y, random.nextInt(255));
        }

        String sCode = GetRandomStr(codeCount);
        this.code = sCode;
        for (int i = 0; i < codeCount; i++) {
            String strRand = sCode.substring(i, i + 1);
            g.setColor(getRandomColor(1, 255));
            g.drawString(strRand, i * fontWidth + 3, codeY);
        }
    }

    // 获取指定长度随机字符
    private static String GetRandomStr(int len) {
        StringBuilder str= new StringBuilder();
        final String SourceStr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random r= new Random();
        for (int i=0; i<len; i++) {
            str.append(SourceStr.charAt(r.nextInt(62) + 1));
        }
        return str.toString();
    }

    //获得指定范围随机颜色(0-255)
    private Color getRandomColor(int s, int e) {
        int r = s + random.nextInt(e - s);
        int g = s + random.nextInt(e - s);
        int b = s + random.nextInt(e - s);
        return new Color(r, g, b);
    }

    public void write(OutputStream out) throws IOException {
        ImageIO.write(buffImg, "png", out);
        out.close();
    }

    public void write(File file) throws IOException {
        ImageIO.write(buffImg, "png", file);
    }

    public String getCode() {
        return code.toLowerCase();
    }

    //web调用
 /*  public void getCode3(HttpServletRequest req, HttpServletResponse response,HttpSession session) throws IOException{
      CreateImageCode vCode = new CreateImageCode(100,30,5,10);
      session.setAttribute("code", vCode.getCode());
      vCode.write(response.getOutputStream());
     }  */
    //本地保存
    public static void main(String[] args) throws IOException {
        File dir = new File("d:\\verifies");
        CreateImageCode vCode = new CreateImageCode(100, 30, 5, 10);
       // CreateImageCode vCode1 = new CreateImageCode(120, 30, 6, 10);
        File file = new File(dir, vCode.getCode() + ".png");
        vCode.write(file);
    }

}

效果:

 ,

可自定义宽度,高度,验证字符个数,干扰线条数量,噪点数量,背景色等。

只是简单封装实现,实际使用中根据自己需要可以再继续修改完善,喜欢的话自己也可以封装成静态调用类,很简单的东西。谢谢!

前面7张为上面的代码生成,后面六张不是,是我后来单独重新开发的在项目中正在使用的模块生成,同时有JS前端生成和java后端生成两份不同的代码,两者生成的验证码效果完全一样,暂时不能免费开源。需要可有偿提供

猜你喜欢

转载自blog.csdn.net/wh445306/article/details/111570795