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 {
        //得到随机数
        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.保存到服务器上
    }
  • 相关阅读:
    Linux学习之十九-Linux磁盘管理
    Linux学习之十八-sudo分权管理
    Linux学习之十七-配置Linux简单的脚本文件自启动
    Linux系统救援模式应用:恢复误删的系统文件
    Linux学习之十六-Linux用户管理
    Linux学习之十五-Linux文件特殊权限和附加权限
    Linux学习之十四-Linux文件和目录权限
    Linux学习之十三-vi和vim编辑器及其快捷键
    Linux学习之十二-Linux文件属性
    Linux系统救援模式应用:单用户模式找回密码
  • 原文地址:https://www.cnblogs.com/wxldlxt/p/11310978.html
Copyright © 2011-2022 走看看