Java實(shí)現(xiàn)動(dòng)態(tài)獲取圖片驗(yàn)證碼的示例代碼
本文介紹了Java實(shí)現(xiàn)動(dòng)態(tài)獲取圖片驗(yàn)證碼的示例代碼,分享給大家,具體如下:
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Random;
public class ImgAuthCode {
/**
* 圖片的寬度
*/
private int width = 330;
/**
* 圖片的高度
*/
private int height = 40;
/**
* 驗(yàn)證碼字符個(gè)數(shù)
*/
private int codeCount = 5;
/**
* 驗(yàn)證碼干擾線數(shù)
*/
private int lineCount = 150;
/**
* 圖片驗(yàn)證碼類型
*/
private ImgCodeType imgCodeType = DEFAULT_TYPE;
/**
* 驗(yàn)證碼
*/
private String code = null;
/**
* 驗(yàn)證碼圖片Buffer
*/
private BufferedImage buffImg = null;
private static final ImgCodeType DEFAULT_TYPE = ImgCodeType.ENANDNUMBER;
private static char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
public ImgAuthCode() {
this.createCode();
}
/**
* @param width 圖片寬
* @param height 圖片高
*/
public ImgAuthCode(int width, int height) {
this.width = width;
this.height = height;
this.createCode();
}
/**
* @param width 圖片寬
* @param height 圖片高
* @param codeCount 字符個(gè)數(shù)
* @param lineCount 干擾線條數(shù)
*/
public ImgAuthCode(int width, int height, int codeCount, int lineCount,ImgCodeType imgCodeType) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
this.imgCodeType = imgCodeType;
this.createCode();
}
public void createCode() {
int x = 0, fontHeight = 0, codeY = 0;
x = width / (codeCount + 2);// 每個(gè)字符的寬度
fontHeight = height - 2;// 字體的高度
codeY = height - 4;
// 圖像buffer
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
// 將圖像填充為白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// 創(chuàng)建字體
// ImgFontByte imgFont = new ImgFontByte();
// Font font = imgFont.getFont(fontHeight);
Font font=new Font("微軟雅黑",Font.PLAIN, fontHeight);
g.setFont(font);
drawRandomLine(g);
// randomCode記錄隨機(jī)產(chǎn)生的驗(yàn)證碼
StringBuffer randomCode = new StringBuffer();
// 隨機(jī)產(chǎn)生codeCount個(gè)字符的驗(yàn)證碼。
for (int i = 0; i < codeCount; i++) {
String strRand = getRandomStr(imgCodeType);
// 產(chǎn)生隨機(jī)的顏色值,讓輸出的每個(gè)字符的顏色值都將不同。
g.setColor(getRandomColor());
g.drawString(strRand, (i + 1) * x, codeY);
// 將產(chǎn)生的四個(gè)隨機(jī)數(shù)組合在一起。
randomCode.append(strRand);
}
this.code = randomCode.toString();
}
private void drawRandomLine(Graphics g){
//干擾線
for (int i = 0; i < lineCount; i++) {
int xs = new Random().nextInt(width);
int ys = new Random().nextInt(height);
int xe = xs + new Random().nextInt(width / 8);
int ye = ys + new Random().nextInt(height / 8);
g.setColor(getRandomColor());
g.drawLine(xs, ys, xe, ye);
}
}
public Color getRandomColor(){
int red = 0, green = 0, blue = 0;
Random random = new Random();
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
return new Color(red,green,blue);
}
public String getRandomStr(ImgCodeType type) {
switch (type) {
case ENANDNUMBER:
return creatENAndNumberCode();
case HAN:
return creatHan();
case EN:
return creatENCode();
case NUMBER:
return creatNumberCode();
default:
return creatENAndNumberCode();
}
}
public void write(String path) throws IOException {
OutputStream sos = new FileOutputStream(path);
this.write(sos);
}
public void write(OutputStream sos) throws IOException {
ImageIO.write(buffImg, "png", sos);
sos.close();
}
/**
* 單個(gè)漢字
*
* @return
*/
public static String creatHan() {
String code = "";
Random random = new Random();
//一個(gè)漢字兩個(gè)字節(jié)
byte[] bytes = new byte[2];
bytes[0] = Integer.valueOf(176 + Math.abs(random.nextInt(39))).byteValue();
bytes[1] = Integer.valueOf(161 + Math.abs(random.nextInt(93))).byteValue();
try {
code = new String(bytes, "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return code;
}
/**
* 英文字母加數(shù)字
*
* @return
*/
public static String creatENAndNumberCode() {
Random random = new Random();
return String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
}
/**
* 英文字母
*
*
* @return
*/
public static String creatENCode() {
StringBuffer code = new StringBuffer();
Random random = new Random();
int s = random.nextInt(25) + 65;
code.append((char) s);
return code.toString();
}
/**
* 純數(shù)字驗(yàn)證碼
*
*
* @return
*/
public static String creatNumberCode() {
StringBuffer code = new StringBuffer();
Random random = new Random();
return code.append(random.nextInt(9) + "").toString();
}
public BufferedImage getBuffImg() {
return buffImg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public static void main(String[] args) {
String randomChineseChar = creatHan();
String randomEnChar = creatENCode();
System.out.println("args = [" + randomChineseChar + "]");
System.out.println("args = [" + randomEnChar + "]");
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java+OpenCV調(diào)用攝像頭實(shí)現(xiàn)拍照功能
隨著我們對(duì)環(huán)境、Mat基本使用越來(lái)越熟練、Java Swing也逐步熟悉了起來(lái)。本文將通過(guò)OpenCV驅(qū)動(dòng)攝像頭實(shí)現(xiàn)識(shí)臉和拍照功能,需要的可以參考一下2022-03-03
詳解如何使用Spring的@FeignClient注解實(shí)現(xiàn)通信功能
SpringBoot是一個(gè)非常流行的Java框架,它提供了一系列工具來(lái)使這種交互無(wú)縫且高效,在這些工具中,@FeignClient注解因其易用性和強(qiáng)大的功能而脫穎而出, 在這篇文章中,我們將探討如何使用Spring的@FeignClient注解進(jìn)行客戶端-服務(wù)器通信,需要的朋友可以參考下2023-11-11
java并發(fā)包中CountDownLatch和線程池的使用詳解
這篇文章主要介紹了java并發(fā)包中CountDownLatch和線程池的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
Java實(shí)現(xiàn)簡(jiǎn)單抽獎(jiǎng)功能界面
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單抽獎(jiǎng)功能界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
Java繼承extends與super關(guān)鍵字詳解
本篇文章給大家詳細(xì)講述了Java繼承extends與super關(guān)鍵字的相關(guān)知識(shí)點(diǎn),需要的朋友們可以參考學(xué)習(xí)下。2018-02-02
使用Log4j為項(xiàng)目配置日志輸出應(yīng)用詳解以及示例演示的實(shí)現(xiàn)分析
本篇文章是對(duì)Log4j為項(xiàng)目配置日志輸出應(yīng)用詳解以及示例演示的實(shí)現(xiàn)進(jìn)行了分析介紹,需要的朋友參考下2013-05-05

