使用Java怎么動態(tài)獲取圖片驗(yàn)證碼?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)建站2013年開創(chuàng)至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)營銷網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元漳平做網(wǎng)站,已為上家服務(wù),為漳平各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
具體如下:
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; /** * 驗(yàn)證碼字符個(gè)數(shù) */ private int codeCount = 5; /** * 驗(yàn)證碼干擾線數(shù) */ private int lineCount = 150; /** * 圖片驗(yàn)證碼類型 */ private ImgCodeType imgCodeType = DEFAULT_TYPE; /** * 驗(yàn)證碼 */ private String code = null; /** * 驗(yàn)證碼圖片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 字符個(gè)數(shù) * @param lineCount 干擾線條數(shù) */ 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);// 每個(gè)字符的寬度 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); // 創(chuàng)建字體 // ImgFontByte imgFont = new ImgFontByte(); // Font font = imgFont.getFont(fontHeight); Font font=new Font("微軟雅黑",Font.PLAIN, fontHeight); g.setFont(font); drawRandomLine(g); // randomCode記錄隨機(jī)產(chǎn)生的驗(yàn)證碼 StringBuffer randomCode = new StringBuffer(); // 隨機(jī)產(chǎn)生codeCount個(gè)字符的驗(yàn)證碼。 for (int i = 0; i < codeCount; i++) { String strRand = getRandomStr(imgCodeType); // 產(chǎn)生隨機(jī)的顏色值,讓輸出的每個(gè)字符的顏色值都將不同。 g.setColor(getRandomColor()); g.drawString(strRand, (i + 1) * x, codeY); // 將產(chǎn)生的四個(gè)隨機(jī)數(shù)組合在一起。 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(); } /** * 單個(gè)漢字 * * @return */ public static String creatHan() { String code = ""; Random random = new Random(); //一個(gè)漢字兩個(gè)字節(jié) 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; } /** * 英文字母加數(shù)字 * * @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(); } /** * 純數(shù)字驗(yàn)證碼 * * * @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 + "]"); } }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
分享文章:使用Java怎么動態(tài)獲取圖片驗(yàn)證碼
當(dāng)前地址:http://www.rwnh.cn/article40/jjsceo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、營銷型網(wǎng)站建設(shè)、品牌網(wǎng)站制作、微信公眾號、商城網(wǎng)站、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)