日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

英文.数字和中文混合的彩色验证码【JSP】

發(fā)布時間:2025/3/17 javascript 97 豆豆
生活随笔 收集整理的這篇文章主要介紹了 英文.数字和中文混合的彩色验证码【JSP】 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、編寫生成英文,數(shù)字和中文混合的彩色驗證碼的Servlet實現(xiàn)類

(1)創(chuàng)建名稱為PictureCheckCode.java的Servlet。

public class PictureCheckCode extends HttpServlet {public PictureCheckCode() {super();}public void destroy() {super.destroy(); }public void init() throws ServletException {super.init();}public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { }
}

(2)編寫隨機生成RGB顏色的方法getRandColor()

// 獲取隨機顏色public Color getRandColor(int s, int e) {Random random = new Random();if (s > 255) s = 255;if (e > 255) e = 255;int r = s + random.nextInt(e - s); //隨機生成RGB顏色中的r值int g = s + random.nextInt(e - s); //隨機生成RGB顏色中的g值int b = s + random.nextInt(e - s); //隨機生成RGB顏色中的b值return new Color(r, g, b);}

(3)在Service()方法中編寫生成彩色驗證碼的代碼

public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "No-cache");response.setDateHeader("Expires", 0);// 指定生成的響應是圖片response.setContentType("image/jpeg");int width = 86;int height = 22;BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics g = image.getGraphics();Graphics2D g2d = (Graphics2D) g;Random random = new Random();Font mFont = new Font("黑體", Font.BOLD, 17);g.setColor(getRandColor(200, 250));g.fillRect(0, 0, width, height);g.setFont(mFont);g.setColor(getRandColor(180, 200));// 畫隨機的線條for (int i = 0; i < 100; i++) {int x = random.nextInt(width - 1);int y = random.nextInt(height - 1);int x1 = random.nextInt(6) + 1;int y1 = random.nextInt(12) + 1;BasicStroke bs = new BasicStroke(2f, BasicStroke.CAP_BUTT,BasicStroke.JOIN_BEVEL);Line2D line = new Line2D.Double(x, y, x + x1, y + y1);g2d.setStroke(bs);g2d.draw(line);}String sRand = "";// 輸出隨機的驗證文字String ctmp = "";int itmp = 0;for (int i = 0; i < 4; i++) {//random = new Random(new java.util.Date().getTime() + i);switch (random.nextInt(4)) {case 1:itmp = random.nextInt(26) + 65; // 生成A~Z的字母ctmp = String.valueOf((char) itmp);break;case 2: // 生成漢字String[] rBase = { "0", "1", "2", "3", "4", "5", "6", "7", "8","9", "a", "b", "c", "d", "e", "f" };// 生成第1位的區(qū)碼int r1 = random.nextInt(3) + 11; //生成11到14之間的隨機數(shù)String str_r1 = rBase[r1];// 生成第2位的區(qū)碼int r2;if (r1 == 13) {r2 = random.nextInt(7); //生成0到7之間的隨機數(shù)} else {r2 = random.nextInt(16); //生成0到16之間的隨機數(shù) }String str_r2 = rBase[r2];// 生成第1位的位碼int r3 = random.nextInt(6) + 10; //生成10到16之間的隨機數(shù)String str_r3 = rBase[r3];// 生成第2位的位碼int r4;if (r3 == 10) {r4 = random.nextInt(15) + 1; //生成1到16之間的隨機數(shù)} else if (r3 == 15) {r4 = random.nextInt(15); //生成0到15之間的隨機數(shù)} else {r4 = random.nextInt(16); //生成0到16之間的隨機數(shù) }String str_r4 = rBase[r4];System.out.println(str_r1 + str_r2 + str_r3 + str_r4);// 將生成機內(nèi)碼轉(zhuǎn)換為漢字byte[] bytes = new byte[2];//將生成的區(qū)碼保存到字節(jié)數(shù)組的第1個元素中String str_r12 = str_r1 + str_r2;int tempLow = Integer.parseInt(str_r12, 16);bytes[0] = (byte) tempLow;//將生成的位碼保存到字節(jié)數(shù)組的第2個元素中String str_r34 = str_r3 + str_r4;int tempHigh = Integer.parseInt(str_r34, 16);bytes[1] = (byte) tempHigh;ctmp = new String(bytes); //根據(jù)字節(jié)數(shù)組生成漢字 // System.out.println("生成漢字:" + ctmp);break;default:itmp = random.nextInt(10) + 48; // 生成0~9的數(shù)字ctmp = String.valueOf((char) itmp);break;}sRand += ctmp;Color color = new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110));g.setColor(color);/** **隨機縮放文字并將文字旋轉(zhuǎn)指定角度* */// 將文字旋轉(zhuǎn)指定角度Graphics2D g2d_word = (Graphics2D) g;AffineTransform trans = new AffineTransform();trans.rotate(random.nextInt(45) * 3.14 / 180, 15 * i + 8, 7);// 縮放文字float scaleSize = random.nextFloat() +0.8f;if (scaleSize > 1f) scaleSize = 1f;trans.scale(scaleSize, scaleSize);g2d_word.setTransform(trans);/** ********************* */g.drawString(ctmp, 15 * i + 18, 14);}// 將生成的驗證碼保存到Session中HttpSession session = request.getSession(true);session.setAttribute("randCheckCode", sRand);g.dispose();ImageIO.write(image, "JPEG", response.getOutputStream());}

二、配置servlet

<servlet><description>This is the description of my J2EE component</description><display-name>This is the display name of my J2EE component</display-name><servlet-name>PictureCheckCode</servlet-name><servlet-class>com.PictureCheckCode</servlet-class></servlet><servlet-mapping><servlet-name>PictureCheckCode</servlet-name><url-pattern>/PictureCheckCode</url-pattern></servlet-mapping>

?

轉(zhuǎn)載于:https://www.cnblogs.com/xiaojiaohuazi/archive/2013/04/02/2995717.html

總結(jié)

以上是生活随笔為你收集整理的英文.数字和中文混合的彩色验证码【JSP】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。