1.在工具包下编写ImageCode.java
public class ImageCode {
private static Random random = new Random();
public static Map<String, BufferedImage> getImage(int width, int height,
int n, int charSize) {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(new Color(174, 221, 129));
g.fillRect(0, 0, width, height);
g.setColor(getColor());
// 画干扰线
for (int i = 0; i < 10; i++) {
g.drawLine(random.nextInt(width), random.nextInt(height), random
.nextInt(width) + 5, random.nextInt(height) + 5);
}
/**
* 获取验证字符
*/
String[] code = getCheckString(n);
StringBuilder builder = new StringBuilder();
/**
* 将字符画到图片上去
*/
g.setFont(new Font(null, Font.BOLD + Font.ITALIC, charSize));
for (int i = 0; i < code.length; i++) {
g.setColor(getColor());
g.drawString(code[i], i * wid