分享一個Java生成二維碼工具類

分享一個Java生成二維碼工具類

直接上代碼:

1、CodeUtil.class

package top.lrshuai.blog.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 驗證碼工具類
 *
 */
public class CodeUtil {

    private static final String[] CODE = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P",
            "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

    /**
     * 生成驗證碼圖片
     * 
     * @return obj[0]: 圖片; obj[1]:字符串
     */
    public static Object[] CreateCode() {
        int imgW = 120;
        int imgH = 42;
        int r, g, b;
        Color color;
        String result = "";
        Random random = new Random();
        BufferedImage img = new BufferedImage(imgW, imgH, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = img.createGraphics();
        graphics.setFont(new Font("MicroSoft YaHei", Font.PLAIN, 30));

        // 繪製背景色
        r = random.nextInt(20) + 230;
        g = random.nextInt(20) + 230;
        b = random.nextInt(20) + 230;
        color = new Color(r, g, b);
        graphics.setColor(color);
        graphics.fillRect(0, 0, imgW, imgH);

        // 繪製背景干擾線條
        for (int i = 0; i < 3; i++) {
            r = random.nextInt(50) + 200;
            g = random.nextInt(50) + 200;
            b = random.nextInt(50) + 200;
            color = new Color(r, g, b);
            graphics.setColor(color);
            graphics.setStroke(new BasicStroke(2.0f));
            graphics.drawLine(random.nextInt(20), random.nextInt(imgH), random.nextInt(20) + 80, random.nextInt(imgH));
        }

        if (random.nextInt(100) >= 50) {
            // 繪製字符串驗證碼
            for (int i = 0; i < 4; i++) {
                String str = CODE[random.nextInt(CODE.length)];
                r = random.nextInt(180) + 50;
                g = random.nextInt(180) + 50;
                b = random.nextInt(180) + 50;
                color = new Color(r, g, b);
                graphics.setColor(color);
                graphics.drawString(str, (i * 20) + 20, random.nextInt(4) + 30);
                result += str;
            }
        } else {
            // 繪製計算題驗證碼
            int is = random.nextInt(100);
            int num1 = 0, num2 = 0;
            if (is >= 50) {
                // 加法
                r = random.nextInt(180) + 50;
                g = random.nextInt(180) + 50;
                b = random.nextInt(180) + 50;
                color = new Color(r, g, b);
                graphics.setColor(color);
                num1 = random.nextInt(9) + 1;
                graphics.drawString(num1 + "", 20, random.nextInt(4) + 30);

                r = random.nextInt(180) + 50;
                g = random.nextInt(180) + 50;
                b = random.nextInt(180) + 50;
                color = new Color(r, g, b);
                graphics.setColor(color);
                graphics.drawString("+", 40, random.nextInt(4) + 30);

                r = random.nextInt(180) + 50;
                g = random.nextInt(180) + 50;
                b = random.nextInt(180) + 50;
                color = new Color(r, g, b);
                graphics.setColor(color);
                num2 = random.nextInt(9) + 1;
                graphics.drawString(num2 + "", 60, random.nextInt(4) + 30);

                r = random.nextInt(180) + 50;
                g = random.nextInt(180) + 50;
                b = random.nextInt(180) + 50;
                color = new Color(r, g, b);
                graphics.setColor(color);
                graphics.drawString("=", 80, random.nextInt(4) + 30);

                result = (num1 + num2) + "";
            } else {
                // 乘法
                r = random.nextInt(180) + 50;
                g = random.nextInt(180) + 50;
                b = random.nextInt(180) + 50;
                color = new Color(r, g, b);
                graphics.setColor(color);
                num1 = random.nextInt(9) + 1;
                graphics.drawString(num1 + "", 20, random.nextInt(4) + 30);

                r = random.nextInt(180) + 50;
                g = random.nextInt(180) + 50;
                b = random.nextInt(180) + 50;
                color = new Color(r, g, b);
                graphics.setColor(color);
                graphics.drawString("×", 40, random.nextInt(4) + 30);

                r = random.nextInt(180) + 50;
                g = random.nextInt(180) + 50;
                b = random.nextInt(180) + 50;
                color = new Color(r, g, b);
                graphics.setColor(color);
                num2 = random.nextInt(9) + 1;
                graphics.drawString(num2 + "", 60, random.nextInt(4) + 30);

                r = random.nextInt(180) + 50;
                g = random.nextInt(180) + 50;
                b = random.nextInt(180) + 50;
                color = new Color(r, g, b);
                graphics.setColor(color);
                graphics.drawString("=", 80, random.nextInt(4) + 30);

                result = (num1 * num2) + "";
            }
        }

        // 繪製前景干擾線條
        for (int i = 0; i < 3; i++) {
            r = random.nextInt(50) + 200;
            g = random.nextInt(50) + 200;
            b = random.nextInt(50) + 200;
            color = new Color(r, g, b);
            graphics.setColor(color);
            graphics.setStroke(new BasicStroke(1.0f));
            graphics.drawLine(0, random.nextInt(imgH), imgW, random.nextInt(imgH));
        }
        return new Object[] { img, result };
    }

}

2、controller 請求

/**
* 生成驗證碼
* @throws IOException 
*/
@GetMapping(value = "/code")
public String getCode(HttpServletResponse response){
    OutputStream os = null;
    try {
        // 獲取圖片
        Object[] img = CodeUtil.CreateCode();
        System.out.println("code="+img[1].toString());
        BufferedImage image = (BufferedImage) img[0];
        // 輸出到瀏覽器
        response.setContentType("image/png");
        os = response.getOutputStream();
        ImageIO.write(image, "png", os);
        os.flush();
        // 用於驗證的字符串存入session
        this.getSession().setAttribute(Const.AUTH_CODE, img[1].toString());
    } catch (IOException e) {
        log.error("驗證碼輸出異常",e);
    }finally {
        if(os != null) {
            try {
                os.close();
            } catch (IOException e) {
                System.out.println("close error");
                e.printStackTrace();
            }
        }
    }
    return null;
}

3、html 頁面調用

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
</head>
<body>
<img id="authCode" onclick="changeCode()" src="/code" alt="驗證碼" title="點擊更換" />

<script>
function changeCode() {
    document.getElementById("authCode").src = "/code?t=" + genTimestamp();
}
function genTimestamp() {
    var time = new Date();
    return time.getTime();
}
</script>
</body>
</html>

分享結束,覺得不錯,點個贊。
我的個人博客:http://www.lrshuai.top/blog

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章