本篇文章為大家展示了Javaweb中怎么實(shí)現(xiàn)動(dòng)態(tài)圖片驗(yàn)證碼功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)建站成立于2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目做網(wǎng)站、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元撫寧做網(wǎng)站,已為上家服務(wù),為撫寧各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
驗(yàn)證碼
防止惡意表單注冊(cè)
生成驗(yàn)證碼圖片
定義寬高
int width = 100;int height = 50;
使用BufferedImage再內(nèi)存中生成圖片
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
繪制背景和邊框
Graphics g = image.getGraphics();g.setColor(Color.WHITE);g.fillRect(0, 0, width, height);g.setColor(Color.BLACK);g.drawRect(0, 0, width - 1, height - 1);
創(chuàng)建隨機(jī)字符集和隨機(jī)數(shù)對(duì)象
//字符集String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgjijklmnopqrstuvwxyz";//隨機(jī)數(shù)Random ran = new Random();
創(chuàng)建隨機(jī)顏色生成方法
private Color getRandomColor(Random random) { //獲取隨機(jī)顏色 int colorIndex = random.nextInt(3); switch (colorIndex) { case 0: return Color.BLUE; case 1: return Color.GREEN; case 2: return Color.RED; case 3: return Color.YELLOW; default: return Color.MAGENTA; }}
繪制驗(yàn)證碼字符
//繪制驗(yàn)證碼for (int i = 0; i < 4; i++) { //獲取隨機(jī)字符 int index = ran.nextInt(str.length()); char ch = str.charAt(index); //獲取隨機(jī)色 Color randomColor = getRandomColor(ran); g.setColor(randomColor); //設(shè)置字體 Font font = new Font("宋體", Font.BOLD, height / 2); g.setFont(font); //寫(xiě)入驗(yàn)證碼 g.drawString(ch + "", (i == 0) ? width / 4 * i + 2 : width / 4 * i, height - height / 4);}
繪制干擾線
//干擾線for (int i = 0; i < 10; i++) { int x1 = ran.nextInt(width); int x2 = ran.nextInt(width); int y1 = ran.nextInt(height); int y2 = ran.nextInt(height); Color randomColor = getRandomColor(ran); g.setColor(randomColor); g.drawLine(x1, x2, y1, y2);}
使用ImageIO輸出圖片
ImageIO.write(image, "jpg", resp.getOutputStream());
成果圖
實(shí)現(xiàn)刷新效果
新建html頁(yè)面使用img標(biāo)簽實(shí)現(xiàn)圖片展示
<img id="identcode" src="identcode"><a id="refesh" href="">看不清,換一張</a>
使用js實(shí)現(xiàn)刷新效果
//點(diǎn)擊圖片時(shí)var img = document.getElementById("identcode");img.onclick = function (){ refesh();}//點(diǎn)擊連接時(shí)var a = document.getElementById("refesh");a.onclick = function (){ refesh(); //返回false防止a標(biāo)簽?zāi)J(rèn)href行為 return false;}function refesh() { /** * 由于路徑相同時(shí)瀏覽器會(huì)自動(dòng)調(diào)用緩存中的圖片 * 所以在連接后加時(shí)間戳解決此問(wèn)題 */ var date = new Date().getTime(); img.src = "identcode?" + date;}
上述內(nèi)容就是Javaweb中怎么實(shí)現(xiàn)動(dòng)態(tài)圖片驗(yàn)證碼功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享題目:Javaweb中怎么實(shí)現(xiàn)動(dòng)態(tài)圖片驗(yàn)證碼功能
當(dāng)前鏈接:http://www.chinadenli.net/article0/geigoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、自適應(yīng)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、微信公眾號(hào)、網(wǎng)站維護(hù)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)