来我群吧java-web交流群:132607763
生成图片的类
package com.randy.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
public class IdentityImage {
private static final char[] CHARS = new char[] { '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
'L', 'M', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
private static Random random = new Random();
public static String getRandomString(int num) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < num; i++) {
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
}
private static Color getRandomColor() {
return new Color(random.nextInt(255), random.nextInt(255),
random.nextInt(255));
}
private static Color getReverseColor(Color c) {
return new Color(fixRGB(255 - random.nextInt(c.getRed())),
fixRGB(255 - random.nextInt(c.getGreen())), fixRGB(255 - random.nextInt(c
.getBlue())));
}
private static int fixRGB(int i) {
if(i > 0) {
return i;
}
return 1;
}
public static BufferedImage createImage(String string,int width,int height,int x, int y,int numHSBNoise) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
Color c = getRandomColor();
g.setColor(c);
g.fillRect(0, 0, width, height);
g.setColor(getReverseColor(c));
g.setFont(new Font(Font.SANS_SERIF,Font.BOLD,16));
g.drawString(string, x, y);
for(int i = 0; i < numHSBNoise; i++) {
g.fillRect(random.nextInt(width), random.nextInt(height), 1, 1);
}
return image;
}
}
验证码的servlet
package servlets;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.randy.util.IdentityImage;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* Servlet implementation class IdentityImageServlet
*/
public class IdentityImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public IdentityImageServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
String randomString = IdentityImage.getRandomString(5);
session.setAttribute("randomString", randomString);
BufferedImage image = IdentityImage.createImage(randomString, 100, 30, 18, 20, 100);
ServletOutputStream outputStream = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
encoder.encode(image);
outputStream.flush();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
}
使用:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Identity Image</title>
<script type="text/javascript">
function reloadImage() {
document.getElementById("btn").disabled = true;
document.getElementById("identity").src = 'servlets/IdentityImageServlet?ts=' + new Date().getTime();
}
</script>
</head>
<body>
<img src="servlets/IdentityImageServlet" id="identity" onload="btn.disabled=false;" />
<input type="button" value="change" onclick="reloadImage()" id="btn">
</body>
</html>