package tk.donghongyujava.util;
import java.awt.Color;
public class RandomImage {
private final static int WIDTH = 100;
private final static int HEIGHT = 30;
public static Random rd = new Random();
public static BufferedImage creatImage() {
// 画出图片
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_BGR);
// 得到画笔
Graphics g = image.getGraphics();
// 设置背景
drawBackground(g);
// 绘制边框
drawBorder(g);
//绘制内容
drawContent(g);
// 绘制干扰
drawDisturb(g);
return image;
}
private static void drawContent(Graphics g) {
// TODO Auto-generated method stub
String con="qwertyuiopasdfgjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
StringBuffer sb=new StringBuffer();
for(int i=0;i<4;i++){
int index=rd.nextInt(con.length());
char str=con.charAt(index);
sb.append(str+" ");
}
g.setColor(Color.black);
g.setFont(new Font("Gungsuh",Font.BOLD, 20));
g.drawString(sb.toString(), 15, 20);
}
private static void drawDisturb(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.BLUE);
for (int i = 0; i < 4; i++) {
int x1 = rd.nextInt(WIDTH);
int y1 = rd.nextInt(HEIGHT);
int x2 = rd.nextInt(WIDTH);
int y2 = rd.nextInt(HEIGHT);
g.drawLine(x1, y1, x2, y2);
}
g.setColor(Color.red);
g.drawOval(30, 20, 2, 2);
}
private static void drawBorder(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.yellow);
g.drawRect(0, 0, WIDTH - 2, HEIGHT - 2);
}
private static void drawBackground(Graphics g) {
// TODO Auto-generated method stub
g.setColor(Color.green);
g.fillRect(0, 0, WIDTH, HEIGHT);
}
}
servlet代码,作用是要搭配下面的HTML页面使用
package tk.donghongyujava.servlet;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tk.donghongyujava.util.RandomImage;
public class ImageServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 通知浏览器以图片的方式打开
response.setContentType("image/jpeg");
// 设置浏览器不要缓存
response.setIntHeader("expires", 0);
response.setHeader("cache-control", "no-cache");
response.setHeader("pragma", "no-cache");
ImageIO.write(RandomImage.creatImage(), "jpeg", response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
HTML页面代码
<!DOCTYPE html>
<html>
<head>
<title>Login.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function loadXMLDoc(){
variable=new XMLHttpRequest();
variable.open("GET","./creatImage.do",true);
variable.send();
}
</script>
</head>
<body>
<div>
<div style="text-align:center">
<form action="" method="get">
<table align="center">
<tr>
<td>用户名:</td>
<td colspan="2"><input type="text" name="userName"></td>
</tr>
<tr>
<td>密码:</td>
<td colspan="2"><input type="password" name="userPass"></td>
</tr>
<tr>
<td>验证码:</td>
<td colspan="2"><input type="text" name="checkCode"></td>
</tr>
<tr>
<td colspan="3"><img alt="" src="./creatImage.do" style="cursor:pointer" οnclick="loadXMLDoc()"></td>
</tr>
<tr>
<td colspan="3" style="text-align:center">
<input type="submit" value="登录"> <input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>