記錄下本身的驗證碼小工具,留待之後複用。java
字符庫代碼數組
public class SecurityCode { public enum SecurityCodeLevel { Simple, Medium, Hard }; public static String getSecurityCode() { return getSecurityCode(4, SecurityCodeLevel.Medium, false); } public static String getSecurityCode(int length, SecurityCodeLevel level, boolean isCanRepeat) { // 隨機抽取len個字符 int len = length; // 字符集合(除去易混淆的數字0、數字一、字母l、字母o、字母O) char[] codes = { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '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' }; // 根據不一樣的難度截取字符數組 if (level == SecurityCodeLevel.Simple) { codes = Arrays.copyOfRange(codes, 0, 9); } else if (level == SecurityCodeLevel.Medium) { codes = Arrays.copyOfRange(codes, 0, 33); } // 字符集合長度 int n = codes.length; // 拋出運行時異常 if (len > n && isCanRepeat == false) { throw new RuntimeException( String.format( "驗證碼長度大於字符庫長度,請改小驗證碼位數參數 length", len, level, isCanRepeat, n)); } // 存放抽取出來的字符 char[] result = new char[len]; // 判斷可否出現重複的字符 if (isCanRepeat) { for (int i = 0; i < result.length; i++) { // 索引 0 and n-1 int r = (int) (Math.random() * n); // 將result中的第i個元素設置爲codes[r]存放的數值 result[i] = codes[r]; } } else { for (int i = 0; i < result.length; i++) { // 索引 0 and n-1 int r = (int) (Math.random() * n); // 將result中的第i個元素設置爲codes[r]存放的數值 result[i] = codes[r]; // 必須確保不會再次抽取到那個字符,由於全部抽取的字符必須不相同。 // 所以,這裏用數組中的最後一個字符改寫codes[r],並將n減1 codes[r] = codes[n - 1]; n--; } } return String.valueOf(result); } }
畫圖片代碼dom
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Random; import com.sun.image.codec.jpeg.ImageFormatException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class SecurityImage { public static BufferedImage createImage(String securityCode) { // 驗證碼長度 int codeLength = securityCode.length(); // 字體大小 int fSize = 15; int fWidth = fSize + 1; // 圖片寬度 int width = codeLength * fWidth + 6; // 圖片高度 int height = fSize * 2 + 1; // 圖片 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.createGraphics(); // 設置背景色 g.setColor(Color.WHITE); // 填充背景 g.fillRect(0, 0, width, height); // 設置邊框顏色 g.setColor(Color.LIGHT_GRAY); // 邊框字體樣式 g.setFont(new Font("Arial", Font.BOLD, height - 2)); // 繪製邊框 g.drawRect(0, 0, width - 1, height - 1); // 繪製噪點 Random rand = new Random(); for (int i = 0; i < codeLength * 6; i++) { int xs = rand.nextInt(width); int ys = rand.nextInt(height); // 繪製1*1大小的矩形 randColor(g, rand); g.drawRect(xs, ys, 1, 1); } // 干擾線 for (int i = 0; i < 10; i++) { int xs = rand.nextInt(width); int ys = rand.nextInt(height); int xe = xs + 10; int ye = ys + 10; randColor(g, rand); g.drawLine(xs, ys, xe, ye); } // 繪製驗證碼 int codeY = height - 10; // 設置字體顏色和樣式 g.setFont(new Font("Georgia", Font.BOLD, fSize)); for (int i = 0; i < codeLength; i++) { randColor(g, rand); g.drawString(String.valueOf(securityCode.charAt(i)), i * 16 + 5, codeY); } // 關閉資源 g.dispose(); return image; } /** * 隨機色 * * @param g * @param rand */ private static void randColor(Graphics g, Random rand) { int red = 0, green = 0, blue = 0; red = rand.nextInt(255); green = rand.nextInt(255); blue = rand.nextInt(255); g.setColor(new Color(red, green, blue)); } public static ByteArrayInputStream getImageAsInputStream(String securityCode) { BufferedImage image = createImage(securityCode); return convertImageToStream(image); } private static ByteArrayInputStream convertImageToStream(BufferedImage image) { ByteArrayInputStream inputStream = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(bos); try { jpeg.encode(image); byte[] bts = bos.toByteArray(); inputStream = new ByteArrayInputStream(bts); } catch (ImageFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return inputStream; } }