java實(shí)現(xiàn)簡(jiǎn)單驗(yàn)證碼生成
本文實(shí)例為大家分享了java驗(yàn)證碼生成的具體代碼,供大家參考,具體內(nèi)容如下
簡(jiǎn)單驗(yàn)證碼java實(shí)現(xiàn)--servlet類(lèi)生成 驗(yàn)證碼img,并寫(xiě)入session
老師給的源碼,在此做個(gè)記錄,簡(jiǎn)單驗(yàn)證碼,java繪圖

在此鳴謝并附上源碼:.
//
package app61;
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;
public class VerifyCode extends HttpServlet {
private Font mFont = new Font("Times New Roman", Font.PLAIN, 18); //設(shè)置字體
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//取得一個(gè)1000-9999的隨機(jī)數(shù)
HttpSession session = request.getSession(true);
response.setContentType("image/gif");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
int width = 60, height = 20;
ServletOutputStream out = response.getOutputStream();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB); //設(shè)置圖片大小的
Graphics gra = image.getGraphics();
Random random = new Random();
gra.setColor(getRandColor(200, 250)); //設(shè)置背景色rand墊底
gra.fillRect(0, 0, width, height);
gra.setColor(Color.black); //設(shè)置字體色
gra.setFont(mFont);
// 隨機(jī)產(chǎn)生155條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到
gra.setColor(getRandColor(160, 200));
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);
gra.drawLine(x, y, x + xl, y + yl);
}
// 取隨機(jī)產(chǎn)生的認(rèn)證碼(4位數(shù)字)
String sRand = "";
for (int i = 0; i < 4; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
// 將認(rèn)證碼顯示到圖象中
gra.setColor(new Color(20 + random.nextInt(110),
20 + random.nextInt(110),
20 + random.nextInt(110))); //調(diào)用函數(shù)出來(lái)的顏色相同,可能是因?yàn)榉N子太接近,所以只能直接生成
gra.drawString(rand, 13 * i + 6, 16);
}
session.setAttribute("rand",sRand);
// session.setAttribute("getImg", sRand);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
static Color getRandColor(int fc, int bc) { //給定范圍獲得隨機(jī)顏色
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);
}
//Clean up resources
public void destroy() {
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot消息國(guó)際化配置實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了SpringBoot消息國(guó)際化配置實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù)
x-www-form-urlencoded格式是一種常見(jiàn)的HTTP請(qǐng)求數(shù)據(jù)格式,它將請(qǐng)求參數(shù)編碼為鍵值對(duì)的形式,以便于傳輸和解析,下面這篇文章主要給大家介紹了關(guān)于后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù),需要的朋友可以參考下2023-05-05
Java實(shí)現(xiàn)微信網(wǎng)頁(yè)授權(quán)的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)微信網(wǎng)頁(yè)授權(quán)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
深入理解Java線程池從設(shè)計(jì)思想到源碼解讀
這篇文章主要介紹了深入理解Java線程池從設(shè)計(jì)思想到源碼解讀,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
解決mybatisPlus null 值更新的問(wèn)題
這篇文章主要介紹了解決mybatisPlus null 值更新的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
springcloud使用profile實(shí)現(xiàn)多環(huán)境配置方式
這篇文章主要介紹了springcloud使用profile實(shí)現(xiàn)多環(huán)境配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
通過(guò)MyBatis讀取數(shù)據(jù)庫(kù)數(shù)據(jù)并提供rest接口訪問(wèn)
這篇文章主要介紹了通過(guò)MyBatis讀取數(shù)據(jù)庫(kù)數(shù)據(jù)并提供rest接口訪問(wèn) 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08

