/*     */ package com.picc.grab.util;
/*     */ 
/*     */ import java.awt.Color;
/*     */ import java.awt.Font;
/*     */ import java.awt.Graphics;
/*     */ import java.awt.Graphics2D;
/*     */ import java.awt.image.BufferedImage;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.util.Random;
/*     */ import javax.imageio.ImageIO;
/*     */ import sun.misc.BASE64Encoder;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class VerificationCodeUtil
/*     */ {
/*  20 */   private static char[] letter = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static BufferedImage getVerCodeImg(char[] content) {
/*  29 */     Random rand = new Random();
/*  30 */     int width = 90;
/*  31 */     int height = 40;
/*  32 */     int degree = 0;
/*  33 */     BufferedImage image = new BufferedImage(width, height, 6);
/*     */     
/*  35 */     Graphics g = image.getGraphics();
/*     */ 
/*     */     
/*  38 */     Color background = Color.WHITE;
/*  39 */     g.setColor(background);
/*  40 */     g.fillRect(0, 0, width, height);
/*     */     
/*  42 */     g.setColor(background);
/*  43 */     g.drawRect(0, 0, width - 1, height - 1);
/*     */     
/*  45 */     int[] xs = getRadonWidths(width, content.length);
/*  46 */     int[] ys = getRadomHeights(height, content.length);
/*  47 */     for (int i = 0; i < content.length; i++) {
/*  48 */       String s = content[i] + "";
/*  49 */       if (content[i] == '!') {
/*  50 */         s = "";
/*     */       }
/*     */       
/*  53 */       int maxDegree = rand.nextInt(2);
/*  54 */       if (maxDegree == 0) {
/*  55 */         maxDegree = 0;
/*     */       } else {
/*  57 */         maxDegree = 305;
/*     */       } 
/*  59 */       degree = rand.nextInt(45) + maxDegree;
/*  60 */       g.setColor(getColor(background, 230));
/*     */ 
/*     */       
/*  63 */       g.setFont(new Font("Atlantic Inline", 0, getIntRandom(23, 25)));
/*  64 */       RotateString(s, xs[i], ys[i], g, degree);
/*     */     } 
/*     */     
/*  67 */     CreateRandomPoint(width, height, 100, g, 100);
/*     */     
/*  69 */     CreateRandomLine(width, height, 8, g, 100);
/*     */     
/*  71 */     g.dispose();
/*  72 */     return image;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static void RotateString(String s, int x, int y, Graphics g, int degree) {
/*  90 */     Graphics2D g2d = (Graphics2D)g.create();
/*     */ 
/*     */     
/*  93 */     g2d.translate(getIntRandom(x, x + 2), getIntRandom(y, y + 2));
/*     */     
/*  95 */     g2d.rotate(degree * Math.PI / 180.0D);
/*     */     
/*  97 */     g2d.drawString(s, 0, 0);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static void CreateRandomPoint(int width, int height, int many, Graphics g, int alpha) {
/* 111 */     Random rand = new Random();
/* 112 */     for (int i = 0; i < many; i++) {
/* 113 */       int x = rand.nextInt(width);
/* 114 */       int y = rand.nextInt(height);
/* 115 */       g.setColor(getColor(alpha));
/* 116 */       g.drawOval(x, y, rand.nextInt(3), rand.nextInt(3));
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static void CreateRandomLine(int width, int height, int minMany, Graphics g, int alpha) {
/* 132 */     for (int i = 0; i < getIntRandom(minMany, minMany + 6); i++) {
/* 133 */       int x1 = getIntRandom(0, (int)(width * 0.6D));
/* 134 */       int y1 = getIntRandom(0, (int)(height * 0.6D));
/* 135 */       int x2 = getIntRandom((int)(width * 0.4D), width);
/* 136 */       int y2 = getIntRandom((int)(height * 0.2D), height);
/* 137 */       g.setColor(getColor(alpha));
/* 138 */       g.drawLine(x1, y1, x2, y2);
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static int getIntRandom(int start, int end) {
/* 146 */     if (end < start) {
/* 147 */       int t = end;
/* 148 */       end = start;
/* 149 */       start = t;
/*     */     } 
/* 151 */     int i = start + (int)(Math.random() * (end - start));
/* 152 */     return i;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static Color getColor(int alpha) {
/* 163 */     int R = (int)(Math.random() * 255.0D);
/* 164 */     int G = (int)(Math.random() * 255.0D);
/* 165 */     int B = (int)(Math.random() * 255.0D);
/* 166 */     return new Color(R, G, B, alpha);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static Color getColor(Color c, int alpha) {
/* 175 */     int R = getIntRandom(-140, 140);
/* 176 */     int G = getIntRandom(-140, 140);
/* 177 */     int B = getIntRandom(-140, 140);
/* 178 */     R = getCloserRandom(c.getRed(), R);
/* 179 */     B = getCloserRandom(c.getBlue(), B);
/* 180 */     G = getCloserRandom(c.getGreen(), G);
/* 181 */     return new Color(R, G, B, alpha);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static int getCloserRandom(int colorValue, int randomValue) {
/* 192 */     if (colorValue + randomValue > 255)
/* 193 */       return getCloserRandom(colorValue, randomValue - getIntRandom(1, randomValue)); 
/* 194 */     if (colorValue + randomValue < 0)
/* 195 */       return getCloserRandom(colorValue, randomValue + getIntRandom(1, randomValue + 20)); 
/* 196 */     if (Math.abs(randomValue) < 60) {
/* 197 */       return getCloserRandom(colorValue, getIntRandom(-255, 255));
/*     */     }
/* 199 */     return colorValue + randomValue;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static char[] getDrawContent() {
/* 208 */     char[] temp = new char[4];
/* 209 */     char[] w = letter;
/* 210 */     for (int i = 0; i < temp.length; i++) {
/* 211 */       temp[i] = w[(int)(Math.random() * 62.0D)];
/*     */     }
/* 213 */     return temp;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static int[] getRadomHeights(int height, int many) {
/* 222 */     int[] temp = new int[many];
/* 223 */     for (int i = 0; i < many; i++) {
/* 224 */       temp[i] = getRadomHeight(height);
/*     */     }
/*     */     
/* 227 */     return temp;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static int[] getRadonWidths(int width, int many) {
/* 236 */     int[] temp = new int[many];
/* 237 */     for (int i = 0; i < many; i++) {
/* 238 */       if (i == 0) {
/*     */         
/* 240 */         temp[i] = getRadonWidth(many, -10, width);
/* 241 */       } else if (i == 4) {
/* 242 */         temp[i] = getRadonWidth(many, temp[i - 1], width - 5);
/*     */       } else {
/*     */         
/* 245 */         temp[i] = getRadonWidth(many, temp[i - 1], width);
/*     */       } 
/*     */     } 
/* 248 */     return temp;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static int getRadomHeight(int fullHeight) {
/* 257 */     return getIntRandom(25, 28);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static int getRadonWidth(int many, int minWidth, int maxWidth) {
/* 268 */     int minJianju = maxWidth / (many + 1);
/* 269 */     int maxJianju = maxWidth / many;
/* 270 */     int temp = maxJianju - minJianju;
/*     */     
/* 272 */     return (int)(Math.random() * temp) + minWidth + minJianju;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static String[] getVerificationCode() {
/* 281 */     char[] con = getDrawContent();
/* 282 */     String content = con[0] + "" + con[1] + "" + con[2] + "" + con[3];
/* 283 */     BufferedImage bi = getVerCodeImg(con);
/* 284 */     String[] varCode = new String[2];
/* 285 */     BASE64Encoder encoder = new BASE64Encoder();
/*     */     try {
/* 287 */       ByteArrayOutputStream baos = new ByteArrayOutputStream();
/* 288 */       ImageIO.write(bi, "png", baos);
/* 289 */       byte[] bytes = baos.toByteArray();
/*     */       
/* 291 */       varCode[0] = content;
/*     */       
/* 293 */       varCode[1] = encoder.encodeBuffer(bytes).trim().replaceAll("\r|\n", "");
/* 294 */       return varCode;
/* 295 */     } catch (IOException e) {
/* 296 */       e.printStackTrace();
/*     */       
/* 298 */       return null;
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static void main(String[] args) {
/* 316 */     System.out.println(getVerificationCode());
/*     */   }
/*     */ }


/* Location:              C:\Users\Administrator\Desktop\ROOT\WEB-INF\classes\!\com\picc\gra\\util\VerificationCodeUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */