字符串验证码,数字验证码生成

JSP网页生成字符串验证码,数字验证码。

<%@ page contentType="image/jpeg"
    import="java.util.HashMap,java.text.SimpleDateFormat,java.util.Date,java.awt.Color,java.awt.Font,java.awt.Graphics2D,java.awt.image.BufferedImage,java.io.IOException,java.io.OutputStream,java.util.Random,javax.imageio.ImageIO"%>
<%!public class RandomGraphic {
        private int wordHeight = 10;
        private int wordWidth = 15;
        private int fontSize = 25;
        private static final int MAX_CHARCOUNT = 16;
        public Color[] CHAR_COLOR = { Color.RED, Color.BLUE, Color.GREEN,
                Color.MAGENTA, Color.BLACK };
        public static final String GRAPHIC_JPEG = "JPEG";
        private final int initypos = 5;
        private int charCount = 0;

        private Random r = new Random();

        protected RandomGraphic(int charCount) {
            this.charCount = charCount;
        }

        public String drawAlpha(String graphicFormat, OutputStream out)
                throws IOException {
            String charValue = "";
            charValue = randAlpha();
            return draw(charValue, graphicFormat, out);
        }

        private String draw(String charValue, String graphicFormat,
                OutputStream out) throws IOException {

            int w = (charCount + 1) * wordWidth;
            int h = wordHeight * 3;

            BufferedImage bi = new BufferedImage(w, h,
                    BufferedImage.TYPE_3BYTE_BGR);
            Graphics2D g = bi.createGraphics();

            Color backColor = Color.WHITE;
            g.setBackground(backColor);
            g.fillRect(0, 0, w, h);
            g.setFont(new Font(null, Font.BOLD, fontSize));

            g.setColor(getRandColor(200, 255));
            for (int i = 0; i < 200; i++) {
                int x = r.nextInt(w);
                int y = r.nextInt(h);
                int xl = r.nextInt(12);
                int yl = r.nextInt(12);
                g.drawLine(x, y, x + xl, y + yl);
            }

            int size = CHAR_COLOR.length;
            for (int i = 0; i < charCount; i++) {
                String c = charValue.substring(i, i + 1);
                Color color = CHAR_COLOR[randomInt(0, size)];
                g.setColor(color); 
                int xpos = (i + 1) * wordWidth;//X
                int ypos = 25;//Y
                g.drawString(c, xpos, ypos);
            }
            g.dispose();
            bi.flush();

            ImageIO.write(bi, graphicFormat, out);
            return charValue;
        }

        private String randAlpha() {
            String charValue = "";
            //字符串验证码char Verifyid "defc"
            for (int i = 0; i < charCount; i++) {
                char c = (char) (randomInt(0, 26) + 'a');
                charValue += String.valueOf(c);
            } 
            //数字验证码 num Verifyid "8-9="
            int istart=1+(int)(Math.random()*9);
            int iend=1+(int)(Math.random()*9);
            String[] strOper = {"+","-","x"};
            int index=(int)(Math.random()*strOper.length);
            if(index == 1){ 
             while(istart <= iend){
              iend=1+(int)(Math.random()*10);
             }
            }
            charValue = istart+strOper[index]+iend+"=";
            for(int i = charValue.length();i<=4;i++){
             charValue =charValue+" ";
            }
            return charValue;
        }

        protected int randomInt(int from, int to) {
            return from + r.nextInt(to - from);
        }
       
        private Color getRandColor(int fc, int bc) {
            Random random = new Random();
            if (fc > 255)
                fc = 255;
            if (bc > 255)
                bc = 255;
            int r = fc + random.nextInt(bc - fc);
            int g = fc + random.nextInt(bc - fc);
            int b = fc + random.nextInt(bc - fc);
            return new Color(r, g, b);
        }
    }%>
<%
    out.clear();
    out = pageContext.pushBody();
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/jpg");
    RandomGraphic rg = new RandomGraphic(5);
    //print out jsp
    String v = rg.drawAlpha(RandomGraphic.GRAPHIC_JPEG, response
            .getOutputStream()); 
    HashMap map = new HashMap();
    map.put("SESS_VER_CD",v);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String curTime = sdf.format(new Date());
    map.put("CUR_TIME",curTime);
    session.setAttribute("SVER_CD", map);
%>

例子:
https://www.cmpay.com/service/image.xhtml?viewCode=ajax

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值