Java 验证码生成,随机颜色字体

验证码生成工具类

package com.kongjs.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class YanCode {

    private static int width;
    private static int height;
    private static int codeLen;

    private int getRandomInt(int n1, int n2) {
        return (int) Math.abs((Math.random() * (n2 - n1)));
    }

    private Font getRandomFont() {
        String[] fonts = {"Georgia", "Verdana", "Arial", "Tahoma", "Time News Roman", "Courier New", "Arial Black", "Quantzite"};
        int fontIndex = (int) Math.round(Math.random() * (fonts.length - 1));
        int fontSize = (int) Math.round(Math.random() * 4 + 36);
        return new Font(fonts[fontIndex], Font.PLAIN, fontSize);
    }

    private Color getColor() {
        Random random = new Random();
        int r = 0, g = 0, b = 0;
        for (int i = 1; i < 255; i++) {
            for (int j = 1; j < 255; j++) {
                r = random.nextInt(i) + 1;
                g = random.nextInt(j) + 1;
                b = random.nextInt((i + j) / 2) + 1;
            }
        }
        return new Color(r, g, b);
    }

    private String getStrCode() {
        char[] ch1 = new char[26];
        char[] ch2 = new char[26];
        int a = 97;
        int A = 65;
        for (int i = 0; i < ch1.length; i++) {
            ch1[i] = (char) (a++);
            ch2[i] = (char) (A++);
        }
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 2; i < 33; i++) {
            sb.append(r.nextInt(i - 1));
            sb.append(ch1[r.nextInt(ch1.length)]);
            sb.append(r.nextInt(i));
            sb.reverse();
            sb.append(ch2[r.nextInt(ch2.length)]);
            sb.append(r.nextInt(i - 1));
            sb.reverse();
        }
        String str = String.valueOf(sb);
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < codeLen; i++) {
            code.append(str.charAt(r.nextInt(33)));
        }
        return new String(code);
    }

    private BufferedImage bufferedImage = getBufferedImage();

    synchronized private BufferedImage getBufferedImage() {
        if (bufferedImage == null) {
            bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        }
        return bufferedImage;
    }

    private BufferedImage getImage(String strCode) {
        Graphics graphics = bufferedImage.getGraphics();
        graphics.setColor(getColor());
        graphics.fillRect(0, 0, width, height);
        graphics.setFont(getRandomFont());
        graphics.setColor(getColor());
        int x = (int) Math.round(width * 0.2);
        int y = (int) Math.round(height * 0.7);
        graphics.drawString(strCode, x, y);
        graphics.setColor(getColor());
        graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
        graphics.setColor(getColor());
        graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
        graphics.setColor(getColor());
        graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
        graphics.setColor(getColor());
        graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
        graphics.setColor(getColor());
        graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
        graphics.setColor(getColor());
        graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
        graphics.setColor(getColor());
        graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
        graphics.setColor(getColor());
        graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
        graphics.setColor(getColor());
        graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
        graphics.setColor(getColor());
        // 可以用循环替代重复的
         for (int i = 0; i <10 ; i++) {
            graphics.drawLine(getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height), getRandomInt(width, height));
            graphics.setColor(getColor());
        }
        return bufferedImage;
    }

    private YanCode() {

    }

    static {
        width = 150;
        height = 50;
        codeLen = 4;
    }

    static synchronized private YanCode getInstance() {

        return new YanCode();
    }

    public static BufferedImage getImageCode(String strCode) {
        return getInstance().getImage(strCode);
    }

    public static String getStringCode() {
        return getInstance().getStrCode();
    }

    public static void setWidth(int width) {
        YanCode.width = width;
    }

    public static void setHeight(int height) {
        YanCode.height = height;
    }

    public static void setCodeLen(int codeLen) {
        YanCode.codeLen = codeLen;
    }
}

测试代码如下

public class TestCode {
    @Test
    public void Cofr(){
        YanCode.setCodeLen(3);

        String stringCode = YanCode.getStringCode();
        System.out.println("stringCode = " + stringCode);
        BufferedImage imageCode = YanCode.getImageCode(stringCode);


        try {
            ImageIO.write(imageCode, "png", new FileOutputStream("code.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

servlet 请求

@WebServlet("/code")
public class CodeServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", -1);
        response.setContentType("image/png");
        ServletOutputStream sos;
        try {
            sos = response.getOutputStream();
            String code = YanCode.getStringCode();
            System.out.println("code = " + code);
            ImageIO.write(YanCode.getImageCode(code),"png", sos);
            sos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值