验证码绘制

package com.oracle.mvc;



import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet("/validateCode")
public class ValidateCodeServelet extends HttpServlet{

    //验证码 GDI+技术  PHP NET JAVA 绘图技术 点 线  面 图片
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //jsp-api jar        //jsp的九大内置对象
        //request response session application out Exception config page pageContext
        //作用域 pageContext request session application
        //servelt-api jar        //Servlet5大对象
        //HttpServletRequest HttpServletResponse HttpSession ServletContext HttpCookie
        //request.getParamter("");  //获取前台的参数
        //response.sendRedirect("");//重定向  不带数据
        //req.getRequestDispatcher().forward();//转发 //带数据
        //req.getSession();
        HttpSession session=req.getSession();
        Random random=new Random();
        int width=100;
        int height=52;

        //1、准备好一张图片  矩形
        BufferedImage validateCodeIMG=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //2、获取图片的画笔
        Graphics graphics = validateCodeIMG.getGraphics();
        graphics.setColor(Color.WHITE);
        //3、绘制矩形
        graphics.fillRect(0,0,width,height);
        //4、准备素材 0-9+A-Z+a-z
        String[] res = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","j","m","n","q","r","t","v","x","y","A","B","C","D","E","F","G","H","I","J","M","N","Q","R","T","X","Y","","","","","","","","","",""};
        Font font=new Font("微软雅黑",Font.BOLD,25);
        graphics.setFont(font);

        //5、开始绘制4个 随机
        String data="";
        for (int i=0;i<4;i++){
            graphics.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
            int index = random.nextInt(res.length);
            String num=res[index];
            data+=num;
            graphics.drawString(num,i*20,30);
        }
        session.setAttribute("validateCode",data);

        //6、绘制干扰线
        for (int i=0;i<10;i++){
            graphics.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
            int ox=random.nextInt(width);
            int oy=random.nextInt(height);
            int ox2=random.nextInt(width);
            int oy2=random.nextInt(height);
            graphics.drawLine(ox,oy,ox2,oy2);
        }

        //7、绘制干扰点
        for (int i=0;i<10;i++){
            graphics.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
            int ox=random.nextInt(width);
            int oy=random.nextInt(height);
            graphics.fillOval(ox,oy,5,5);
        }

        //8、渲染到图片  图片格式 png jpeg
        ImageIO.write(validateCodeIMG,"jpeg",resp.getOutputStream());





    }
}

猜你喜欢

转载自blog.csdn.net/luckily_star/article/details/79631960