Java Picture Verification code includes the rotation of text and pictures

Source: Internet
Author: User

Java Picture Verification code includes the rotation of the text picture:

This example shows a two-digit plus and minus operation, which requires more ways to get it through its own modifications:

Or there are two other ways to choose from the resources I upload: http://download.csdn.net/detail/huitoukest/8043711

Package Com.utils;import Java.awt.color;import java.awt.font;import java.awt.graphics;import Java.awt.Graphics2D; Import Java.awt.renderinghints;import Java.awt.image.bufferedimage;import Java.util.random;import Javax.swing.imageicon;import Javax.swing.jframe;import Javax.swing.jlabel;public class VerificationCode {private int    result2=0; Random rand = new Random ();/** * Randomly generated addend and addend */private int jiashu=0;private int beijiashu=0;/** * Randomly generated calculation method, 0 for Plus, 1 for minus */PR      ivate int js=0;    Private char[] aa={' 0 ', ' one ', ' II ', ' three ', ' establishments ', ' Wu ', ' Lu ', ' qi ', ' ba ', ' JIU '};    Private char[] bb={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};    Private char[] cc={' 0 ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ', ' seven ', ' eight ', ' nine '};    Private char[] action={' plus ', ' minus ', ' + ', '-'}; Private char[] jieguo={' et ', ' yes '};p ublic static void Main (string[] args) {//TODO auto-generated method Stubverificationima GE code=new verificationimage ();          JFrame jframe=new JFrame ();                    Jframe.setbounds (400, 400, 250, 250); ImageIcon img = new ImageIcon (cOde.getverificationcode2 ());                    JLabel background = new JLabel (IMG);          Jframe.add (background);          Jframe.setvisible (TRUE); Jframe.setdefaultcloseoperation (Jframe.exit_on_close);        }/** * The second type of verification code, two-digit addition and subtraction * @return a new captcha image */public BufferedImage getVerificationCode2 () {int width=150;        int height=70; The int degree=0;//continues the angle of rotation altogether, conveniently rotates back at the last moment bufferedimage image = new BufferedImage (width, height, Buffered         IMAGE.TYPE_INT_RGB);         Get graphic context Graphics G = image.getgraphics ();        Set Background color color Background=getcolor ();        G.setcolor (background);         G.fillrect (0, 0, width, height);        Draw Border G.setcolor (background);         G.drawrect (0,0,width-1,height-1);        Display the authentication code in the image, if you want to generate more bits of the authentication Code char[] Content=getdrawcontent2 ();        Int[] Xs=getradonwidths (content.length);        Int[] Ys=getradomheights (content.length);    for (int i=0;i<content.length;i++)    {String s=content[i]+ "";                   if (content[i]== '! ')                   S= "";                   If the picture is rotated before stooped (i!=2) {int maxdegree=rand.nextint (2);                   if (maxdegree==0) maxdegree=0;           else maxdegree=305;                       Degree=rand.nextint (+maxdegree);            } else degree=0;             G.setcolor (GetColor ());            The IF (i==2)//operation symbol shows a larger number of G.setfont (New Font ("Atlantic Inline", font.plain,24));                      else G.setfont (New Font ("Atlantic Inline", font.plain,18));        Rotatestring (S, Xs[i], ys[i], G, degree);               }//Draw disturbance point createrandompoint (width, height, g);                Randomly draw several lines createrandomline (width, height,4,g);        Releases the graphics context g.dispose ();              SYSTEM.OUT.PRINTLN ("The result of the calculation is =" +getresult2 ());    return image; }/** * Rotates and draws the specified string * @param s The string that needs to be rotated * @param x-coordinate of the string * @paramY-coordinate of Y-string * @param g brush g * @param degree rotation angle */private void rotatestring (String s,int x,int y,graphics g,int degree)                                          {graphics2d g2d = (graphics2d) g.create ();                     Translating the origin to the center of the graphical environment, this method is essentially moving the string to a location g2d.translate (x-1, y+3);         Rotated text g2d.rotate (degree* math.pi/180);    It is important to note that the brush here already has the last specified position, so the specified here is actually a relative position g2d.drawstring (s,0, 0);} /** * * @param width * @param height * @param many * @param g */private void createrandompoint (int width,int height,int m             Any,graphics g) {//randomly generates a disturbance point for (int i=0;i<many;i++) {int x = rand.nextint (width);             int y = rand.nextint (height);            G.setcolor (GetColor ());         G.drawoval (x,y,1,1);  }}/** * * @param width * @param height * @param minmany Minimum quantity * @param g */private void createrandomline (int width,int Height,int Minmany,graphics g) {//randomly generate interference lines for (int i=0;i<rand.nextint (MINMAny) +5;i++ {int x1 = rand.nextint (width)%15;            int y1 = rand.nextint (height);             int x2 = (int) (Rand.nextint (width)%40+width*0.7);             int y2 = rand.nextint (height);            G.setcolor (GetColor ());        G.drawline (x1, y1, x2, y2);  }}/*** * @return randomly returns a color */private color GetColor () {int r= (int) (Math.random () *255); int g= (int) (Math.random () *255); int    b= (int) (Math.random () *255); return new Color (R,G,B); /** * * @return returns what GetVerificationCode2 needs to draw: two-digit plus subtraction character array */private char[] GetDrawContent2 () {Beijiash      u=0;    jiashu=0;    Char[] Temp=new char[6];      Char[] W =aa;              int k=0;        /** * Generate Summand *//Select a character array from AA\BB\CC as the footage k= (int) (Math.random ());        if (k==0) W=aa;        else if (k==1) w=bb;          else if (k==3) w=cc;          k= (int) (Math.random () *10);          TEMP[0]=W[K];          if (k==0) temp[0]= '! ';          beijiashu+=k*10; K= (inT) (Math.random () *10);          TEMP[1]=W[K];          Beijiashu+=k;        /** * Generate Addend *//Select a character array from AA\BB\CC as the footage k= (int) (Math.random ());        if (k==0) W=aa;        else if (k==1) w=bb;                else if (k==3) w=cc;          k= (int) (Math.random () *10);      TEMP[3]=W[K];        if (k==0) temp[3]= '! ';                 Jiashu=k*10+jiashu;          k= (int) (Math.random () *10);          TEMP[4]=W[K];          Jiashu+=k;          Choose subtraction W=action;          k= (int) (Math.random ());          TEMP[2]=W[K];          js=k%2;          Results W=jieguo;          k= (int) (Math.random ());    TEMP[5]=W[K];          System.out.println (new String (temp));    return temp;  }/** * to the picture selection, which is reserved for easy use later * @param bufferedimage * @param degree * @return a rotated picture */Public BufferedImage rolateimage (bufferedimage bufferedimage,int degree,color backGround) {BufferedImage    img int w =Bufferedimage.getwidth ();    int h = bufferedimage.getheight ();    int type = BUFFEREDIMAGE.TYPE_INT_RGB;    Graphics2D graphics2d;    GRAPHICS2D = (img = new BufferedImage (W, h, type)). CreateGraphics (); Graphics2d.setrenderinghint (Renderinghints.key_interpolation, Renderinghints.value_interpolation_bicubi    C);    Graphics2d.rotate (Math.toradians (degree), W/2, H/2);    Graphics2d.drawimage (bufferedimage,null, NULL);    return img; }/** * Get verification code GETVERIFICATIONCODE2, calculated results */public int getResult2 () {if (js==0) return (beijiashu+      Jiashu);    else if (js==1) return (Beijiashu-jiashu);    return 0; }/** * * @param many * @return when drawing a random height array */private int[] getradomheights (int many) {int[]     Temp=new Int[many];    for (int i=0;i<many;i++) {temp[i]=getradomheight ();    } return temp; }/** * * @param many * @return when drawing the starting x-coordinate array */private int[] getradonwidths (int many) {INt[] Temp=new Int[many];  for (int i=0;i<many;i++) {if (i==0) temp[i]=getradonwidth (0);  else Temp[i]=getradonwidth (temp[i-1]);       } return temp;      } private int getradomheight () {int fullheight=70;       return (int) (Math.random () *fullheight)%35+15;      } private int Getradonwidth (int minWidth) {int maxwidth=150;      int MINJIANJU=MAXWIDTH/9;      int MAXJIANJU=MAXWIDTH/6;      int Temp=maxjianju-minjianju;       A random number return (int) (Math.random () *temp) +minwidth+minjianju is produced within the prescribed range; }}


Operation Result:


Java Picture Verification code includes the rotation of text and pictures

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.