Java實現(xiàn)動態(tài)獲取圖片驗證碼的示例代碼
更新時間:2019年08月16日 09:57:54 作者:楓止水
這篇文章主要介紹了Java實現(xiàn)動態(tài)獲取圖片驗證碼的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
本文介紹了Java實現(xiàn)動態(tài)獲取圖片驗證碼的示例代碼,分享給大家,具體如下:
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;
/**
* 驗證碼字符個數(shù)
*/
private int codeCount = 5;
/**
* 驗證碼干擾線數(shù)
*/
private int lineCount = 150;
/**
* 圖片驗證碼類型
*/
private ImgCodeType imgCodeType = DEFAULT_TYPE;
/**
* 驗證碼
*/
private String code = null;
/**
* 驗證碼圖片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 字符個數(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);// 每個字符的寬度
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記錄隨機產(chǎn)生的驗證碼
StringBuffer randomCode = new StringBuffer();
// 隨機產(chǎn)生codeCount個字符的驗證碼。
for (int i = 0; i < codeCount; i++) {
String strRand = getRandomStr(imgCodeType);
// 產(chǎn)生隨機的顏色值,讓輸出的每個字符的顏色值都將不同。
g.setColor(getRandomColor());
g.drawString(strRand, (i + 1) * x, codeY);
// 將產(chǎn)生的四個隨機數(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();
}
/**
* 單個漢字
*
* @return
*/
public static String creatHan() {
String code = "";
Random random = new Random();
//一個漢字兩個字節(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ù)字驗證碼
*
*
* @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 + "]");
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java+OpenCV調(diào)用攝像頭實現(xiàn)拍照功能
隨著我們對環(huán)境、Mat基本使用越來越熟練、Java Swing也逐步熟悉了起來。本文將通過OpenCV驅(qū)動攝像頭實現(xiàn)識臉和拍照功能,需要的可以參考一下2022-03-03
詳解如何使用Spring的@FeignClient注解實現(xiàn)通信功能
SpringBoot是一個非常流行的Java框架,它提供了一系列工具來使這種交互無縫且高效,在這些工具中,@FeignClient注解因其易用性和強大的功能而脫穎而出, 在這篇文章中,我們將探討如何使用Spring的@FeignClient注解進行客戶端-服務(wù)器通信,需要的朋友可以參考下2023-11-11
java并發(fā)包中CountDownLatch和線程池的使用詳解
這篇文章主要介紹了java并發(fā)包中CountDownLatch和線程池的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Java繼承extends與super關(guān)鍵字詳解
本篇文章給大家詳細講述了Java繼承extends與super關(guān)鍵字的相關(guān)知識點,需要的朋友們可以參考學習下。2018-02-02
使用Log4j為項目配置日志輸出應用詳解以及示例演示的實現(xiàn)分析
本篇文章是對Log4j為項目配置日志輸出應用詳解以及示例演示的實現(xiàn)進行了分析介紹,需要的朋友參考下2013-05-05

