package esri;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
//javax.imageio
/**
*
* @author rjgcx
* @version 1.0
* <p>this class use to generate the image that contains the validate code</p>
*/
public class ImageValidate {
private static final int WIDTH = 130;
private static final int HEIGHT = 40;
public static void main(String[] args) throws IOException {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) image.createGraphics();
g2.setColor(Color.white);
g2.fillRect(0, 0, WIDTH, HEIGHT);
g2.setColor(Color.black);
// generate ten [a-z]
g2.setFont(new Font("DialogInput", Font.BOLD, 20));
int[] drawLetter = new int[10];
Random rand = new Random();
for (int i = 0; i < 5; i++) {
drawLetter[i] = 65 + rand.nextInt(24);
g2.drawString(String.valueOf((char)drawLetter[i]),10 * i,15 + rand.nextInt(5));
}
g2.setFont(new Font("DialogInput", Font.PLAIN, 20));
for (int i = 0; i < 5; i++) {
drawLetter[i] = 65 + rand.nextInt(24);
g2.drawString(String.valueOf((char)drawLetter[i]),80 + 10 * i,35);
}
//draw the lines
g2.dispose();
//write the image to the disk.
ImageIO.write(image, "png", new File("validate.PNG"));
}
}
运行结果