Verification code picture

From , 3 Years ago, written in Java, viewed 230 times.
URL https://pastebin.vip/view/3e89ebdb
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.image.BufferedImage;
  5. import java.util.Random;
  6.  
  7. import javax.imageio.ImageIO;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import javax.servlet.http.HttpSession;
  11.  
  12. public class RandomValidateCode {
  13.  
  14.     public static final String RANDOMCODEKEY = "RANDOMVALIDATECODEKEY";//放到session中的key
  15.     private Random random = new Random();
  16.     private String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//随机产生的字符串
  17.    
  18.     private int width = 80;//图片宽
  19.     private int height = 26;//图片高
  20.     private int lineSize = 40;//干扰线数量
  21.     private int stringNum = 4;//随机产生字符数量
  22.     /*
  23.      * 获得字体
  24.      */
  25.     private Font getFont(){
  26.         return new Font("Fixedsys",Font.CENTER_BASELINE,18);
  27.     }
  28.     /*
  29.      * 获得颜色
  30.      */
  31.     private Color getRandColor(int fc,int bc){
  32.         if(fc > 255)
  33.             fc = 255;
  34.         if(bc > 255)
  35.             bc = 255;
  36.         int r = fc + random.nextInt(bc-fc-16);
  37.         int g = fc + random.nextInt(bc-fc-14);
  38.         int b = fc + random.nextInt(bc-fc-18);
  39.         return new Color(r,g,b);
  40.     }
  41.     /**
  42.      * 生成随机图片
  43.      */
  44.     public void getRandcode(HttpServletRequest request,
  45.             HttpServletResponse response) {
  46.         HttpSession session = request.getSession();
  47.         //BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
  48.         BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
  49.         Graphics g = image.getGraphics();//产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
  50.         g.fillRect(0, 0, width, height);
  51.         g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));
  52.         g.setColor(getRandColor(110, 133));
  53.         //绘制干扰线
  54.         for(int i=0;i<=lineSize;i++){
  55.             drowLine(g);
  56.         }
  57.         //绘制随机字符
  58.         String randomString = "";
  59.         for(int i=1;i<=stringNum;i++){
  60.             randomString=drowString(g,randomString,i);
  61.         }
  62.         session.removeAttribute(RANDOMCODEKEY);
  63.         session.setAttribute(RANDOMCODEKEY, randomString);
  64.         System.out.println(randomString);
  65.         g.dispose();
  66.         try {
  67.             ImageIO.write(image, "JPEG", response.getOutputStream());//将内存中的图片通过流动形式输出到客户端
  68.         } catch (Exception e) {
  69.             e.printStackTrace();
  70.         }
  71.     }
  72.     /*
  73.      * 绘制字符串
  74.      */
  75.     private String drowString(Graphics g,String randomString,int i){
  76.         g.setFont(getFont());
  77.         g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
  78.         String rand = String.valueOf(getRandomString(random.nextInt(randString.length())));
  79.         randomString +=rand;
  80.         g.translate(random.nextInt(3), random.nextInt(3));
  81.         g.drawString(rand, 13*i, 16);
  82.         return randomString;
  83.     }
  84.     /*
  85.      * 绘制干扰线
  86.      */
  87.     private void drowLine(Graphics g){
  88.         int x = random.nextInt(width);
  89.         int y = random.nextInt(height);
  90.         int xl = random.nextInt(13);
  91.         int yl = random.nextInt(15);
  92.         g.drawLine(x, y, x+xl, y+yl);
  93.     }
  94.     /*
  95.      * 获取随机的字符
  96.      */
  97.     public String getRandomString(int num){
  98.         return String.valueOf(randString.charAt(num));
  99.     }
  100. }

Reply to "Verification code picture"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website