驗證碼程序
阿新 • • 發佈:2018-10-08
fill tran wing buffer abcde rand return operation ++ package Captcha;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Captcha {
// 圖像寬度
private int width = 80;
// 圖像高度
private int height = 30;
// 驗證碼字符長度
private int length = 4;
// 隨機生成驗證碼基礎字符串
private final String baseCharacters = "abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXYZ23456789"; public BufferedImage getCaptchaImage() {
BufferedImage img = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(getRandomColor(200, 255));
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));
Random rand = new Random();
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);
}
g.dispose();
return img;
}
private void drawString(Graphics g, int width, int height, String str) {
Random rand = new Random();
int degree = rand.nextInt(60);
if (degree > 30)
degree = 30 - degree;
g.setColor(getRandomColor(0, 80));
Graphics2D g2 = (Graphics2D) g.create();
g2.translate(width + rand.nextInt(5), height + rand.nextInt(5));
g2.rotate(degree * Math.PI / 180);
g2.drawString(str, 0, 0);
} private Color getRandomColor(int minimum, int maximum) {
if (minimum > maximum) {
int tmp = minimum;
minimum = maximum;
maximum = tmp;
}
if (maximum > 255)
maximum = 255;
if (minimum < 0)
minimum = 0; int r = minimum + (int) (Math.random() * (maximum - minimum));
int g = minimum + (int) (Math.random() * (maximum - minimum));
int b = minimum + (int) (Math.random() * (maximum - minimum)); return new Color(r, g, b);
} public static void main(String[] args) {
Captcha captcha = new Captcha();
JFrame frame = new JFrame("驗證碼");
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel lbl = new JLabel(new ImageIcon(captcha.getCaptchaImage()));
frame.add(lbl);
frame.setVisible(true);
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Captcha {
// 圖像寬度
private int width = 80;
// 圖像高度
private int height = 30;
// 驗證碼字符長度
private int length = 4;
// 隨機生成驗證碼基礎字符串
private final String baseCharacters = "abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXYZ23456789"; public BufferedImage getCaptchaImage() {
BufferedImage img = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(getRandomColor(200, 255));
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));
Random rand = new Random();
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);
}
g.dispose();
return img;
}
private void drawString(Graphics g, int width, int height, String str) {
Random rand = new Random();
int degree = rand.nextInt(60);
if (degree > 30)
degree = 30 - degree;
g.setColor(getRandomColor(0, 80));
Graphics2D g2 = (Graphics2D) g.create();
g2.translate(width + rand.nextInt(5), height + rand.nextInt(5));
g2.rotate(degree * Math.PI / 180);
g2.drawString(str, 0, 0);
} private Color getRandomColor(int minimum, int maximum) {
if (minimum > maximum) {
int tmp = minimum;
minimum = maximum;
maximum = tmp;
}
if (maximum > 255)
maximum = 255;
if (minimum < 0)
minimum = 0; int r = minimum + (int) (Math.random() * (maximum - minimum));
int g = minimum + (int) (Math.random() * (maximum - minimum));
int b = minimum + (int) (Math.random() * (maximum - minimum)); return new Color(r, g, b);
} public static void main(String[] args) {
Captcha captcha = new Captcha();
JFrame frame = new JFrame("驗證碼");
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel lbl = new JLabel(new ImageIcon(captcha.getCaptchaImage()));
frame.add(lbl);
frame.setVisible(true);
}
}
驗證碼程序