java驗(yàn)證碼生成具體代碼
本文實(shí)例為大家分享了java驗(yàn)證碼生成的示例代碼,供大家參考,具體內(nèi)容如下
package com.gonvan.component.captcha; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Created by yuerzm on 2016/3/14. */ public class CaptchaFactory { private static final char[] CODE_SEQUENCE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" .toCharArray(); private static final int DEFAULT_WIDTH = 60; private static final int DEFAULT_HEIGHT = 20; private static final int DEFAULT_CODE_LEN = 4; private static final int DEFAULT_CODE_X = 13; private static final int DEFAULT_CODE_Y = 16; private static final int DEFAULT_FONT_SIZE = 18; private static final String DEFAULT_FONT_FAMILY = "Times New Roman"; private static CaptchaFactory instance = new CaptchaFactory(); private int width = DEFAULT_WIDTH; // 定義圖片的width private int height = DEFAULT_HEIGHT; // 定義圖片的height private int length = DEFAULT_CODE_LEN; // 定義圖片上顯示驗(yàn)證碼的個(gè)數(shù) private int xx = DEFAULT_CODE_X; // 定義圖片上顯示驗(yàn)證碼x坐標(biāo) private int yy = DEFAULT_CODE_Y; // 定義圖片上顯示驗(yàn)證碼y坐標(biāo) private int fontSize = DEFAULT_FONT_SIZE; // 定義圖片上顯示驗(yàn)證碼的字體大小 private String fontFamily = DEFAULT_FONT_FAMILY; // 定義圖片上顯示驗(yàn)證碼的個(gè)數(shù) private CaptchaFactory() { } public static CaptchaFactory getInstance() { return instance; } /** * 配置寬高 * * @param w * @param h * @return */ public CaptchaFactory configWidthAndHeight(int w, int h) { instance.width = w; instance.height = h; return instance; } /** * 配置坐標(biāo) * * @param x * @param y * @return */ public CaptchaFactory configXY(int x, int y) { instance.xx = x; instance.yy = y; return instance; } /** * 配置字體大小 * * @param fontSize * @return */ public CaptchaFactory configFontSize(int fontSize) { instance.fontSize = fontSize; return instance; } /** * 配置字體 * * @param fontFamily * @return */ public CaptchaFactory configFontSize(String fontFamily) { instance.fontFamily = fontFamily; return instance; } public void write(HttpServletRequest request, HttpServletResponse response) throws IOException { // 將四位數(shù)字的驗(yàn)證碼保存到Session中。 Map captcha = generate(); String randomCode = (String) captcha.get("captchaCode"); BufferedImage buffImg = (BufferedImage) captcha.get("captchaImg"); HttpSession session = request.getSession(); session.setAttribute("code", randomCode); // 禁止圖像緩存。 response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); // 將圖像輸出到Servlet輸出流中。 ServletOutputStream outputStream = response.getOutputStream(); ImageIO.write(buffImg, "jpeg", outputStream); outputStream.close(); } public Map<String, Object> generate() throws IOException { // 定義圖像buffer BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics gd = buffImg.getGraphics(); // 設(shè)定背景色 gd.setColor(getRandColor(200, 250)); gd.fillRect(0, 0, width, height); // 設(shè)定字體,字體的大小應(yīng)該根據(jù)圖片的高度來(lái)定。 gd.setFont(new Font(fontFamily, Font.PLAIN, fontSize)); // 創(chuàng)建一個(gè)隨機(jī)數(shù)生成器類 Random random = new Random(); // 隨機(jī)產(chǎn)生40條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到。 gd.setColor(getRandColor(160, 200)); for (int i = 0; i < 155; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); gd.drawLine(x, y, x + xl, y + yl); } // randomCode用于保存隨機(jī)產(chǎn)生的驗(yàn)證碼,以便用戶登錄后進(jìn)行驗(yàn)證。 StringBuffer randomCode = new StringBuffer(); int red = 0, green = 0, blue = 0; // 隨機(jī)產(chǎn)生 length 個(gè)驗(yàn)證碼。 for (int i = 0; i < length; i++) { // 得到隨機(jī)產(chǎn)生的驗(yàn)證碼數(shù)字。 String code = String.valueOf(CODE_SEQUENCE[random.nextInt(36)]); // 產(chǎn)生隨機(jī)的顏色分量來(lái)構(gòu)造顏色值,這樣輸出的每位數(shù)字的顏色值都將不同。 red = random.nextInt(110); green = random.nextInt(110); blue = random.nextInt(110); // 用隨機(jī)產(chǎn)生的顏色將驗(yàn)證碼繪制到圖像中。 gd.setColor(new Color(red + 20, green + 20, blue + 20)); gd.drawString(code, i * xx + 6, yy); // 將產(chǎn)生的隨機(jī)數(shù)組合在一起。 randomCode.append(code); } Map<String, Object> retval = new HashMap<>(); retval.put("captchaCode", randomCode.toString()); retval.put("captchaImg", buffImg); return retval; } /** * 給定范圍獲得隨機(jī)顏色 * * @param fc * 最小值 * @param bc * 最大值 * @return Color */ private Color getRandColor(int fc, int bc) { Random random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
SpringBoot編譯target目錄下沒有resource下的文件踩坑記錄
這篇文章主要介紹了SpringBoot編譯target目錄下沒有resource下的文件踩坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08IDEA Java win10環(huán)境配置的圖文教程
這篇文章主要介紹了IDEA Java win10環(huán)境配置,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07java多線程中執(zhí)行多個(gè)程序的實(shí)例分析
在本篇文章里小編給大家整理的是一篇關(guān)于java多線程中執(zhí)行多個(gè)程序的實(shí)例分析內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。2021-02-02關(guān)于Java中Comparable 和 Comparator的用法
這篇文章主要介紹了關(guān)于Java中Comparable 和 Comparator的用法,Comparable 和 Comparator 是關(guān)于排序的兩個(gè)接口,用來(lái)實(shí)現(xiàn) Java 集合中的的排序功能,需要的朋友可以參考下2023-04-04聊聊java多線程創(chuàng)建方式及線程安全問(wèn)題
線程被稱為輕量級(jí)進(jìn)程,是程序執(zhí)行的最小單位,它是指在程序執(zhí)行過(guò)程中,能夠執(zhí)行代碼的一個(gè)執(zhí)行單位。接下來(lái)通過(guò)本文給大家介紹java多線程創(chuàng)建方式及線程安全問(wèn)題,感興趣的朋友一起看看吧2022-01-01maven assembly打包生成Java應(yīng)用啟動(dòng)腳本bat和sh的方法
springboot應(yīng)用通過(guò)maven插件appassembler-maven-plugi生成啟動(dòng)腳本bat和sh,這篇文章主要介紹了maven assembly打包生成Java應(yīng)用啟動(dòng)腳本bat和sh,需要的朋友可以參考下2022-11-11spring+springmvc整合mabytis時(shí)mapper注入失敗問(wèn)題解決方法
這篇文章主要介紹了spring+springmvc整合mabytis時(shí)mapper注入失敗問(wèn)題解決方法 ,需要的朋友可以參考下2017-08-08Java輸入學(xué)號(hào)、姓名、年齡并對(duì)其進(jìn)行輸出的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Java輸入學(xué)號(hào)、姓名、年齡并對(duì)其進(jìn)行輸出的實(shí)現(xiàn)方法,在計(jì)算機(jī)編程中,輸出學(xué)號(hào)和姓名是一個(gè)常見的任務(wù),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09