关于验证码的一制作
验证码的制作
1、指定用于生成验证码的长度和来源
public class VerifyCodeUtils{ //使用到Algerian字体,系统里没有的话需要安装字体,字体只显示大写,去掉了1,0,i,o几个容易混淆的字符 public static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"; private static Random random = new Random(); /** * 使用系统默认字符源生成验证码 * @param verifySize 验证码长度 * @return */ public static String generateVerifyCode(int verifySize){ return generateVerifyCode(verifySize, VERIFY_CODES); } /** * 使用指定源生成验证码 * @param verifySize 验证码长度 * @param sources 验证码字符源 * @return */ public static String generateVerifyCode(int verifySize, String sources){ if(sources == null || sources.length() == 0){ sources = VERIFY_CODES; } int codesLen = sources.length(); //获取字符源的长度 Random rand = new Random(System.currentTimeMillis());// StringBuilder verifyCode = new StringBuilder(verifySize); for(int i = 0; i < verifySize; i++){ verifyCode.append(sources.charAt(rand.nextInt(codesLen-1))); } return verifyCode.toString(); }
2、生成指定验证码图片
1 /** 2 * 生成指定验证码图像文件 3 * @param w:验证码图片的宽 4 * @param h:验证码图片的高 5 * @param outputFile:图片保存的位置 6 * @param code:生成的验证码 7 * @throws IOException 8 */ 9 public static void outputImage(int w, int h, File outputFile, String code) throws IOException{ 10 /**如果图片保存的路径文件不存在,这结束*/ 11 if(outputFile == null){ 12 return; 13 } 14 /**获取该图片的父路径*/ 15 File dir = outputFile.getParentFile(); 16 if(!dir.exists()){ 17 dir.mkdirs(); 18 } 19 try{ 20 /**判断该文件是否存在*/ 21 outputFile.createNewFile(); 22 /**创建输出流对象*/ 23 FileOutputStream fos = new FileOutputStream(outputFile); 24 /**将图片输出文件的指定位置*/ 25 outputImage(w, h, fos, code); 26 fos.close(); 27 } catch(IOException e){ 28 throw e; 29 } 30 }
3、开始画验证码
1 /** 2 * 输出指定验证码图片流 3 * @param w:图片的宽 4 * @param h:图片的高 5 * @param os:图片的输出流 6 * @param code:随机生成的字符 7 * @throws IOException 8 */ 9 public static void outputImage(int w, int h, OutputStream os, String code) throws IOException{ 10 /**获取验证码的长度*/ 11 int verifySize = code.length(); 12 /**构造一个BufferedImage义的图像类型的一个BufferedImage,imageType:创建图像的类型*/ 13 BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 14 Random rand = new Random(); 15 /**创建一个 Graphics2D ,可以用来绘制这个 BufferedImage*/ 16 Graphics2D g2 = image.createGraphics(); 17 /**为渲染算法设置单个首选项的值*/ 18 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); 19 Color[] colors = new Color[5]; 20 /**颜色的数组*/ 21 Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN, 22 Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, 23 Color.PINK, Color.YELLOW }; 24 float[] fractions = new float[colors.length]; 25 for(int i = 0; i < colors.length; i++){ 26 colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)]; 27 fractions[i] = rand.nextFloat(); 28 } 29 Arrays.sort(fractions); 30 31 g2.setColor(Color.GRAY);// 设置边框色 32 g2.fillRect(0, 0, w, h); 33 Color c = getRandColor(200, 250); 34 g2.setColor(c);// 设置背景色 35 g2.fillRect(0, 2, w, h-4); 36 37 //绘制干扰线 38 Random random = new Random(); 39 g2.setColor(getRandColor(160, 200));// 设置线条的颜色 40 for (int i = 0; i < 20; i++) { 41 int x = random.nextInt(w - 1); 42 int y = random.nextInt(h - 1); 43 int xl = random.nextInt(6) + 1; 44 int yl = random.nextInt(12) + 1; 45 g2.drawLine(x, y, x + xl + 40, y + yl + 20); 46 } 47 48 // 添加噪点 49 float yawpRate = 0.05f;// 噪声率 50 int area = (int) (yawpRate * w * h); 51 for (int i = 0; i < area; i++) { 52 int x = random.nextInt(w); 53 int y = random.nextInt(h); 54 int rgb = getRandomIntColor(); 55 image.setRGB(x, y, rgb); 56 } 57 58 shear(g2, w, h, c);// 使图片扭曲 59 60 g2.setColor(getRandColor(100, 160)); 61 int fontSize = h-4; 62 Font font = new Font("Algerian", Font.ITALIC, fontSize); 63 g2.setFont(font); 64 char[] chars = code.toCharArray(); 65 for(int i = 0; i < verifySize; i++){ 66 AffineTransform affine = new AffineTransform(); 67 affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), (w / verifySize) * i + fontSize/2, h/2); 68 g2.setTransform(affine); 69 g2.drawChars(chars, i, 1, ((w-10) / verifySize) * i + 5, h/2 + fontSize/2 - 10); 70 } 71 72 g2.dispose(); 73 ImageIO.write(image, "jpg", os); 74 }
1 private static Color getRandColor(int fc, int bc) { 2 if (fc > 255) 3 fc = 255; 4 if (bc > 255) 5 bc = 255; 6 int r = fc + random.nextInt(bc - fc); 7 int g = fc + random.nextInt(bc - fc); 8 int b = fc + random.nextInt(bc - fc); 9 return new Color(r, g, b); 10 } 11 12 private static int getRandomIntColor() { 13 int[] rgb = getRandomRgb(); 14 int color = 0; 15 for (int c : rgb) { 16 color = color << 8; 17 color = color | c; 18 } 19 return color; 20 } 21 22 private static int[] getRandomRgb() { 23 int[] rgb = new int[3]; 24 for (int i = 0; i < 3; i++) { 25 rgb[i] = random.nextInt(255); 26 } 27 return rgb; 28 } 29 30 private static void shear(Graphics g, int w1, int h1, Color color) { 31 shearX(g, w1, h1, color); 32 shearY(g, w1, h1, color); 33 } 34 35 36 private static void shearX(Graphics g, int w1, int h1, Color color) { 37 38 int period = random.nextInt(2); 39 40 boolean borderGap = true; 41 int frames = 1; 42 int phase = random.nextInt(2); 43 44 for (int i = 0; i < h1; i++) { 45 double d = (double) (period >> 1) 46 * Math.sin((double) i / (double) period 47 + (6.2831853071795862D * (double) phase) 48 / (double) frames); 49 g.copyArea(0, i, w1, 1, (int) d, 0); 50 if (borderGap) { 51 g.setColor(color); 52 g.drawLine((int) d, i, 0, i); 53 g.drawLine((int) d + w1, i, w1, i); 54 } 55 } 56 57 } 58 59 60 private static void shearY(Graphics g, int w1, int h1, Color color) { 61 62 int period = random.nextInt(40) + 10; // 50; 63 64 boolean borderGap = true; 65 int frames = 20; 66 int phase = 7; 67 for (int i = 0; i < w1; i++) { 68 double d = (double) (period >> 1) 69 * Math.sin((double) i / (double) period 70 + (6.2831853071795862D * (double) phase) 71 / (double) frames); 72 g.copyArea(i, 0, 1, h1, 0, (int) d); 73 if (borderGap) { 74 g.setColor(color); 75 g.drawLine(i, (int) d, i, 0); 76 g.drawLine(i, (int) d + h1, i, h1); 77 } 78 79 } 80 81 }