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

 

转载于:https://www.cnblogs.com/Dreamlu/p/4151263.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值