Java后端實現(xiàn)生成驗證碼圖片的示例代碼
驗證碼是一種用于驗證用戶身份或確保用戶操作安全的技術手段。它通常以圖形、聲音或文字的形式出現(xiàn),要求用戶在登錄、注冊或進行特定操作之前輸入正確的驗證碼。
驗證碼的目的是防止網(wǎng)絡惡意行為,如惡意注冊、暴力破解密碼、爬取數(shù)據(jù)等。通過要求用戶正確輸入驗證碼,可以有效阻止機器人或自動化程序的訪問。
常見的驗證碼形式包括圖像驗證碼、音頻驗證碼和文字驗證碼。圖像驗證碼要求用戶識別并選擇特定圖像中的特定物體或圖案,音頻驗證碼要求用戶聽取一段隨機生成的音頻并輸入其中的數(shù)字或字母,文字驗證碼則要求用戶識別并輸入特定的字母、數(shù)字或符號。
驗證碼的生成和驗證通常基于特定的算法和技術,如隨機數(shù)生成、圖像識別、語音合成等。驗證碼通常具有一定的復雜性和難度,以確保只有真正的用戶能夠解讀和輸入正確的驗證碼。
然而,驗證碼也可能給用戶帶來一定的不便,特別是對于視力或聽力有障礙的用戶。因此,一些網(wǎng)站和應用程序也提供了可選的輔助功能,如文字提示、放大鏡、語音提示等,以幫助用戶更好地完成驗證碼驗證。
下面講解一下如何使用Java生成驗證碼圖片。
代碼如下:
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
/**
* @author tobiasy
* @date 2020/4/10
*/
public class ValidateCode {
private int width = 160;
private int height = 40;
/**
* 驗證碼字符個數(shù)
*/
private int codeCount = 5;
/**
* 驗證碼干擾線數(shù)
*/
private int lineCount = 150;
/**
* 驗證碼
*/
private String code = null;
/**
* 驗證碼圖片Buffer
*/
private BufferedImage buffImg = null;
// 驗證碼范圍,去掉0(數(shù)字)和O(拼音)容易混淆的(小寫的1和L也可以去掉,大寫不用了)
// private 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', '2', '3', '4', '5', '6', '7', '8', '9'};
private char[] codeSequence = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
public ValidateCode() {
this.createCode();
}
public ValidateCode(int width, int height) {
this.width = width;
this.height = height;
this.createCode();
}
public ValidateCode(int width, int height, int codeCount, int lineCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
this.createCode();
}
public void createCode() {
int x, fontHeight, codey;
x = width / (codeCount + 2);
fontHeight = height - 2;
codey = height - 4;
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
buffImg = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g.dispose();
g = buffImg.createGraphics();
g.fillRect(128, 128, width, height);
Random random = new Random();
Font font = new Font("微軟雅黑", Font.PLAIN, fontHeight);
g.setFont(font);
for (int i = 0; i < lineCount; i++) {
int xs = random.nextInt(width);
int ys = random.nextInt(height);
int xe = xs + random.nextInt(width / 8);
int ye = ys + random.nextInt(height / 8);
g.setColor(getRandomColor());
g.drawLine(xs, ys, xe, ye);
}
StringBuilder randomCode = new StringBuilder();
for (int i = 0; i < codeCount; i++) {
String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);
g.setColor(getRandomColor());
g.drawString(strRand, (i + 1) * x, codey);
randomCode.append(strRand);
}
code = randomCode.toString();
}
private Color getRandomColor(){
Random random = new Random();
return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
}
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();
}
public BufferedImage getBuffImg() {
return buffImg;
}
public String getCode() {
return code;
}
public static void main(String[] args) {
ValidateCode vCode = new ValidateCode(160, 40, 4, 60);
try {
String path = "F:/test/images/" + System.currentTimeMillis() + ".png";
FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
vCode.write(fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}效果如下

到此這篇關于Java后端實現(xiàn)生成驗證碼圖片的示例代碼的文章就介紹到這了,更多相關Java生成驗證碼圖片內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java mybatis框架實現(xiàn)多表關系查詢功能
這篇文章主要介紹了java mybatis框架實現(xiàn)多表關系查詢,基于Maven框架的整體設計 —— 一多一的關系,文中通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10
Spring Boot前后端分離開發(fā)模式中的跨域問題及解決方法
本文介紹了解決Spring Boot前端Vue跨域問題的實戰(zhàn)經(jīng)驗,并提供了后端和前端的配置示例,通過配置后端和前端,我們可以輕松解決跨域問題,實現(xiàn)正常的前后端交互,需要的朋友可以參考下2023-09-09
springboot配置多數(shù)據(jù)源并集成Druid和mybatis的操作
這篇文章主要介紹了springboot配置多數(shù)據(jù)源并集成Druid和mybatis的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot使用Swagger生成多模塊的API文檔
這篇文章將以?Spring?Boot?多模塊項目為例,為大家詳細介紹一下如何使用?Swagger?生成多模塊的?API?文檔,感興趣的小伙伴可以了解一下2025-02-02

