JCaptcha是非常強大的,不光是可以生成圖片式的驗證碼,還可以生成聲音式的(新浪就使用了雙重驗證碼)。本文簡單的介紹了JCaptcha庫以及使用實例,下面一起來看看。
下載JCaptcha庫
maven依賴如此添加:
1
2
3
4
5
|
<dependency> <groupId>com.octo.captcha</groupId> <artifactId>jcaptcha</artifactId> <version> 1.0 </version> </dependency> |
封裝了一個簡單的類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator; import com.octo.captcha.component.image.backgroundgenerator.FileReaderRandomBackgroundGenerator; import com.octo.captcha.component.image.color.RandomListColorGenerator; import com.octo.captcha.component.image.fontgenerator.FontGenerator; import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator; import com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster; import com.octo.captcha.component.image.textpaster.TextPaster; import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator; import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage; import com.octo.captcha.component.image.wordtoimage.WordToImage; import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator; import com.octo.captcha.component.word.wordgenerator.WordGenerator; import com.octo.captcha.engine.CaptchaEngine; import com.octo.captcha.engine.image.ListImageCaptchaEngine; import com.octo.captcha.image.gimpy.GimpyFactory; import java.awt.*; /** * 產(chǎn)生驗證碼圖片的類 */ public class CapchaHelper { private static final Integer MIN_WORD_LENGTH = 4 ; // 驗證碼最小長度 private static final Integer MAX_WORD_LENGTH = 4 ; // 驗證碼最大長度 private static final Integer IMAGE_HEIGHT = 30 ; // 驗證碼圖片高度 private static final Integer IMAGE_WIDTH = 130 ; // 驗證碼圖片寬度 private static final Integer MIN_FONT_SIZE = 15 ; // 驗證碼最小字體 private static final Integer MAX_FONT_SIZE = 15 ; // 驗證碼最大字體 private static final String RANDOM_WORD = "0123456789" ; // 隨機字符 // 驗證碼隨機字體 private static final Font[] RANDOM_FONT = new Font[]{ new Font( "nyala" , Font.BOLD, MIN_FONT_SIZE), new Font( "Arial" , Font.BOLD, MIN_FONT_SIZE), new Font( "Bell MT" , Font.BOLD, MIN_FONT_SIZE), new Font( "Credit valley" , Font.BOLD, MIN_FONT_SIZE), new Font( "Impact" , Font.BOLD, MIN_FONT_SIZE) }; // 驗證碼隨機顏色 private static final Color[] RANDOM_COLOR = new Color[]{ new Color( 255 , 255 , 255 ), new Color( 255 , 220 , 220 ), new Color( 220 , 255 , 255 ), new Color( 220 , 220 , 255 ), new Color( 255 , 255 , 220 ), new Color( 220 , 255 , 220 ) }; private static ListImageCaptchaEngine captchaEngine; public static CaptchaEngine getCaptchaEngine( final String imgPath) { if (captchaEngine == null ) { synchronized (CapchaHelper. class ) { if (captchaEngine == null && imgPath != null ) { captchaEngine = new ListImageCaptchaEngine() { @Override protected void buildInitialFactories() { RandomListColorGenerator randomListColorGenerator = new RandomListColorGenerator(RANDOM_COLOR); BackgroundGenerator backgroundGenerator = new FileReaderRandomBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT, imgPath); WordGenerator wordGenerator = new RandomWordGenerator(RANDOM_WORD); FontGenerator fontGenerator = new RandomFontGenerator(MIN_FONT_SIZE, MAX_FONT_SIZE, RANDOM_FONT); TextDecorator[] textDecorator = new TextDecorator[]{}; TextPaster textPaster = new DecoratedRandomTextPaster(MIN_WORD_LENGTH, MAX_WORD_LENGTH, randomListColorGenerator, textDecorator); WordToImage wordToImage = new ComposedWordToImage(fontGenerator, backgroundGenerator, textPaster); addFactory( new GimpyFactory(wordGenerator, wordToImage)); } }; } } } return captchaEngine; } } |
響應(yīng)網(wǎng)頁中對驗正碼圖像的請求
可以定義一個servlet
響應(yīng)這個請求,如果用springMVC
,也可以用某個Controller
中的某個方法響應(yīng)這個請求,不管怎樣,都需要指定一個路徑對應(yīng)到servlet或controller
的方法,比如路徑是:”/aaa/captcha”
那么在響應(yīng)對這個路徑的請求的Servlet中可以這樣寫:
1
2
3
4
5
6
7
8
9
10
11
|
//獲取驗證碼背景圖片的路徑,這路徑放了很多作為背景的圖像 String captcha_backgrounds = session.getServletContext().getRealPath( "/WEB-INF/img/captcha" ); CaptchaEngine ce = CapchaHelper.getCaptchaEngine(captcha_backgrounds); //需要admin網(wǎng)頁中用js定時從服務(wù)端獲取當前的驗證碼 Captcha captcha = ce.getNextCaptcha(); //為了驗證,把captcha對象放到session中,以在客戶端提交驗證碼時進行驗證 req.getSession().setAttribute( "captcha" , captcha); //獲取驗證碼圖片,這是未壓縮的位圖 BufferedImage image = (BufferedImage) captcha.getChallenge(); resp.setContentType( "image/jpeg" ); ImageIO.write(image, "jpg" , resp.getOutputStream()); |
如果用springMVC,就這樣寫:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//獲取驗證碼背景圖片的路徑,這路徑放了很多作為背景的圖像 String captcha_backgrounds = session.getServletContext().getRealPath( "/WEB-INF/img/captcha" ); CaptchaEngine ce = CapchaHelper.getCaptchaEngine(captcha_backgrounds); //需要admin網(wǎng)頁中用js定時從服務(wù)端獲取當前的驗證碼 Captcha captcha = ce.getNextCaptcha(); //為了驗證,把captcha對象放到session中,以在客戶端提交驗證碼時進行驗證 session.setAttribute( "captcha" , captcha); //獲取驗證碼圖片,這是未壓縮的位圖 BufferedImage image = (BufferedImage) captcha.getChallenge(); ByteArrayOutputStream bao= new ByteArrayOutputStream(); //應(yīng)縮成jpg并寫到輸出流中 ImageIO.write(image, "jpg" , bao); return bao.toByteArray(); |
這兩種方式,向客戶端返回的都是二進制數(shù)據(jù)。
String captcha_backgrounds = session.getServletContext().getRealPath(“/WEB-INF/img/captcha”);
表示路徑/WEB-INF/img/captcha
下面放的是作為驗證碼圖像的背景的多張圖片,必須是jpeg,大小可能沒限制,你可以自己試一下。
網(wǎng)頁中用 <IMG> 指向這個地址
1
|
< img id = "captcha" src = "/captcha_img" onclick = "refreshCaptchaImg()" /> |
js函數(shù)refreshCaptchaImg()
響應(yīng)圖片的點擊,每點擊一次,就重新獲取一張新的驗證碼圖片。如何重新獲取驗正碼圖片呢?
只需改變img的src屬性,但這里是每次都是用同一個地址來設(shè)置這個屬性,這樣不會引起真正的刷新,所以方法refreshCaptchaImg()
是這樣實現(xiàn)的:
1
2
3
4
5
|
function refreshCaptchaImg() { //從服務(wù)端重新下載驗證碼圖片 //給這個地加參數(shù)純?yōu)榱藦娭扑⑿拢駝t由于src指向的url地址沒變,瀏覽器不會真正生刷新圖片 var now = new Date() $( "#captcha" ).attr( "src" , "/captcha_img?" +now.getTime()); |
以上就是Java中驗證碼生成庫JCaptcha的介紹及使用,希望對大家學(xué)習(xí)java有所幫助。