c.jsp 代码
<%@page import="java.awt.Graphics2D"%>
<%@page import="java.awt.Font"%>
<%@page import="java.util.Random"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.awt.Color"%>
<%@page import="java.awt.Graphics"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@ page language="java" contentType="image/png; charset=utf-8" pageEncoding="UTF-8"%>
<%
int h = 30;
int len = 4;
int w = len*20;
BufferedImage i = new BufferedImage(w,h,1);
Graphics g = i.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,w,h);
//g.setColor(Color.RED);
//g.drawRect(0,0,w-2,h-2);
String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random r = new Random();
String cc = "";
for(int n=0;n<len;n++){
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(100)+155));
int p = r.nextInt(s.length());
g.setFont(new Font("宋体",Font.BOLD,r.nextInt(8)+18));
String ss = s.substring(p,p+1);
cc+=ss;
g.drawString(ss,n*20+5,r.nextInt(5)+20);
}
session.setAttribute("check",cc);
for(int n=0;n<len;n++){
int p = r.nextInt(s.length());
g.setFont(new Font("宋体",Font.BOLD,r.nextInt(25)+15));
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(50)));
int x = r.nextInt(w);
int y = r.nextInt(h);
int width = r.nextInt(w);
int height = r.nextInt(h);
//g.fillOval(x, y, width, width);
g.drawArc(x, y, width, height, r.nextInt(360),r.nextInt(360)-360);
}
// 随机干扰字
for(int n=0;n<len;n++){
int p = r.nextInt(s.length());
g.setFont(new Font("宋体",Font.BOLD,r.nextInt(25)+15));
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(100)));
int x = r.nextInt(w);
int y = r.nextInt(h);
g.drawString(s.substring(p,p+1),x,y);
}
//随机线
for(int n=0;n<len*len;n++){
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(50)));
int x1 = r.nextInt(w);
int x2 = r.nextInt(w);
int y1 = r.nextInt(h);
int y2 = r.nextInt(h);
g.drawLine(x1, y1, x2, y2);
}
out.clear();
out = pageContext.pushBody();
ImageIO.write(i,"png",response.getOutputStream());
g.dispose();
%>