Java 驗證碼工具類


 

驗證碼代碼工具代碼如下:

 

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
* @title 驗證碼工具類
* @author boat
*/
public class ImageUtil {

private static int width = 90;// 定義圖片的width
private static int height = 30;// 定義圖片的height

//5個驗證碼一組
/* private static int codeCount = 5;// 定義圖片上顯示驗證碼的個數
private static int xx = 13;*/

//4個驗證碼一組
private static int codeCount = 4;// 定義圖片上顯示驗證碼的個數
private static int xx = 15;

private static int codeY = 23;
private static int fontHeight = 22;

private static char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t', 'u','v','w','x','y','z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

/**
* 生成一個map集合
* code為生成的驗證碼
* codePic為生成的驗證碼BufferedImage對象
*
* @return
*/
public static Map<String, Object> generateCodeAndPic() {
// 定義圖像buffer
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// Graphics2D gd = buffImg.createGraphics();
// Graphics2D gd = (Graphics2D) buffImg.getGraphics();
Graphics gd = buffImg.getGraphics();

// 創建一個隨機數生成器類
Random random = new Random();

// 將圖像填充為白色
gd.setColor(Color.WHITE);
gd.fillRect(0, 0, width, height);

// 創建字體,字體的大小應該根據圖片的高度來定。
Font font = new Font("Arial", Font.BOLD, fontHeight);

// 設置字體。
gd.setFont(font);

// 畫邊框。
//gd.setColor(Color.LIGHT_GRAY);
gd.setColor(getRandomColor());
gd.drawRect(0, 0, width - 1, height - 1);

// 隨機產生40條干擾線,使圖象中的認證碼不易被其它程序探測到。
gd.setColor(getRandomColor());

for (int i = 0; i < 30; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
gd.drawLine(x, y, x + xl, y + yl);
}

// randomCode用於保存隨機產生的驗證碼,以便用戶登錄后進行驗證。
StringBuffer randomCode = new StringBuffer();

// 隨機產生codeCount數字的驗證碼。
for (int i = 0; i < codeCount; i++) {

// 得到隨機產生的驗證碼數字。
String code = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);

// 用隨機產生的顏色將驗證碼繪制到圖像中。
gd.setColor(getRandomColor());

gd.drawString(code, (i + 1) * xx, codeY);

// 將產生的四個隨機數組合在一起。
randomCode.append(code);
}
Map<String, Object> map = new HashMap<String, Object>();
//存放驗證碼
map.put("code", randomCode);
//存放生成的驗證碼BufferedImage對象
map.put("codePic", buffImg);
return map;
}

/**
* 隨機取色
*/
public static Color getRandomColor() {
Random ran = new Random();
int red = ran.nextInt(256);
int green = ran.nextInt(256);
int blue = ran.nextInt(256);
Color color = new Color(red,green,blue);
return color;
}

// 獲取驗證碼
public static Map<String,Object> getCheckCodePic(){
return ImageUtil.generateCodeAndPic();
}

public static void main(String[] args) throws Exception {
//創建文件輸出流對象
OutputStream out = new FileOutputStream("d://"+System.currentTimeMillis()+".jpg");
Map<String,Object> map = ImageUtil.generateCodeAndPic();
ImageIO.write((RenderedImage) map.get("codePic"), "jpeg", out);
System.out.println("驗證碼的值為:"+map.get("code"));
}

}


直接copy代碼,復制到項目中使用!



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM