码迷,mamicode.com
首页 > Web开发 > 详细

jsp 验证码

时间:2014-12-08 17:35:06      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   for   java   

<%@page import="java.awt.Graphics2D"%>
<%@page import="java.util.Random"%>
<%@page import="java.awt.Font"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.awt.Color"%>
<%@page import="java.awt.Graphics"%>
<%@ page import="java.awt.image.BufferedImage"%>
<%@ page language="java" pageEncoding="utf-8" contentType="image/png" %>
<%
int h = 45;
int len = 4;
int w = len*20;
Random r = new Random();
BufferedImage img = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)img.getGraphics();
//g.setColor(Color.RED);
Color c = new Color(245,245,245);
g.setColor(c);
g.fillRect(0,0,w,h);

String str = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXY3456789";

g.setColor(Color.WHITE);

for(int i=0;i<20;i++){
    g.setFont(new Font("宋体",Font.BOLD,r.nextInt(20)+10));
    c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(50));
    String s = String.valueOf(str.charAt(r.nextInt(str.length())));
    g.setColor(c);
    g.drawString(s, r.nextInt(w),r.nextInt(h));
}

StringBuffer ss = new StringBuffer();
for(int i=0;i<len;i++){
    int n = -5 +r.nextInt(10);
    g.rotate(n * Math.PI / 180);
    g.setFont(new Font("宋体",Font.BOLD,r.nextInt(15)+15));
    c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(55)+200);
    String s = String.valueOf(str.charAt(r.nextInt(str.length())));
    ss.append(s);
    g.setColor(c);
    g.drawString(s, 2+i*20,r.nextInt(10)+25);
}
session.setAttribute("checkcode",ss.toString());

//添加干扰线 点
for(int i=0;i<10;i++){
    c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(80));
    g.setColor(c);
    g.drawLine(r.nextInt(w), r.nextInt(h),r.nextInt(w), r.nextInt(h));
    int size = r.nextInt(6)+5;
    g.fillOval(r.nextInt(w),r.nextInt(h),size,size);
}

c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(150));
int hh = r.nextInt(h);
for(int i=0;i<=300;i++){
    g.setColor(c);
    double x = i;
    double y = hh+15*Math.sin(10*i*Math.PI/360);
    g.fillOval((int)x, (int)y, 5,5);
}

g.dispose();
ImageIO.write(img, "png", response.getOutputStream());
out.clear();
out = pageContext.pushBody();
%>

 

jsp 验证码

标签:style   blog   io   ar   color   os   sp   for   java   

原文地址:http://www.cnblogs.com/Dreamlu/p/4151263.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!