java驗證碼生成詳細代碼。本站提示廣大學習愛好者:(java驗證碼生成詳細代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是java驗證碼生成詳細代碼正文
本文實例為年夜家分享了java驗證碼生成的示例代碼,供年夜家參考,詳細內容以下
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; // 界說圖片上顯示驗證碼的個數 private int xx = DEFAULT_CODE_X; // 界說圖片上顯示驗證碼x坐標 private int yy = DEFAULT_CODE_Y; // 界說圖片上顯示驗證碼y坐標 private int fontSize = DEFAULT_FONT_SIZE; // 界說圖片上顯示驗證碼的字體年夜小 private String fontFamily = DEFAULT_FONT_FAMILY; // 界說圖片上顯示驗證碼的個數 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; } /** * 設置裝備擺設坐標 * * @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 { // 將四位數字的驗證碼保留到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(); // 設定配景色 gd.setColor(getRandColor(200, 250)); gd.fillRect(0, 0, width, height); // 設定字體,字體的年夜小應當依據圖片的高度來定。 gd.setFont(new Font(fontFamily, Font.PLAIN, fontSize)); // 創立一個隨機數生成器類 Random random = new Random(); // 隨機發生40條攪擾線,使圖像中的認證碼不容易被其它法式探測到。 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用於保留隨機發生的驗證碼,以便用戶登錄落後行驗證。 StringBuffer randomCode = new StringBuffer(); int red = 0, green = 0, blue = 0; // 隨機發生 length 個驗證碼。 for (int i = 0; i < length; i++) { // 獲得隨機發生的驗證碼數字。 String code = String.valueOf(CODE_SEQUENCE[random.nextInt(36)]); // 發生隨機的色彩重量來結構色彩值,如許輸入的每位數字的色彩值都將分歧。 red = random.nextInt(110); green = random.nextInt(110); blue = random.nextInt(110); // 用隨機發生的色彩將驗證碼繪制到圖象中。 gd.setColor(new Color(red + 20, green + 20, blue + 20)); gd.drawString(code, i * xx + 6, yy); // 將發生的隨機數組合在一路。 randomCode.append(code); } Map<String, Object> retval = new HashMap<>(); retval.put("captchaCode", randomCode.toString()); retval.put("captchaImg", buffImg); return retval; } /** * 給定規模取得隨機色彩 * * @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); } }
以上就是本文的全體內容,願望對年夜家的進修有所贊助。