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

成都創(chuàng)新互聯(lián)公司技術(shù)團(tuán)隊(duì)十多年來(lái)致力于為客戶提供做網(wǎng)站、網(wǎng)站建設(shè)、高端網(wǎng)站設(shè)計(jì)、全網(wǎng)營(yíng)銷(xiāo)推廣、搜索引擎SEO優(yōu)化等服務(wù)。經(jīng)過(guò)多年發(fā)展,公司擁有經(jīng)驗(yàn)豐富的技術(shù)團(tuán)隊(duì),先后服務(wù)、推廣了上千多家網(wǎng)站,包括各類(lèi)中小企業(yè)、企事單位、高校等機(jī)構(gòu)單位。
Java類(lèi)
public class RandomValidateCode { public static final String RANDOMCODEKEY = "randomcode_key";//放到session中的key private Random random = new Random(); private String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//隨機(jī)產(chǎn)生的字符串 private int width = 80;//圖片寬 private int height = 26;//圖片高 private int lineSize = 40;//干擾線數(shù)量 private int stringNum = 4;//隨機(jī)產(chǎn)生字符數(shù)量 /** * 生成隨機(jī)圖片 */ public void getRandcode(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); //BufferedImage類(lèi)是具有緩沖區(qū)的Image類(lèi),Image類(lèi)是用于描述圖像信息的類(lèi) BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR); //產(chǎn)生Image對(duì)象的Graphics對(duì)象,改對(duì)象可以在圖像上進(jìn)行各種繪制操作 Graphics g = image.getGraphics(); g.fillRect(0, 0, width, height); g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18)); g.setColor(getRandColor(160, 200)); //繪制干擾線 for(int i=0;i<=lineSize;i++){ drowLine(g); } //繪制隨機(jī)字符 String randomString = ""; for(int i=1;i<=stringNum;i++){ randomString=drowString(g,randomString,i); } session.removeAttribute(RANDOMCODEKEY); session.setAttribute(RANDOMCODEKEY, randomString); g.dispose(); try { //將內(nèi)存中的圖片通過(guò)流動(dòng)形式輸出到客戶端 ImageIO.write(image, "JPEG", response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); } } /* * 獲得字體 */ private Font getFont(){ return new Font("Fixedsys",Font.CENTER_BASELINE,18); } /* * 獲得顏色 */ private 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); } /* * 繪制字符串 */ private 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, 16); return randomString; } /* * 繪制干擾線 */ private 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); } /* * 獲取隨機(jī)的字符 */ public String getRandomString(int num){ return String.valueOf(randString.charAt(num)); }}
Controller
@RequestMapping(value="/checkCode") public void checkCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設(shè)置相應(yīng)類(lèi)型,告訴瀏覽器輸出的內(nèi)容為圖片 response.setContentType("image/jpeg"); //設(shè)置響應(yīng)頭信息,告訴瀏覽器不要緩存此內(nèi)容 response.setHeader("pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expire", 0); RandomValidateCode randomValidateCode = new RandomValidateCode(); try { randomValidateCode.getRandcode(request, response);//輸出圖片方法 } catch (Exception e) { e.printStackTrace(); } }
前端jsp頁(yè)面表單,顯示驗(yàn)證碼
<p class="layui-form-item"> <p class="layui-row"> <p class="layui-col-xs7"> <label class="layadmin-user-login-icon layui-icon layui-icon-vercode" for="LAY-user-login-vercode"></label> <input type="text" name="code" id="LAY-user-login-vercode" lay-verify="required" placeholder="圖形驗(yàn)證碼" class="layui-input"> </p> <p class="layui-col-xs5"> <p > <img src="${pageContext.request.contextPath}checkCode" class="layadmin-user-login-codeimg" id="LAY-user-get-vercode" onclick="this.src=this.src+'?'" alt=""> </p> </p> </p> </p>
上述內(nèi)容就是java中怎么實(shí)現(xiàn)生成圖片驗(yàn)證碼功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站題目:java中怎么實(shí)現(xiàn)生成圖片驗(yàn)證碼功能
當(dāng)前網(wǎng)址:http://chinadenli.net/article8/goshop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、服務(wù)器托管、網(wǎng)站排名、用戶體驗(yàn)、網(wǎng)站建設(shè)、ChatGPT
聲明:本網(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)