java web中圖片驗(yàn)證碼功能的簡(jiǎn)單實(shí)現(xiàn)方法
用戶在注冊(cè)網(wǎng)站信息的時(shí)候基本上都要數(shù)據(jù)驗(yàn)證碼驗(yàn)證。那么圖片驗(yàn)證碼功能該如何實(shí)現(xiàn)呢?
大概步驟是:
1.在內(nèi)存中創(chuàng)建緩存圖片
2.設(shè)置背景色
3.畫(huà)邊框
4.寫(xiě)字母
5.繪制干擾信息
6.圖片輸出
廢話不多說(shuō),直接上代碼
package com.lsgjzhuwei.servlet.response;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class VerificationCode
*/
@WebServlet(asyncSupported = true, urlPatterns = { "/VerificationCode" })
public class VerificationCode extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public VerificationCode() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
int width = 120;
int height = 30;
//創(chuàng)建一張內(nèi)存中的緩存圖片
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
///背景色
//通過(guò)graphics繪制圖像
Graphics graphics = bufferedImage.getGraphics();
//設(shè)置顏色
graphics.setColor(Color.yellow);
//填充
graphics.fillRect(0, 0, width, height);
///畫(huà)邊框
graphics.setColor(Color.blue);
graphics.drawRect(0, 0, width-1, height-1);
//寫(xiě)字母
String content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz1234567890";
Random random = new Random();
//設(shè)置字體顏色
graphics.setColor(Color.red);
//設(shè)置字體及大小
graphics.setFont(new Font("宋體", Font.BOLD, 20));
int x=20;
int y=20;
for(int i = 0; i < 4; i++)
{
int index = random.nextInt(content.length());
char letter = content.charAt(index);
graphics.drawString(letter+" ", x, y);
x = x+20;
}
//繪制干擾線
int x1;
int x2;
int y1;
int y2;
graphics.setColor(Color.LIGHT_GRAY);
for(int i = 0;i <50;i++)
{
x1=random.nextInt(width);
x2=random.nextInt(width);
y1=random.nextInt(height);
y2=random.nextInt(height);
graphics.drawLine(x1, y1, x2, y2);
}
//將圖片輸出到瀏覽器
//將內(nèi)存的圖片通過(guò)瀏覽器輸出流輸出成jpg圖片
ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
以上這篇java web中圖片驗(yàn)證碼功能的簡(jiǎn)單實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ IDEA代碼提示忽略大小寫(xiě)的具體設(shè)置步驟
在IDEA開(kāi)發(fā)過(guò)程中,代碼補(bǔ)全功能是提升編碼效率的核心特性之一,默認(rèn)情況下,IDEA的代碼提示會(huì)嚴(yán)格匹配大小寫(xiě),這對(duì)于遵循命名規(guī)范的項(xiàng)目是合理的,然而,在某些場(chǎng)景下,開(kāi)發(fā)者可能希望代碼提示忽略大小寫(xiě),以提升輸入靈活性,所以本文介紹了如何設(shè)置IDEA提示忽略大小寫(xiě)2025-06-06
如何解決創(chuàng)建maven工程時(shí),產(chǎn)生“找不到插件的錯(cuò)誤”問(wèn)題
這篇文章主要介紹了如何解決創(chuàng)建maven工程時(shí),產(chǎn)生“找不到插件的錯(cuò)誤”問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
SpringBoot2 集成log4j2日志框架的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot2 集成log4j2日志框架的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Springboot actuator生產(chǎn)就緒功能實(shí)現(xiàn)解析
這篇文章主要介紹了Springboot actuator生產(chǎn)就緒功能實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
如何用java計(jì)算兩個(gè)時(shí)間相差多少小時(shí)
最近工作中遇到需要計(jì)算時(shí)間差,下面這篇文章主要給大家介紹了關(guān)于如何用java計(jì)算兩個(gè)時(shí)間相差多少小時(shí)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
java線程安全鎖ReentrantReadWriteLock原理分析readLock
這篇文章主要為大家介紹了java線程安全鎖ReentrantReadWriteLock原理分析readLock,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10

