package validation;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* 验证码
*
* @author
*
*/
public class ValidationCode {
private int width = 110;//验证码宽度
private int height = 50;//验证码高度
private int length = 5;//验证码长度
private int temp=8;//线条条数
// 随机生成验证码基础字符串
private final String baseCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ0123456789";
Random rand = new Random();//随机数
/**
* 获取验证码图像
*
* @return 验证码图像
*/
public BufferedImage getCaptchaImage() {
BufferedImage img = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
int r1 = rand.nextInt(224);
int g1 = rand.nextInt(224);
int b1 = rand.nextInt(224);
g.setColor(new Color(r1, g1, b1));
g.fillRect(0, 0, width, height);
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
//生成随机验证码
int len = baseCharacters.length(); // 基础字符串长度
g.setFont(new Font("楷体", Font.HANGING_BASELINE, 24)); // 设置验证码字体
// 循环生成验证码各字符
for (int i = 0; i < length; i++) {
// 随机生成验证码中单个字符
String randStr = String.valueOf(baseCharacters.charAt(rand
.nextInt(len)));
// 单个字符绘制宽度
int width = this.width / this.length;
// 当前字符绘制原点
int x = width * i;
int y = this.height / 2 + rand.nextInt(this.height / 3);
/* 将该字符画到图像中 */
drawString(g, x, y, randStr);
}
// 循环画干扰线
for (int i = 0; i < temp; i++) {
// 设置线条随机颜色
int r2 = rand.nextInt(224);
int g2 = rand.nextInt(224);
int b2 = rand.nextInt(224);
g.setColor(new Color(r2, g2, b2));
// 生成随机线条起点终点坐标点
int x1 = rand.nextInt(this.width);
int y1 = rand.nextInt(this.height);
int x2 = rand.nextInt(this.width);
int y2 = rand.nextInt(this.height);
// 画线条
g.drawLine(x1, y1, x2, y2);
}
// 释放图像上下文(画笔)
g.dispose();
return img;
}
/**
* 画验证码字符串中单个字符
*/
private void drawString(Graphics g, int width, int height, String str) {
// 设置字体颜色
int r1 = rand.nextInt(224);
int g1 = rand.nextInt(224);
int b1 = rand.nextInt(224);
g.setColor(new Color(r1, g1, b1));
// 转换 Graphics2D
Graphics2D g2 = (Graphics2D) g.create();
// 平移原点到图形环境的中心
g2.translate(width + rand.nextInt(5), height + rand.nextInt(5));
// 旋转文本
g2.rotate((rand.nextInt(60)-30) * Math.PI / 180);
// 画文本
g2.drawString(str, 0, 0);
}
public static void main(String[] args) {
ValidationCode captcha = new ValidationCode();
captcha.start();
}
/**
* 生成
*
*/
JFrame frame;
public void start(){
ValidationCode captcha = new ValidationCode();
frame = new JFrame("验证码");
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lbl= new JLabel(new ImageIcon(captcha.getCaptchaImage()));
JPanel pnl = new JPanel();
pnl.add(lbl);
JButton jbut=new JButton("看不清,换一张");
pnl.add(jbut);
frame.add(pnl, BorderLayout.CENTER);
//监听按钮(看不清)
ActionListener listener=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
start();
}
};
jbut.addActionListener(listener);
frame.setVisible(true);
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* 验证码
*
* @author
*
*/
public class ValidationCode {
private int width = 110;//验证码宽度
private int height = 50;//验证码高度
private int length = 5;//验证码长度
private int temp=8;//线条条数
// 随机生成验证码基础字符串
private final String baseCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ0123456789";
Random rand = new Random();//随机数
/**
* 获取验证码图像
*
* @return 验证码图像
*/
public BufferedImage getCaptchaImage() {
BufferedImage img = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
int r1 = rand.nextInt(224);
int g1 = rand.nextInt(224);
int b1 = rand.nextInt(224);
g.setColor(new Color(r1, g1, b1));
g.fillRect(0, 0, width, height);
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
//生成随机验证码
int len = baseCharacters.length(); // 基础字符串长度
g.setFont(new Font("楷体", Font.HANGING_BASELINE, 24)); // 设置验证码字体
// 循环生成验证码各字符
for (int i = 0; i < length; i++) {
// 随机生成验证码中单个字符
String randStr = String.valueOf(baseCharacters.charAt(rand
.nextInt(len)));
// 单个字符绘制宽度
int width = this.width / this.length;
// 当前字符绘制原点
int x = width * i;
int y = this.height / 2 + rand.nextInt(this.height / 3);
/* 将该字符画到图像中 */
drawString(g, x, y, randStr);
}
// 循环画干扰线
for (int i = 0; i < temp; i++) {
// 设置线条随机颜色
int r2 = rand.nextInt(224);
int g2 = rand.nextInt(224);
int b2 = rand.nextInt(224);
g.setColor(new Color(r2, g2, b2));
// 生成随机线条起点终点坐标点
int x1 = rand.nextInt(this.width);
int y1 = rand.nextInt(this.height);
int x2 = rand.nextInt(this.width);
int y2 = rand.nextInt(this.height);
// 画线条
g.drawLine(x1, y1, x2, y2);
}
// 释放图像上下文(画笔)
g.dispose();
return img;
}
/**
* 画验证码字符串中单个字符
*/
private void drawString(Graphics g, int width, int height, String str) {
// 设置字体颜色
int r1 = rand.nextInt(224);
int g1 = rand.nextInt(224);
int b1 = rand.nextInt(224);
g.setColor(new Color(r1, g1, b1));
// 转换 Graphics2D
Graphics2D g2 = (Graphics2D) g.create();
// 平移原点到图形环境的中心
g2.translate(width + rand.nextInt(5), height + rand.nextInt(5));
// 旋转文本
g2.rotate((rand.nextInt(60)-30) * Math.PI / 180);
// 画文本
g2.drawString(str, 0, 0);
}
public static void main(String[] args) {
ValidationCode captcha = new ValidationCode();
captcha.start();
}
/**
* 生成
*
*/
JFrame frame;
public void start(){
ValidationCode captcha = new ValidationCode();
frame = new JFrame("验证码");
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lbl= new JLabel(new ImageIcon(captcha.getCaptchaImage()));
JPanel pnl = new JPanel();
pnl.add(lbl);
JButton jbut=new JButton("看不清,换一张");
pnl.add(jbut);
frame.add(pnl, BorderLayout.CENTER);
//监听按钮(看不清)
ActionListener listener=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
start();
}
};
jbut.addActionListener(listener);
frame.setVisible(true);
}
}