java圖片驗證碼生成教程詳解
我們先來看本地如何生成圖片驗證碼的,再來寫輸出到網(wǎng)頁的驗證碼如何實現(xiàn)。
先來看最簡單的—實現(xiàn)的功能是,將一個字符串變成圖片寫入到文件中
實現(xiàn)代碼:
package cn.hncu.img;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
//該類包含一些用來查找 ImageReader 和 ImageWriter 以及執(zhí)行簡單編碼和解碼的靜態(tài)便捷方法。
import org.junit.Test;
public class ImgDemo {
//學習如何把一個字符串變成圖片寫到一個文件
@Test
public void ImgDemo1() throws FileNotFoundException, IOException{
BufferedImage img = new BufferedImage(60, 30, BufferedImage.TYPE_INT_RGB);
// 表示一個圖像,它具有合成整數(shù)像素的 8 位 RGB 顏色分量。
Graphics g = img.getGraphics();
g.drawString("Hello",10,20);
//使用此圖形上下文的當前字體和顏色繪制由指定 string 給定的文本。最左側(cè)字符的基線位于此圖形上下文坐標系的 (x, y) 位置處。
g.dispose();////類似于流中的close()帶動flush()---把數(shù)據(jù)刷到img對象當中
//釋放此圖形的上下文以及它使用的所有系統(tǒng)資源。調(diào)用 dispose 之后,就不能再使用 Graphics 對象。
ImageIO.write(img, "JPG", new FileOutputStream("img/a.jpg"));
//使用支持給定格式的任意 ImageWriter 將一個圖像寫入 File。
}
}
結(jié)果:

上面那個很簡單,對不對,我們看到的驗證碼都不是這樣的,那好,我們給它加點干擾線,背景色,字符和y坐標隨機生成。
有干擾線、背景色的驗證碼-寫入文件
演示代碼:
package cn.hncu.img;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import javax.imageio.ImageIO;
//該類包含一些用來查找 ImageReader 和 ImageWriter 以及執(zhí)行簡單編碼和解碼的靜態(tài)便捷方法。
import org.junit.Test;
public class ImgDemo {
//把上面的字符串改成我們平時用的驗證碼---生成幾個隨機數(shù)字,有背景色和干擾線
@Test
public void ImgDemo2() throws FileNotFoundException, IOException{
int width = 80;
int height= 40;
int lines = 10;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
//設(shè)置背景色
g.setColor(Color.white);
g.fillRect(0, 0, width, height);//畫背景
//填充指定的矩形。使用圖形上下文的當前顏色填充該矩形
//設(shè)置字體
g.setFont(new Font("宋體", Font.BOLD, 18));
//隨機數(shù)字
Date d = new Date();
//System.out.println(d.getTime());
Random r = new Random(d.getTime());
for(int i=0;i<4;i++){
int a = r.nextInt(10);//取10以內(nèi)的整數(shù)[0,9]
int y = 10+r.nextInt(20); //10~30范圍內(nèi)的一個整數(shù),作為y坐標
Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
g.setColor(c);
g.drawString(""+a, 5+i*width/4, y);
}
//干擾線
for(int i=0;i<lines;i++){
Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
g.setColor(c);
g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
}
g.dispose();//類似于流中的close()帶動flush()---把數(shù)據(jù)刷到img對象當中
ImageIO.write(img, "JPG", new FileOutputStream("img/b.jpg"));
}
}
演示結(jié)果:


