Java production image Verification Code

Source: Internet
Author: User

[Java] import javax. servlet. servletException; import javax. servlet. servletOutputStream; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. http. httpSession; import org. apache. log4j. logger; import com. ailk. bboss. constants. constants; import com. ailk. bboss. util. imageCode; import com.sun.image.codec.jp Eg. export codec; import com.sun.image.codec.jpeg. JPEGImageEncoder;/*** generate random Verification Code */www.2cto. compublic class ValidateCodeServlet extends HttpServlet {private static final long serialVersionUID =-protected; protected void service (HttpServletRequest req, HttpServletResponse response) throws ServletException, java. io. IOException {HttpSession session = req. getSession (); ImageCode image = ne W ImageCode (); // Save the four-digit verification code to the Session. // Disable image caching. Response. setHeader ("Pragma", "no-cache"); response. setHeader ("Cache-Control", "no-cache"); response. setDateHeader ("Expires", 0); response. setContentType ("image/jpeg"); // output the image to the Servlet output stream. ServletOutputStream outStream = response. getOutputStream (); required imageencoder encoder = required codec. createJPEGEncoder (outStream); encoder. encode (image. creatImage (); outStream. close (); session. setAttribute (Constants. VALIDATE_CODE, image. getStrRand (); Logger. getLogger (this. getClass ()). info ("image code =" + image. getStrRand () + "cc") ;}} [java] <pre name = "code" class = "java"> import java. awt. color; import java. awt. font; import java. awt. graphics; import java. awt. geom. affineTransform; import java. awt. image. bufferedImage; import java. util. random; public class ImageCode {private String strRand = ""; public final Color [] defCols = {Color. WHITE, Color. BLACK}; private static String [] CHARS = getChars (); public final Font [] defFonts = {new Font ("", Font. BOLD, 18), new Font ("", Font. BOLD, 18), new Font ("", Font. BOLD, 18), new Font ("", Font. BOLD, 18), new Font ("rom7", Font. BOLD, 18)}; private Font getFont (int I) {Random random = new Random (new Double (Math. random () * 10000 * (I + 1 )). longValue (); int a = random. nextInt (defFonts. length); AffineTransform trans = new AffineTransform (); trans. scale (1 + random. nextFloat () * 0.2, 0.95 + random. nextFloat () * 0.65); return defFonts [a]. deriveFont (trans);} public BufferedImage creatImage () {int width = 75, height = 20; BufferedImage image = new BufferedImage (width, height, BufferedImage. TYPE_INT_RGB); Graphics g = image. getGraphics (); Random random = new Random (); g. setColor (new Color (202,255,255); g. fillRect (0, 0, width, height); String arr [] = CHARS; Random rm = new Random (new Double (Math. random () * 10000*59 ). longValue (); g. setColor (Color. BLACK); for (int I = 0; I <4; I ++) {String rand = arr [rm. nextInt (arr. length)]; strRand + = rand; g. setFont (getFont (I); if (I = 0) g. drawString (rand, 2 + random. nextInt (2), 20); else g. drawString (rand, 16 * I + 2 + random. nextInt (3), 20);} g. dispose (); return image;} public String getStrRand () {return strRand;} public void setStrRand (String strRand) {this. strRand = strRand;} www.2cto.com private static String [] getChars () {String [] chars = null; chars = new String [] {"2", "3 ", "4", "5", "6", "7", "8", "9", "A", "B", "D", "E ", "F", "G", "H", "J", "K", "L", "M", "N", "P", "R ", "S", "U", "V", "X", "Y", "Z"}; return chars ;}} </pre> <br> <pre> </pre>

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.