zoukankan      html  css  js  c++  java
  • java验证码

    package utils;
     
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.Random;
    import javax.imageio.ImageIO;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    public class CodeUtils {
     //10.得到随机数
     private static char[] chars = "abdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY3456789".toCharArray();
     private static Random random = new Random();
     private static String getCode(int length){
      StringBuffer sb = new StringBuffer();
      for(int i=0;i<length;i++){
       sb.append(chars[random.nextInt(chars.length)]);
      }
      return sb.toString();
     }
     
     public static void createCode(HttpServletRequest req,HttpServletResponse resp){
      try {
       //1.创建画布
       BufferedImage bufferedImage = new BufferedImage(120, 34, BufferedImage.TYPE_INT_RGB);
       //2.创建画笔
       Graphics2D graphics2d = bufferedImage.createGraphics();
       //6.设置背景颜色  , 先设置画笔颜色  ,在把画布涂满
       graphics2d.setColor(new Color(249, 250, 108));
       //7.涂满画布
       graphics2d.fillRect(0, 0, 120, 34);
       //8.更新画笔颜色
       graphics2d.setColor(new Color(62, 128, 27));
       //9.设置字体
       graphics2d.setFont(new Font("Dope Crisis", Font.PLAIN, 40));
       String code = getCode(4);
       //12.保存到sesssion中
       HttpSession session=req.getSession();
       session.setAttribute("code", code);
       session.setMaxInactiveInterval(1000);
       //3.写字
       graphics2d.drawString(code, 25,30);
       //4.收笔
       graphics2d.dispose();
       //5.保存到服务器上
       ImageIO.write(bufferedImage, "jpg", resp.getOutputStream());
      } catch (IOException e) {
       e.printStackTrace();
      }
     }
     
     
     
     
     
     
     
     
     
     
     
     
     
     public static void main(String[] args) throws Exception {
      //1.创建画布
      BufferedImage bufferedImage = new BufferedImage(120, 34, BufferedImage.TYPE_INT_RGB);
      //2.创建画笔
      Graphics2D graphics2d = bufferedImage.createGraphics();
      //6.设置背景颜色  , 先设置画笔颜色  ,在把画布涂满
      graphics2d.setColor(new Color(20, 123, 228));
      //7.涂满画布
      graphics2d.fillRect(0, 0, 120, 34);
      //8.更新画笔颜色
      graphics2d.setColor(new Color(246, 129, 50));
      //9.设置字体
      graphics2d.setFont(new Font("Dope Crisis", Font.PLAIN, 40));
      String num = getCode(4);
      //3.写字
      graphics2d.drawString(num, 25,30);
      //4.收笔
      graphics2d.dispose();
      //5.保存到硬盘上
      ImageIO.write(bufferedImage, "jpg", new File("c:\abc.jpg"));
     }
     //11.保存到服务器上
    }
  • 相关阅读:
    POJ2155 Matrix 【二维线段树】
    BZOJ4785 [Zjoi2017]树状数组 【二维线段树 + 标记永久化】
    B1027 打印沙漏
    Tomcat无法成功启动——双击startup.bat闪退
    MySQL在cmd命令行查看端口号
    1009 说反话(类似回文字符串)
    除基取余法,
    日期差值
    怎么把VS里的scanf_s换成scanf
    联想小新潮怎么修改fn热键以及怎么进入bios状态
  • 原文地址:https://www.cnblogs.com/lbbk/p/11275285.html
Copyright © 2011-2022 走看看