Java Graphics Verification Code

Source: Internet
Author: User
Tags rand set background

JSP that generated the verification code:
1<%@ page contenttype= "image/jpeg" language= "Java"Import= "java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*" pageencoding= "Utf-8"%>2 3<%!4Color Getrandcolor (intFcintBC) {5Random random =NewRandom ();6     if(FC > 255){7FC = 255;8     }9     if(BC < 255){TenBC = 255; One     } A     intr = FC +random.nextint (bc-FC); -     intg = FC +random.nextint (bc-FC); -     intb = FC +random.nextint (bc-FC); the  -  -     return NewColor (r,g,b); -   } +%> -  +<% A   //Settings page does not cache atResponse.setheader ("Pragma", "No-cache"); -Response.setheader ("Cache-control", "No-catch"); -Response.setdateheader ("Expires", 0); -  -   //Create an image in memory -   intwidth = 60; in   intHeight = 42; -BufferedImage image =NewBufferedImage (WIDTH,HEIGHT,BUFFEREDIMAGE.TYPE_INT_RGB); to  +   //Creating Images -Graphics g =image.getgraphics (); the   //Generating Random objects *Random random =NewRandom (); $   //Set Background colorPanax NotoginsengG.setcolor (Getrandcolor (200,250)); -G.fillrect (0,0, width,height); the   //Set Font +G.setfont (NewFont ("Tines Nev Roman", font.plain,24)); A   //randomly generated interference lines the +G.setcolor (Getrandcolor (160,200)); -    for(inti = 0; I < 255; i++){ $     intx =random.nextint (width); $     inty =random.nextint (height); -     intXL = Random.nextint (12); -     intYL = Random.nextint (12); theG.drawline (x, Y, X+XL, y+yl); -   }WuyiG.setcolor (Getrandcolor (65,105)); the    for(inti = 0; I < 4; i++){ -     intx =random.nextint (width); Wu     inty =random.nextint (height); -     intXL = Random.nextint (12); About     intYL = Random.nextint (12); $G.drawline (x, Y, X+XL, y+yl); -   } -  -   //randomly generated authentication code, 4 digits AString SRand = ""; +    for(inti = 0; I < 4; i++){ theString rand = string.valueof (Random.nextint (10)); -SRand + =Rand; $     //Display the authentication code to the image theG.setcolor (NewColor (+ random.nextint, + random.nextint), + random.nextint (110))); theg.DrawString (Rand,13*i+6,25+random.nextint (10)); the   } theSession.setattribute ("RCode", SRand); -   //Image Effective in g.dispose (); the   //output image to page theImageio.write (Image, "JPEG", Response.getoutputstream ()); About out.clear (); theout =pagecontext.pushbody (); the%>
Front Reference:
<id= "Cknumber"  src= "Loginaction!getnumber.iol"/>  <type= "text"  name= "Code"  class  = "Ta_login_inputneed_notice"/>
Click on the image to refresh the verification code:

Principle: Use jquery to modify the SRC attribute of img, Note: src should be different from before .

Method: Randomly generate a parameter after the link so that the link is not the same as before, I am here by time to generate.

                <script>                    $ (document). Ready (function  () {                        $ ("#ckNumber"). Click (  function  () {                            varnew Date (). GetTime () + ' a ' + math.random ();                            $ (this). attr (' src ', $ (this). attr (' src ') + append)                    })                 </script>
Background verification (for reference only):
if (!request.getparameter ("code"). Equals ( this

Java Graphics Verification Code

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.