JCaptcha is very powerful. It can not only generate image-based verification codes, but also sound-based verification codes (Sina uses two-fold verification codes). This article briefly introduces the JCaptcha library and usage examples, let’s take a look at it below.
Download the JCaptcha library
The maven dependency is added like this:
<dependency> <groupId></groupId> <artifactId>jcaptcha</artifactId> <version>1.0</version> </dependency>
Encapsulated a simple class
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import .*; /** * Class that generates verification code pictures */ public class CapchaHelper { private static final Integer MIN_WORD_LENGTH = 4;// Minimum length of verification code private static final Integer MAX_WORD_LENGTH = 4;// Maximum length of verification code private static final Integer IMAGE_HEIGHT = 30;// Verification code image height private static final Integer IMAGE_WIDTH = 130;// Verification code image width private static final Integer MIN_FONT_SIZE = 15;// Minimum font for verification code private static final Integer MAX_FONT_SIZE = 15;// The maximum font of verification code private static final String RANDOM_WORD = "0123456789";// Random characters // Random font of verification code private static final Font[] RANDOM_FONT = new Font[]{ new Font("nyala", , MIN_FONT_SIZE), new Font("Arial", , MIN_FONT_SIZE), new Font("Bell MT", , MIN_FONT_SIZE), new Font("Credit valley", , MIN_FONT_SIZE), new Font("Impact", , MIN_FONT_SIZE) }; // Random color of verification code 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 () { 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; } }
Respond to a request for a correct code image in the web page
Can define aservlet
Respond to this request, ifspringMVC
, you can also use a certain oneController
A method in response to this request, no matter what, it needs to specify a path corresponding toservlet or controller
The method, for example, the path is:”/aaa/captcha”
Then inServlet that responds to requests for this pathYou can write this way:
//Get the path to obtain the background image of the verification code, this path places a lot of images as background String captcha_backgrounds = ().getRealPath("/WEB-INF/img/captcha"); CaptchaEngine ce = (captcha_backgrounds); //You need to use js to obtain the current verification code from the server regularly in the admin web page Captcha captcha = (); //For verification, put the captcha object into the session to verify when the client submits the verification code ().setAttribute("captcha", captcha); //Get the verification code picture, this is an uncompressed bitmap BufferedImage image = (BufferedImage) (); ("image/jpeg"); (image, "jpg", ());
If springMVC is used, just write:
//Get the path to obtain the background image of the verification code, this path places a lot of images as background String captcha_backgrounds = ().getRealPath("/WEB-INF/img/captcha"); CaptchaEngine ce = (captcha_backgrounds); //You need to use js to obtain the current verification code from the server regularly in the admin web page Captcha captcha = (); //For verification, put the captcha object into the session to verify when the client submits the verification code ("captcha", captcha); //Get the verification code picture, this is an uncompressed bitmap BufferedImage image = (BufferedImage) (); ByteArrayOutputStream bao=new ByteArrayOutputStream(); //It should be condensed into jpg and written to the output stream (image, "jpg", bao); return ();
In both ways, the returned binary data to the client.
String captcha_backgrounds = ().getRealPath(“/WEB-INF/img/captcha”);
Indicates the path/WEB-INF/img/captcha
Below are multiple pictures as the background of the verification code image, which must bejpeg, there may be no limit on the size, you can try it yourself.
Use <IMG> to point to this address in the web page
<img src="/captcha_img" onclick="refreshCaptchaImg()" />
js functionrefreshCaptchaImg()
In response to the click of the image, a new verification code image will be obtained again for each click. How to re-acquire the correct code image?
Just change the src attribute of img, but here we use the same address to set this attribute every time, so that it will not cause real refresh, so the methodrefreshCaptchaImg()
This is achieved:
function refreshCaptchaImg() { //Re-download the verification code picture from the server side //Add parameters to this place purely for forced refresh, otherwise, since the URL address pointed to by src has not changed, the browser will not really refresh the picture automatically because the browser will not refresh the image automatically because the URL address pointed to by src has not changed. var now = new Date() $("#captcha").attr("src","/captcha_img?"+());
The above is an introduction and use of JCaptcha, a verification code generation library in Java. I hope it will be helpful to everyone to learn Java.