?? checkcode.java
字號:
package com.test.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.Random;
/**
* 生成驗證碼
*
* @author javabs
*
*/
public class CheckCode {
private String sRand = "";
private BufferedImage image;
// 設置寬度,高度
private int width = 60, height = 20;
// 設置數字個數
private int numCount = 4;
public CheckCode() {
}
public CheckCode(int numCount) {
this.numCount = numCount;
}
public CheckCode(int width, int height) {
this.width = width;
this.height = height;
}
public CheckCode(int width, int height, int numCount) {
this.width = width;
this.height = height;
this.numCount = numCount;
}
public void generator() {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
g.setColor(getRandColor(160, 200));
// 獲取155條隨機干擾線
Random random = new Random();
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 產生四個隨機數字
for (int i = 0; i < numCount; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(rand, width / numCount * i + 3, 16);
}
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getNumCount() {
return numCount;
}
public void setNumCount(int numCount) {
this.numCount = numCount;
}
// 獲取隨機顏色值
private Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
// 獲取隨機圖片
public Image getRandomImage() {
return image;
}
public String getRand() {
return sRand;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -