验证码

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletTest extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("哈哈");
         int width=57;//图像宽度
            int height=21;//图像高度
            //定义输出格式
           response.setContentType("image/jpeg");
           ServletOutputStream out=response.getOutputStream();
           //准备缓冲图像,不支持表单
            BufferedImage bimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
            Random r=new Random();
            //获取图形上下文环境
           Graphics gc= bimg.getGraphics();
           //设定背景色并进行填充
            gc.setColor(getRandColor(200,250));
            gc.fillRect(0,0,width,height);
            //设置图形上下文环境文字
            gc.setFont(new Font("Times New Roman",Font.PLAIN,18));
            //随机产生200条干扰线条,使图像中的认证码不易被其他分析程序探测到
            gc.setColor(getRandColor(160,200));
            for (int i=0;i<200;i++){
                int x1 = r.nextInt(width);
                int y1 = r.nextInt(height);
                int x2 = r.nextInt(15);
                int y2 = r.nextInt(15);
                gc.drawLine(x1,y1,x2,y2);
            }
            //随机产生100个干扰点,使图像中的验证码不易被其他分析程序探测到
          gc.setColor(getRandColor(120,240));
            for (int i=0;i<100;i++){
                int x = r.nextInt(width);
                int y = r.nextInt(height);
                gc.drawOval(x,y,0,0);
            }
            // 随机产生4个数字的验证码
            String rs = "";
            String rn = "";
            for (int i = 0; i < 4; i++) {
                rn = String.valueOf(r.nextInt(10));
                rs += rn;
                gc.setColor(new Color(20 + r.nextInt(110), 20 + r.nextInt(110), 20 + r.nextInt(110)));
                gc.drawString(rn, 13 * i + 1, 16);
            }

            // 释放图形上下文环境
            gc.dispose();
            request.getSession().setAttribute("rcCaptcha", rs);
            ImageIO.write(bimg, "jpeg", out);
            try {
                out.flush();
            } finally {
                out.close();
            }
    }
      public Color getRandColor(int fc,int bc){
            Random r=new Random();
            if(fc>255){
                fc=255;
            }
            if (bc>255){
                bc=255;
            }
            int red=fc+r.nextInt(bc-fc);//红
            int green=fc+r.nextInt(bc-fc);//绿
            int blue=fc+r.nextInt(bc-fc);//蓝
            return new Color(red,green,blue);
        }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值