每次登陆系统的时候老是要输入烦人的验证码,那么咱们今天就思考这个问题,为何要有验证码这个功能? 不少伙伴应该都知道:javascript
验证码的种类
css
下面以三种不一样的编程语言,经过代码生成验证码。java
先看下Java
代码是如何生成验证码的。手动建立下面这个类,就能够生成验证码了。代码以下:python
public class GenVerifyCodeUtils {
private static char mapTable[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; public static void main(String[] args) { OutputStream outputStream = new BufferedOutputStream(new ByteArrayOutputStream()); System.out.println(getImageCode(100,80,outputStream )); } public static Map<String, Object> getImageCode(int width, int height, OutputStream os) { Map<String,Object> returnMap = new HashMap<String, Object>(); if (width <= 0) width = 60; if (height <= 0) height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 获取图形上下文 Graphics g = image.getGraphics(); //生成随机类 Random random = new Random(); // 设定背景色 g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); //设定字体 g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); // 随机产生168条干扰线,使图像中的认证码不易被其它程序探测到 g.setColor(getRandColor(160, 200)); for (int i = 0; i < 168; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } //取随机产生的码 String strEnsure = ""; //4表明4位验证码,若是要生成更多位的认证码,则加大数值 for (int i = 0; i < 4; ++i) { strEnsure += mapTable[(int) (mapTable.length * Math.random())]; // 将认证码显示到图像中 g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); // 直接生成 String str = strEnsure.substring(i, i + 1); // 设置随便码在背景图图片上的位置 g.drawString(str, 13 * i + 20, 25); } // 释放图形上下文 g.dispose(); returnMap.put("image",image); returnMap.put("strEnsure",strEnsure); return returnMap; } static Color getRandColor(int fc, int bc) { Random random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } } 复制代码
这里我也用原生Js
写了一个生成验证码的工具,代码以下:web
<form action="#">
<input type="text" id="input1" onblur="inputBlur()"/> <input type="text" onclick="createCode()" readonly="readonly" id="checkCode" class="unchanged" style="width: 80px;background: #660099"/><br /> </form> <script language="javascript" type="text/javascript"> var code; //在全局 定义验证码 var code2; //在全局 定义验证码 function createCode() { code = ""; var checkCode = document.getElementById("checkCode"); function RndNum(n) { var rnd = ""; for (var i = 0; i < n; i++) rnd += Math.floor(Math.random() * 10); return rnd; } var num = RndNum(2); var num2 = RndNum(2); code = num + "+" + num2 + "="; code2 = parseInt(num) + parseInt(num2) if (checkCode) { checkCode.className = "code"; checkCode.value = code; } } function inputBlur(){ var inputCode = document.getElementById("input1").value; if (inputCode.length <= 0) { alert("请输入验证码!"); } else if (inputCode != code2) { alert("验证码输入错误!"); createCode(); } else { alert("^-^ OK"); } } </script> <style type="text/css"> .code { font-family: Arial; font-style: italic; color: Red; border: 0; padding: 2px 3px; letter-spacing: 3px; font-weight: bolder; } .unchanged { border: 0; } </style> 复制代码
效果以下: 编程
代码以下:安全
# -*- coding: utf-8 -*
from PIL import Image, ImageDraw, ImageFont, ImageFilter import random # 随机字母: def rndChar(): return chr(random.randint(65, 90)) # 随机颜色1: def rndColor(): return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)) # 随机颜色2: def rndColor2(): return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)) # 240 x 60: width = 60 * 4 height = 60 image = Image.new('RGB', (width, height), (255, 255, 255)) # 建立Font对象: font = ImageFont.truetype('C:\Windows\Fonts\Arial.ttf', 36) # 建立Draw对象: draw = ImageDraw.Draw(image) # 填充每一个像素: for x in range(width): for y in range(height): draw.point((x, y), fill=rndColor()) # 输出文字: for t in range(4): draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2()) # 模糊: image = image.filter(ImageFilter.BLUR) image.save('code.jpg', 'jpeg') image.show() 复制代码
运行效果以下图: 网络
本篇讲了为何会有验证码这个东东,和市面上如今验证码的种类,简单给你们作了一下科普,最后分别以不一样的编程语言,展现了生成验证码的过程。如今网络安全尤其重要,验证码这个功能虽小,可是不可不作!dom