From c635d78b479510ebe2556a420948effcd30a0731 Mon Sep 17 00:00:00 2001 From: skyouc Date: 星期六, 21 十二月 2024 18:40:43 +0800 Subject: [PATCH] 新建德森项目分支 --- zy-asrs-common/src/main/java/com/zy/asrs/common/utils/RandomValidateCodeUtil.java | 244 ++++++++++++++++++++++++------------------------ 1 files changed, 122 insertions(+), 122 deletions(-) diff --git a/zy-asrs-common/src/main/java/com/zy/asrs/common/utils/RandomValidateCodeUtil.java b/zy-asrs-common/src/main/java/com/zy/asrs/common/utils/RandomValidateCodeUtil.java index 7b3aeb4..86aea7d 100644 --- a/zy-asrs-common/src/main/java/com/zy/asrs/common/utils/RandomValidateCodeUtil.java +++ b/zy-asrs-common/src/main/java/com/zy/asrs/common/utils/RandomValidateCodeUtil.java @@ -1,122 +1,122 @@ -package com.zy.asrs.common.utils; - -import javax.imageio.ImageIO; -import javax.servlet.http.HttpServletResponse; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.util.Map; -import java.util.Random; -import java.util.concurrent.ConcurrentHashMap; - -public class RandomValidateCodeUtil { - - public static final Map<String, String> code = new ConcurrentHashMap<>(); - - private static String randString = "0123456789ABCDEFGHJKLMNPRSTUVWXYZ";//闅忔満浜х敓鏁板瓧涓庡瓧姣嶇粍鍚堢殑瀛楃涓� - private static int width = 60;// 鍥剧墖瀹� - private static int height = 27;// 鍥剧墖楂� - private static int lineSize = 40;// 骞叉壈绾挎暟閲� - private static int stringNum = 4;// 闅忔満浜х敓瀛楃鏁伴噺 - - private static Random random = new Random(); - - /** - * 鑾峰緱瀛椾綋 - */ - private static Font getFont() { - return new Font("Fixedsys", Font.CENTER_BASELINE, 18); - } - - /** - * 鑾峰緱棰滆壊 - */ - private static Color getRandColor(int fc, int bc) { - if (fc > 255) - fc = 255; - if (bc > 255) - bc = 255; - int r = fc + random.nextInt(bc - fc - 16); - int g = fc + random.nextInt(bc - fc - 14); - int b = fc + random.nextInt(bc - fc - 18); - return new Color(r, g, b); - } - - public static void init() { - BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); - Graphics g = image.getGraphics();// 浜х敓Image瀵硅薄鐨凣raphics瀵硅薄,鏀瑰璞″彲浠ュ湪鍥惧儚涓婅繘琛屽悇绉嶇粯鍒舵搷浣� - g.fillRect(0, 0, width, height);//鍥剧墖澶у皬 - g.setFont(new Font("Times New Roman", Font.PLAIN, 18));//瀛椾綋澶у皬 - g.setColor(getRandColor(110, 133));//瀛椾綋棰滆壊 - // 缁樺埗骞叉壈绾� - for (int i = 0; i <= lineSize; i++) { - drowLine(g); - } - // 缁樺埗闅忔満瀛楃 - String randomString = ""; - for (int i = 1; i <= stringNum; i++) { - randomString = drowString(g, randomString, i); - } - g.dispose(); - } - - /** - * 鐢熸垚闅忔満鍥剧墖 - */ - public static void getRandcode(String sd, HttpServletResponse response) { - BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); - Graphics g = image.getGraphics();// 浜х敓Image瀵硅薄鐨凣raphics瀵硅薄,鏀瑰璞″彲浠ュ湪鍥惧儚涓婅繘琛屽悇绉嶇粯鍒舵搷浣� - g.fillRect(0, 0, width, height);//鍥剧墖澶у皬 - g.setFont(new Font("Times New Roman", Font.PLAIN, 18));//瀛椾綋澶у皬 - g.setColor(getRandColor(110, 133));//瀛椾綋棰滆壊 - // 缁樺埗骞叉壈绾� - for (int i = 0; i <= lineSize; i++) { - drowLine(g); - } - // 缁樺埗闅忔満瀛楃 - String randomString = ""; - for (int i = 1; i <= stringNum; i++) { - randomString = drowString(g, randomString, i); - } - code.put(sd, randomString); - g.dispose(); - try { - ImageIO.write(image, "JPEG", response.getOutputStream()); - } catch (Exception e) { - e.printStackTrace(); - } - } - - /** - * 缁樺埗瀛楃涓� - */ - private static String drowString(Graphics g, String randomString, int i) { - g.setFont(getFont()); - g.setColor(new Color(random.nextInt(101), random.nextInt(111), random - .nextInt(121))); - String rand = String.valueOf(getRandomString(random.nextInt(randString - .length()))); - randomString += rand; - g.translate(random.nextInt(3), random.nextInt(3)); - g.drawString(rand, 13 * (i-1)+2, 16); - return randomString; - } - - /** - * 缁樺埗骞叉壈绾� - */ - private static void drowLine(Graphics g) { - int x = random.nextInt(width); - int y = random.nextInt(height); - int xl = random.nextInt(13); - int yl = random.nextInt(15); - g.drawLine(x, y, x + xl, y + yl); - } - - /** - * 鑾峰彇闅忔満鐨勫瓧绗� - */ - public static String getRandomString(int num) { - return String.valueOf(randString.charAt(num)); - } - -} +package com.zy.asrs.common.utils; + +import javax.imageio.ImageIO; +import javax.servlet.http.HttpServletResponse; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.ConcurrentHashMap; + +public class RandomValidateCodeUtil { + + public static final Map<String, String> code = new ConcurrentHashMap<>(); + + private static String randString = "0123456789ABCDEFGHJKLMNPRSTUVWXYZ";//闅忔満浜х敓鏁板瓧涓庡瓧姣嶇粍鍚堢殑瀛楃涓� + private static int width = 60;// 鍥剧墖瀹� + private static int height = 27;// 鍥剧墖楂� + private static int lineSize = 40;// 骞叉壈绾挎暟閲� + private static int stringNum = 4;// 闅忔満浜х敓瀛楃鏁伴噺 + + private static Random random = new Random(); + + /** + * 鑾峰緱瀛椾綋 + */ + private static Font getFont() { + return new Font("Fixedsys", Font.CENTER_BASELINE, 18); + } + + /** + * 鑾峰緱棰滆壊 + */ + private static Color getRandColor(int fc, int bc) { + if (fc > 255) + fc = 255; + if (bc > 255) + bc = 255; + int r = fc + random.nextInt(bc - fc - 16); + int g = fc + random.nextInt(bc - fc - 14); + int b = fc + random.nextInt(bc - fc - 18); + return new Color(r, g, b); + } + + public static void init() { + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); + Graphics g = image.getGraphics();// 浜х敓Image瀵硅薄鐨凣raphics瀵硅薄,鏀瑰璞″彲浠ュ湪鍥惧儚涓婅繘琛屽悇绉嶇粯鍒舵搷浣� + g.fillRect(0, 0, width, height);//鍥剧墖澶у皬 + g.setFont(new Font("Times New Roman", Font.PLAIN, 18));//瀛椾綋澶у皬 + g.setColor(getRandColor(110, 133));//瀛椾綋棰滆壊 + // 缁樺埗骞叉壈绾� + for (int i = 0; i <= lineSize; i++) { + drowLine(g); + } + // 缁樺埗闅忔満瀛楃 + String randomString = ""; + for (int i = 1; i <= stringNum; i++) { + randomString = drowString(g, randomString, i); + } + g.dispose(); + } + + /** + * 鐢熸垚闅忔満鍥剧墖 + */ + public static void getRandcode(String sd, HttpServletResponse response) { + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); + Graphics g = image.getGraphics();// 浜х敓Image瀵硅薄鐨凣raphics瀵硅薄,鏀瑰璞″彲浠ュ湪鍥惧儚涓婅繘琛屽悇绉嶇粯鍒舵搷浣� + g.fillRect(0, 0, width, height);//鍥剧墖澶у皬 + g.setFont(new Font("Times New Roman", Font.PLAIN, 18));//瀛椾綋澶у皬 + g.setColor(getRandColor(110, 133));//瀛椾綋棰滆壊 + // 缁樺埗骞叉壈绾� + for (int i = 0; i <= lineSize; i++) { + drowLine(g); + } + // 缁樺埗闅忔満瀛楃 + String randomString = ""; + for (int i = 1; i <= stringNum; i++) { + randomString = drowString(g, randomString, i); + } + code.put(sd, randomString); + g.dispose(); + try { + ImageIO.write(image, "JPEG", response.getOutputStream()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 缁樺埗瀛楃涓� + */ + private static String drowString(Graphics g, String randomString, int i) { + g.setFont(getFont()); + g.setColor(new Color(random.nextInt(101), random.nextInt(111), random + .nextInt(121))); + String rand = String.valueOf(getRandomString(random.nextInt(randString + .length()))); + randomString += rand; + g.translate(random.nextInt(3), random.nextInt(3)); + g.drawString(rand, 13 * (i-1)+2, 16); + return randomString; + } + + /** + * 缁樺埗骞叉壈绾� + */ + private static void drowLine(Graphics g) { + int x = random.nextInt(width); + int y = random.nextInt(height); + int xl = random.nextInt(13); + int yl = random.nextInt(15); + g.drawLine(x, y, x + xl, y + yl); + } + + /** + * 鑾峰彇闅忔満鐨勫瓧绗� + */ + public static String getRandomString(int num) { + return String.valueOf(randString.charAt(num)); + } + +} -- Gitblit v1.9.1