您好,登錄后才能下訂單哦!
使用Java怎么動態獲取圖片驗證碼?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
具體如下:
import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.Random; public class ImgAuthCode { /** * 圖片的寬度 */ private int width = 330; /** * 圖片的高度 */ private int height = 40; /** * 驗證碼字符個數 */ private int codeCount = 5; /** * 驗證碼干擾線數 */ private int lineCount = 150; /** * 圖片驗證碼類型 */ private ImgCodeType imgCodeType = DEFAULT_TYPE; /** * 驗證碼 */ private String code = null; /** * 驗證碼圖片Buffer */ private BufferedImage buffImg = null; private static final ImgCodeType DEFAULT_TYPE = ImgCodeType.ENANDNUMBER; private static char[] codeSequence = {'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'}; public ImgAuthCode() { this.createCode(); } /** * @param width 圖片寬 * @param height 圖片高 */ public ImgAuthCode(int width, int height) { this.width = width; this.height = height; this.createCode(); } /** * @param width 圖片寬 * @param height 圖片高 * @param codeCount 字符個數 * @param lineCount 干擾線條數 */ public ImgAuthCode(int width, int height, int codeCount, int lineCount,ImgCodeType imgCodeType) { this.width = width; this.height = height; this.codeCount = codeCount; this.lineCount = lineCount; this.imgCodeType = imgCodeType; this.createCode(); } public void createCode() { int x = 0, fontHeight = 0, codeY = 0; x = width / (codeCount + 2);// 每個字符的寬度 fontHeight = height - 2;// 字體的高度 codeY = height - 4; // 圖像buffer buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = buffImg.createGraphics(); // 將圖像填充為白色 g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); // 創建字體 // ImgFontByte imgFont = new ImgFontByte(); // Font font = imgFont.getFont(fontHeight); Font font=new Font("微軟雅黑",Font.PLAIN, fontHeight); g.setFont(font); drawRandomLine(g); // randomCode記錄隨機產生的驗證碼 StringBuffer randomCode = new StringBuffer(); // 隨機產生codeCount個字符的驗證碼。 for (int i = 0; i < codeCount; i++) { String strRand = getRandomStr(imgCodeType); // 產生隨機的顏色值,讓輸出的每個字符的顏色值都將不同。 g.setColor(getRandomColor()); g.drawString(strRand, (i + 1) * x, codeY); // 將產生的四個隨機數組合在一起。 randomCode.append(strRand); } this.code = randomCode.toString(); } private void drawRandomLine(Graphics g){ //干擾線 for (int i = 0; i < lineCount; i++) { int xs = new Random().nextInt(width); int ys = new Random().nextInt(height); int xe = xs + new Random().nextInt(width / 8); int ye = ys + new Random().nextInt(height / 8); g.setColor(getRandomColor()); g.drawLine(xs, ys, xe, ye); } } public Color getRandomColor(){ int red = 0, green = 0, blue = 0; Random random = new Random(); red = random.nextInt(255); green = random.nextInt(255); blue = random.nextInt(255); return new Color(red,green,blue); } public String getRandomStr(ImgCodeType type) { switch (type) { case ENANDNUMBER: return creatENAndNumberCode(); case HAN: return creatHan(); case EN: return creatENCode(); case NUMBER: return creatNumberCode(); default: return creatENAndNumberCode(); } } public void write(String path) throws IOException { OutputStream sos = new FileOutputStream(path); this.write(sos); } public void write(OutputStream sos) throws IOException { ImageIO.write(buffImg, "png", sos); sos.close(); } /** * 單個漢字 * * @return */ public static String creatHan() { String code = ""; Random random = new Random(); //一個漢字兩個字節 byte[] bytes = new byte[2]; bytes[0] = Integer.valueOf(176 + Math.abs(random.nextInt(39))).byteValue(); bytes[1] = Integer.valueOf(161 + Math.abs(random.nextInt(93))).byteValue(); try { code = new String(bytes, "GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return code; } /** * 英文字母加數字 * * @return */ public static String creatENAndNumberCode() { Random random = new Random(); return String.valueOf(codeSequence[random.nextInt(codeSequence.length)]); } /** * 英文字母 * * * @return */ public static String creatENCode() { StringBuffer code = new StringBuffer(); Random random = new Random(); int s = random.nextInt(25) + 65; code.append((char) s); return code.toString(); } /** * 純數字驗證碼 * * * @return */ public static String creatNumberCode() { StringBuffer code = new StringBuffer(); Random random = new Random(); return code.append(random.nextInt(9) + "").toString(); } public BufferedImage getBuffImg() { return buffImg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public static void main(String[] args) { String randomChineseChar = creatHan(); String randomEnChar = creatENCode(); System.out.println("args = [" + randomChineseChar + "]"); System.out.println("args = [" + randomEnChar + "]"); } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。