用jsp生成多色彩背景的验证码《源码》

该博客介绍了使用JSP实现四位验证码生成的方法。验证码字符、字体大小、类型、位置及背景颜色均随机。通过设置字符范围、随机选择认证码,对背景和字体进行随机处理,最后将生成的验证码以JPEG图像输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

四位验证码生成,其中字符随机,字体大小,类型以及位置随机,背景为彩色随机。

<%@ page contentType="image/jpeg" autoFlush="false"  import="java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*,javax.imageio.*"%>
<%
   /*生成的验证码字符范围*/
   String chose = "23456789abdefghijmnqrtyABDEFGHJLMNQRTY";
   /*显示验证码内容*/
   char display[] = {'0',' ','0',' ','0',' ','0'},
        ran[] = {'0','0','0','0'},
        temp;
   /*随机选择认证码*/
   Random rand = new Random();
   for(int i=0;i<4;i++){

       temp=chose.charAt(rand.nextInt(chose.length()));
       display[i*2]=temp;
       ran[i]=temp;
  
   }
   //String random=String.valueOf(display);
   /*保存产生的认证码*/
   session.setAttribute("verify_code",String.valueOf(ran));
%>
<%
        out.clear();
        response.setContentType("image/jpeg");
        response.addHeader("pragma","NO-cache");
        response.addHeader("Cache-Control","no-cache");
        response.addDateHeader("Expries",0);
       
        int width=140, height=40;       //认证码显示的范围
        Random tmpRand = new Random();  //随即生成数字
       
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        /*Graphics2D g = (Graphics2D)image.getGraphics();*/
     
        /*背景处理*/
        Color bkgrdColor;
        /*分成N列*/
        int widthFlag = 5 + tmpRand.nextInt(20);
        /*分成M行*/
        int  heightFlag = 0;
        int  SranWidth  = 0, SranHeight = 0;
        /*绘制方格*/
        //Random a = new Random();
        /*处理背景色为白色*/
        g.setColor(Color.WHITE);
        g.fillRect(0,0,width,height);
        /*填充背景格子颜色*/
        for(int i=0; i<widthFlag; i++){
            SranWidth = i * ( (width + 10) / widthFlag );
            heightFlag= 5 + tmpRand.nextInt(8);
            for(int j=0; j<heightFlag; j++){
               SranHeight = j * ( (height + 10) / heightFlag );
               bkgrdColor = new Color(100+tmpRand.nextInt(155),100+tmpRand.nextInt(155),100+tmpRand.nextInt(155));
               //bkgrdColor = new Color( 1+(int)Math.random()*79,1+(int)Math.random()*79,1+(int)Math.random()*79 );
               //
               bkgrdColor.brighter();
               g.setColor(bkgrdColor);
               g.fillRect(SranWidth, SranHeight, width/widthFlag, height/heightFlag);
            }
        }
        /*随机画一条线*/
        g.setColor(Color.BLACK);
        g.drawLine(tmpRand.nextInt(width),tmpRand.nextInt(height),tmpRand.nextInt(width),tmpRand.nextInt(height));
       
        /*字处理*/
        String[] font={"Clarendon Condensed","Comic Sans MS","Times New Roman","Palatino Linotype","Monotype Corsiva"};
        Color fontColor;
        int fontFlag = 0; //选择字体的类型      
        int fontSize = 18;//选择字体的大小
        for(int i=0; i<display.length; i++){
       
          fontFlag = tmpRand.nextInt(4);
          fontSize = 22 + tmpRand.nextInt(18) ;
         
          g.setFont(new Font(font[fontFlag],Font.BOLD,fontSize)); 
          fontColor = new Color( tmpRand.nextInt(100),tmpRand.nextInt(100),tmpRand.nextInt(100) );
          g.setColor(fontColor);
          g.drawChars(display,i,1,15*i+8,30 + tmpRand.nextInt(5));
        }
       
        g.dispose();
      
        ImageIO.setUseCache(false);
        ImageIO.write(image, "JPEG", response.getOutputStream());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值