2012-07-16 40 views
5

我需要在java swing應用程序中添加驗證碼驗證程序。我一直在尋找一些圖書館(JCaptcha和SimpleCatcha),但他們是爲了網絡開發。如何在java swing應用程序中顯示驗證碼

有任何圖書館使用captcha在鞦韆上?如果它不是,是否有一個網頁或存儲庫與一些驗證碼字符來實現我自己的驗證碼?

我真的很感謝你的時間和你的幫助。

在此先感謝。

+4

好問題 - 我發現這個其他類似的問題,其中約JCaptcha的答案會談:http://stackoverflow.com/questions/6219029/captcha-library-for-swing - 我會問我什麼在一個搖擺應用程序中的驗證碼的目的是? – ametren 2012-07-16 18:57:24

+1

@ametren:如果Swing應用程序是Web應用程序的前端,我不明白爲什麼使用CAPTCHA不如瀏覽器那麼有用。 – 2012-07-16 19:04:28

+0

@JBNizet Ya,我可以看到這一點,但它似乎只是爲我提出了更多的問題 - 比如說,爲什麼你會用一個swing應用程序前端一個web應用程序?我並不是試圖說出提問者,只是試圖理解用例。 – ametren 2012-07-16 19:11:38

回答

4

JCaptcha可以返回一個BufferedImage。從那裏,它沒有太多難以用一個JLabel來獲得圖像可見:

BufferedImage captcha = // Get the captcha 
// See also com.octo.captcha.service.image.AbstractManageableImageCaptchaService.getImageChallengeForID(String) 
JLabel label = new JLabel(new ImageIcon(captcha)); 
// ... add that label to a visible container of your Swing application 

在1.0版本中,您可以使用此:http://jcaptcha.sourceforge.net/apidocs/1.0/com/octo/captcha/service/image/AbstractManageableImageCaptchaService.html

在2.0-α1,有是這樣的:http://jcaptcha.sourceforge.net/apidocs/2.0-alpha1/com/octo/captcha/service/image/AbstractManageableImageCaptchaService.html#getImageChallengeForID(java.lang.String)

您還可以使用額外的Locale參數來檢查這些方法的重載版本。

在每種情況下,都有一個默認實現類DefaultManageableImageCaptchaService

+0

非常感謝!我要去嘗試一下,我會發布我是如何做到的。 – Herman 2012-07-17 04:25:46

+0

嗨Guillaume,我試過你的答案,工作得很好,我真的很感謝你的幫助,只有我不得不改變方式把標籤中的驗證碼圖像:BufferedImage imgCaptcha = servicioCaptcha.getImageChallengeForID(「」+ id); – Herman 2012-07-17 05:59:06

+0

謝謝!它的工作原理,所以只驗證一個使用DefaultManageableImageCaptchaService :: validateResponseForID(「」+ id,this.txtCaptcha.getText());謝謝我真的很感謝你的幫助 – Herman 2012-07-17 06:00:18

0
BufferedImage captcha = // Get the captcha 

// See also 
com.octo.captcha.service.image.AbstractManageableImageCaptchaService.getImageChallengeForID(String) 

JLabel label = new JLabel(new ImageIcon(captcha)); 
// ... add that label to a visible container of your Swing application 
相關問題