最后來看一個可以旋轉(zhuǎn)和放縮的驗證碼-寫到圖片本地文件中
演示代碼:
package cn.hncu.img;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Random;
import javax.imageio.ImageIO;
//該類包含一些用來查找 ImageReader 和 ImageWriter 以及執(zhí)行簡單編碼和解碼的靜態(tài)便捷方法。
import org.junit.Test;
public class ImgDemo {
@Test//可以旋轉(zhuǎn)和放縮的驗證碼
public void ImgDemo3() throws FileNotFoundException, IOException{
int width = 80;
int height = 40;
int lines = 10;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = (Graphics2D)img.getGraphics();
g2d.setFont(new Font("宋體", Font.BOLD, 20));
Random r = new Random(new Date().getTime());
//設(shè)置背景色
g2d.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g2d.drawRect(0, 0, width, height);//繪制指定矩形的邊框。
g2d.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g2d.fillRect(0, 0, width, height);//填充指定的矩形。
for(int i=0;i<4;i++){
String str = ""+r.nextInt(10);
//處理旋轉(zhuǎn)
AffineTransform Tx = new AffineTransform();
Tx.rotate(Math.random(), 5+i*15, height-5);
//用弧度測量的旋轉(zhuǎn)角度,旋轉(zhuǎn)錨點的 X 坐標,旋轉(zhuǎn)錨點的 Y 坐標
//Tx.scale(0.7+Math.random(), 0.7+Math.random());
//x坐標方向的縮放倍數(shù),y坐標方向的縮放倍數(shù)
g2d.setTransform(Tx);
Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
g2d.setColor(c);
g2d.drawString(str, 2+i*width/4, height-13);
}
//干擾線
for(int i=0;i<lines;i++){
Color c = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
g2d.setColor(c);
g2d.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
}
g2d.dispose();
ImageIO.write(img, "JPG", new FileOutputStream("img/c.jpg"));
}
}
演示結(jié)果:


下面就要開始演示前臺的圖片驗證技術(shù)了。
前臺的圖片驗證技術(shù)
這個項目的結(jié)構(gòu)圖:

index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function changImg(){
var img = document.getElementById("servletImg");
var d = new Date();
var time = d.getTime();//如果沒有這個
//下面這一句不會起作用,因為瀏覽器的緩存技術(shù),圖片并不會刷新
//img.src="/myHelloWeb/servlet/ImageServlet";
img.src="/myHelloWeb/servlet/ImageServlet?"+time;
//?號后面的東西是通過get方式傳遞的
}
</script>
</head>
<body>
這是我的手動主頁!
<br/>
<img id="servletImg" src="/myHelloWeb/servlet/ImageServlet" /><a href="javascript:changImg()">看不清</a>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ImageServlet</servlet-name> <servlet-class>cn.hncu.img.ImageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ImageServlet</servlet-name> <url-pattern>/servlet/ImageServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
ImageServlet.java
package cn.hncu.img;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ImageServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//告訴客戶端,輸出的格式
response.setContentType("image/jpeg");
int width = 80;
int height = 40;
int lines = 10;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
//設(shè)置背景色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
//設(shè)置字體
g.setFont(new Font("宋體", Font.BOLD, 20));
//隨機數(shù)字
Random r = new Random(new Date().getTime());
for(int i=0;i<4;i++){
int a = r.nextInt(10);
int y = 10+r.nextInt(20);//10~30范圍內(nèi)的一個整數(shù),作為y坐標
Color c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
g.setColor(c);
g.drawString(""+a, 5+i*width/4, y);
}
//干擾線
for(int i=0;i<lines;i++){
Color c = new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
g.setColor(c);
g.drawLine(r.nextInt(width), r.nextInt(height), r.nextInt(width), r.nextInt(height));
}
g.dispose();//類似于流中的close()帶動flush()---把數(shù)據(jù)刷到img對象當中
ImageIO.write(img, "JPG", response.getOutputStream());
}
}
演示結(jié)果:

下面這個是在index.jsp中:
如果是用這句:
img.src=”/myHelloWeb/servlet/ImageServlet”;
大家可以看看響應(yīng)頭:

再看看用這句的響應(yīng)頭:
img.src=”/myHelloWeb/servlet/ImageServlet?”+time;

多了個Date響應(yīng)!
因為時間一直在變,所以每次點看不清,都會再向服務(wù)器請求一次,而不會因為瀏覽器的緩存,而不去請求了。
驗證碼就先到這里結(jié)束啦。
更多關(guān)于驗證碼的文章請點擊查看:《java驗證碼》
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java算法實現(xiàn)調(diào)整數(shù)組順序使奇數(shù)位于偶數(shù)之前的講解
今天小編就為大家分享一篇關(guān)于Java算法實現(xiàn)調(diào)整數(shù)組順序使奇數(shù)位于偶數(shù)之前的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
SpringBoot使用YML文件進行多環(huán)境配置的三種方法
SpringBoot通過其靈活的配置機制,使得在不同環(huán)境中管理應(yīng)用設(shè)置變得簡單,尤其是使用YAML文件進行配置,它提供了一種簡潔、易讀的方式來定義應(yīng)用的配置,本文將探討在SpringBoot中使用YAML文件進行多環(huán)境配置的三種方法,需要的朋友可以參考下2024-04-04

