jsp 验证码

<%@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();
%>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。