今天上午抽了点时间,看到某些论坛上利用python语言写了一个关于验证码的程序,于是乎,手痒痒,就写了一个利用java编写的代码片段,在这里就给
大家贴出来,好来弥补最近没有在论坛发言的罪过。(以下代码,只适合初学者,没什么记住含量,仅仅是为了实现功能,如有更好意见,欢迎论坛致贴)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.io.File;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class test extends JFrame{
private static final int LABLE_WIDTH = 500;
private static final int WORD_NUMBER = 4;
private ImageIcon image = generateRandomPicture();
public static Color[] colors = {Color.black,
Color.blue,
Color.red,
Color.cyan,
Color.darkGray,
Color.gray,
Color.magenta,
Color.orange,
Color.yellow};
public test() {
init();
addComponent(image);
}
private void init(){
this.setBounds(new Rectangle(200,200,LABLE_WIDTH,LABLE_WIDTH));
this.setLocationRelativeTo(null);
this.getContentPane().setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setBackground(Color.orange);
}
private void addComponent(ImageIcon image){
JLabel img = new JLabel(image);
this.getContentPane().add(img,BorderLayout.CENTER);
}
/**
* 创建一个图片
* @return
*/
private ImageIcon generateRandomPicture(){
Random random = new Random();
BufferedImage imageBuffered = new BufferedImage(500,300, BufferedImage.TYPE_INT_BGR);
Graphics g = imageBuffered.createGraphics();
Color colorBak = g.getColor();
//绘制图片背景
g.setColor(Color.green);
g.fillRect(0, 0, LABLE_WIDTH,LABLE_WIDTH);
//绘制线条
for (int i = 1; i < 200; i++) {
int x1 = random.nextInt(LABLE_WIDTH);
int x2 = random.nextInt(LABLE_WIDTH);;
int y1 = random.nextInt(LABLE_WIDTH);;
int y2 = random.nextInt(LABLE_WIDTH);;
g.setColor(colors[i%colors.length]);
g.drawLine(x1, y1, x2, y2);
}
//绘制验证码
for (int i = 0; i < WORD_NUMBER; i++) {
int index = (int)(random.nextFloat()*colors.length);
g.setColor(colors[index]);
int size = getPonit(60,100);
int height = getPonit(100,300);
int width = getPonit(LABLE_WIDTH/WORD_NUMBER*i, LABLE_WIDTH/WORD_NUMBER*i+50);
g.setFont(new Font("宋体",Font.BOLD,size));
char content = getCharater();
g.drawString(content+"", width, height);
}
//解放资源
g.dispose();
return new ImageIcon(imageBuffered);
}
/**
* 获取一个范围内的某一值
* @param x 最小值
* @param y 最大值
* @return
*/
private int getPonit(int x, int y){
Random random = new Random();
int value = (int)(random.nextFloat()*LABLE_WIDTH);
while(true){
if(value > x && value < y){
return value;
}else{
value = (int)(random.nextFloat()*LABLE_WIDTH);
}
}
}
/**
* 获取一个中文字符
* @return
*/
private char getCharater(){
Random random = new Random();
int value = random.nextInt(0x9fbf >> 1);
while(true){
if(value > 0x4e00 && value < 0x9fbf){
return (char)value;
}else{
if(value < 0 || value > 0x9fbf){
value = random.nextInt(0x9fbf >> 1);
}
value <<= 1;
}
}
}
public static void main(String[] args) {
test t = new test();
t.setVisible(true);
}
}
该贴由koei转至本版2014-5-2 16:22:04