Display of graphic verification codes in linux

Source: Internet
Author: User
Linux Verification Code display problems-general Linux technology-Linux technology and application information. The following is a detailed description. I found that many people asked this question, and I also found the answer from the Internet. Share the following with you:

Solution 1:

Add-Djava. awt. headless = true to the java startup parameter. Taking tomcat as an example, you can find catalina. sh in the bin directory of tomcat. After opening, we can see that there are many places where-D is the startup parameter. Add-Djava. awt. headless = true to all branches. The following is an example code of catalina. sh:

-Djava. endorsed. dirs = "$ JAVA_ENDORSED_DIRS"-classpath "$ CLASSPATH "\

-Sourcepath "$ CATALINA_HOME"/.../jakarta-tomcat-catalina/src/share \

-Djava. security. manager \

-Djava. security. policy = "$ CATALINA_BASE"/conf/catalina. policy \

-Dcatalina. base = "$ CATALINA_BASE "\

-Dcatalina. home = "$ CATALINA_HOME "\

-Djava. awt. headless = true \

-Djava. io. tmpdir = "$ CATALINA_TMPDIR "\

Org. apache. catalina. startup. Bootstrap "$ @" start

All you need to do is add the rough question line.


Still not. Try again.



Package com. genius. util;

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. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import javax. servlet. http. HttpSession;

Import org. springframework. web. servlet. ModelAndView;
Import org. springframework. web. servlet. mvc. Controller;

Public final class ValidateImage implements Controller {
Public ModelAndView handleRequest (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request. getSession ();
// Create an image in memory
Int iWidth = 70, iHeight = 16;
BufferedImage image = new BufferedImage (iWidth, iHeight,
BufferedImage. TYPE_INT_RGB );
// Obtain the image Context
Graphics g = image. getGraphics ();
// Set the background color
G. setColor (Color. white );
G. fillRect (0, 0, iWidth, iHeight );
// Draw a border
G. setColor (Color. black );
G. drawRect (0, 0, iWidth-1, iHeight-1 );
// Obtain the random ID code (4 digits)
String rand = request. getParameter ("rand ");
Rand = rand. substring (0, rand. indexOf ("."));
Switch (rand. length ()){
Case 1:
Rand = "000" + rand;
Break;
Case 2:
Rand = "00" + rand;
Break;
Case 3:
Rand = "0" + rand;
Break;
Default:
Rand = rand. substring (0, 4 );
Break;
}
// Save the authentication code to the SESSION
Session. setAttribute ("rand", rand );
// Display the authentication code to the image
G. setColor (Color. black );
G. setFont (new Font ("Times New Roman", Font. PLAIN, 18 ));
G. drawString (rand, 10, 15 );
// Generates 200 random interference points, making the authentication code in the image hard to be detected by other programs.
Random random = new Random ();
For (int iIndex = 0; iIndex <200; iIndex ++ ){
Int x = random. nextInt (iWidth );
Int y = random. nextInt (iHeight );
G. drawLine (x, y, x, y );
}
// The image takes effect
G. dispose ();
// Output the image to the page
ImageIO. write (image, "JPEG", response. getOutputStream ());

Return new ModelAndView ();
}
}


Solution 2: I changed it


Package com. genius. util;

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. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import javax. servlet. http. HttpSession;

Import org. springframework. web. servlet. ModelAndView;
Import org. springframework. web. servlet. mvc. Controller;

Import com.sun.image.codec.jpeg. Unzip codec;
Import com.sun.image.codec.jpeg. encode imageencoder;

Public final class ValidateImage implements Controller {
Public ModelAndView handleRequest (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request. getSession ();
// Create an image in memory
Int iWidth = 70, iHeight = 16;
BufferedImage image = new BufferedImage (iWidth, iHeight,
BufferedImage. TYPE_INT_RGB );
// Obtain the image Context
Graphics g = image. getGraphics ();
// Set the background color
G. setColor (Color. white );
G. fillRect (0, 0, iWidth, iHeight );
// Draw a border
G. setColor (Color. black );
G. drawRect (0, 0, iWidth-1, iHeight-1 );
// Obtain the random ID code (4 digits)
String rand = request. getParameter ("rand ");
Rand = rand. substring (0, rand. indexOf ("."));
Switch (rand. length ()){
Case 1:
Rand = "000" + rand;
Break;
Case 2:
Rand = "00" + rand;
Break;
Case 3:
Rand = "0" + rand;
Break;
Default:
Rand = rand. substring (0, 4 );
Break;
}
// Save the authentication code to the SESSION
Session. setAttribute ("rand", rand );
// Display the authentication code to the image
G. setColor (Color. black );
G. setFont (new Font ("Times New Roman", Font. PLAIN, 18 ));
G. drawString (rand, 10, 15 );
// Generates 200 random interference points, making the authentication code in the image hard to be detected by other programs.
Random random = new Random ();
For (int iIndex = 0; iIndex <200; iIndex ++ ){
Int x = random. nextInt (iWidth );
Int y = random. nextInt (iHeight );
G. drawLine (x, y, x, y );
}
// The image takes effect
G. dispose ();
// Output the image to the page
Required imageencoder encoder = response codec. createJPEGEncoder (response. getOutputStream ());
Encoder. encode (image );

Return new ModelAndView ();
}
}
Modified successfully ,,,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.