Java Verification Code implementation

Source: Internet
Author: User

The role of verification code is not much to say directly to see the effect of it



Package Com.shangdi.naxincameraserver.webpage;import Java.awt.color;import Java.awt.font;import java.awt.Graphics; Import Java.awt.graphics2d;import java.awt.image.bufferedimage;import java.io.ioexception;import java.util.Random; Import Javax.imageio.imageio;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpsession;import Org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;@ Controller@requestmapping ("/verfycode") public class Verifycode {public static final int width = 165;//The width of the generated picture public STA Tic Final int height = 36;//the height of the resulting picture @requestmapping (value= "/code") public void Getverifycode (HttpServletRequest request , httpservletresponse response) {//Set the response header to notify the browser to open the Response.setcontenttype ("Image/jpeg") as a picture;// Equivalent to Response.setheader ("Content-type",//"image/jpeg");//Set Response header control browser do not cache Response.setdateheader ("Expries",-1); Response.setheader ("Cache-control", "No-cache"); response.seTheader ("Pragma", "No-cache");//1. Create a picture in memory bufferedimage image = New BufferedImage (WIDTH, Height,bufferedimage.type _INT_RGB);//2. The virtual brush gets the picture graphics G = image.getgraphics ();//3. Set the background color of the picture setbackground (g);//4. Set the border of the picture SetBorder (g);//5. Draw an interference line drawrandomline (g) on the picture;//6. Write the random number on the picture string random = Drawrandomnum ((graphics2d) g);//Generate a combination of numbers and letters with the CAPTCHA image//7. The random number exists in the session HttpSession Session=request.getsession (TRUE); Session.setattribute ("Checkcode", random);// Within two minutes of expiration session.setmaxinactiveinterval (2*60);//8. Write the picture to the browser try {imageio.write (image, "jpg", Response.getoutputstream ());} catch (IOException e) {e.printstacktrace ();}}  /** * Set the background color of the picture * * @param g */private void SetBackground (Graphics g) {//Set color G.setcolor (color.white);//Fill area g.fillrect (0, 0, WIDTH, HEIGHT);}  /** * Set the border of the picture * * @param g */private void SetBorder (Graphics g) {//Set Border color G.setcolor (color.white);//Border area g.drawrect (1, 1, WIDTH-2, HEIGHT-2);} /** * Draw random lines on the picture * * @param g */private void Drawrandomline (Graphics g) {//Set color//g.setcolor (color.greEN); Random r=new random ();//Set the number of lines and draw lines for (int i = 0; i <; i++) {int x1 = r.nextint (WIDTH); int y1 = R.nextint (HEIGHT); T x2 = r.nextint (WIDTH), int y2 = R.nextint (HEIGHT), G.drawline (x1, y1, x2, y2),//Set interference line color, colour color = new Color (+ R.next Int (R.nextint), + + (+), + R.nextint (()); G.setcolor (color);}} /** * Draw Random characters * * @param g */private String drawrandomnum (graphics2d g) {//Set color G.setcolor (color.red);//Set Font G.setfont (new Font ("Arial", Font.Bold, 30));//combination of numbers and letters string basenumletter = " Aa0bb1ccdd3eeff5gg6hhjjkkll7mmn9noopp8qqrrss2ttuuvv4wwxxyyzz "; return Createrandomchar (G, baseNumLetter);} /** * Create random characters * * @param g * @param Basechar * @return Random characters */private string Createrandomchar (graphics2d g, String Basecha R) {StringBuffer sb = new StringBuffer (); Random r=new random (); int x = 5; String ch = "";//control word for (int i = 0; I < 4; i++) {//Set font rotation angle int degree = r.nextint ()% 30;ch = Basechar.charat (new random (). Nextint (Basechar.length ())) + "";//Set random number Colour color = new ColoR (+ r.nextint), + r.nextint (+), + r.nextint (210)); G.setcolor (color); Sb.append (CH);//Positive angle g.rotate (degree * math.pi/180, x, +); g.drawstring (CH, x, 20);//Reverse Angle g.rotate (-degree * MATH.PI/1 x + + 30;} return sb.tostring ();}}

Page Show

verfycode/code" id= "Valcode" onclick= "Refresh (this, ' <%=basepath%>verfycode/ Code ') "><script type=" Text/javascript ">function Refresh (Thisobj,path) {//math.random (); Prevent client browsers from using cache thisobj.src=path+ "?" +math.random ();} </script>


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Verification Code implementation

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.