Generate a picture color verification code (with verification code rotation)

Source: Internet
Author: User
Tags sin

Generate a CAPTCHA picture generally returns the InputStream type, which can be used in any case, and can be dynamically generated by writing the returned value of InputStream to a file

The code is as follows:[Java] View plain copy package com.yxfei.tool;      import java.awt.color;   Import  java.awt.Font;   import java.awt.graphics;   import java.awt.graphics2d;    import java.awt.geom.affinetransform;   import java.awt.image.bufferedimage;    import java.io.bytearrayinputstream;   import java.io.bytearrayoutputstream;    import java.io.inputstream;   import java.util.random;      import javax.imageio.imageio;   import javax.imageio.stream.imageoutputstream;   import javax.servlet.http.httpservletrequest;         public class  verifycode {              static random  r = new random ();       public static Color  GeTrandcolor (Int min, int max)  {                       if  (min > 255)                 min = 255;            if  (max > 255)                 max = 255;            int red = r.nextint (max - min)  + min;            int green = r.nextint (max - min)  + min;            int blue = r.nextint (max -  min)  + min;           return new  Color (Red,  green, blue);       }           Public static inputstream getverifycode (int width,int height,httpservletrequest  Request)  throws Exception {           //  todo auto-generated method stub            bytearrayinputstream inputstream = null;            bufferedimage image = new bufferedimage (width, height,                    bufferedimage.type_int_rgb );           graphics g = image.getgraphics ();            Graphics2D g2d =  (graphics2d)   g;           g.setcolor (Getrandcolor (200, 250));           g.fillrect (0, 0, width, height);           g.setcolor (Getrandcolor (180, 200));            g.drawrect (0, 0, width-1, height-1);           for  (int i = 0; i < 200;  i++)  {               int  X = r.nextint (width - 1);                int y = r.nextint (height - 1);                int x1 = r.nextint (WIDTH&NBSP;-&NBSP;10) + 10;    &Nbsp;          int y1 = r.nextint (height &NBSP;-&NBSP;4) +4;                G.setcolor (Getrandcolor (180, 200));                g.drawline (x,y,x1,y1);           }            for (int i=0;i<300;i++)             {                int x = r.nextint (width);                int y = r.nextint (height);                g.setcolor (Getrandcolor (150,180));                 g.drawrect (x, y, 0, 0);            }           String sRand =  " ",s = " ";           int digital = 0;            int itmp = 0;            for  (int i = 0; i < 4; i+ +)  {               switch  ( R.nextint (4))  {               case  0:               case 1:                    digital  = r.neXtint ( + 65;               )     s = string.valueof ((char)  digital);                    break;                case 2:                    digital = r.nextint (num)  +  97;                    s = string.valueof ((char)  digital);                    break;                default:s = string.valueof (R.nextInt (9));               }                sRand +=s;                font mfont = new font ("Arial",  font.bold| FONT.ITALIC,&NBSP;25);                G.setfont (mfont);               boolean  b = r.nextboolean ();                string a = b? "": "-";                int sin = integer.parseint (a+r.nextint);                g.setcolor (Getrandcolor (20, 130));    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;    affinetransform tr = new affinetransform ();                tr.rotate (sin*3.14/180, 15*i+6, b?) 5: (0));               g2d.settransform (tr );               g.drawstring (s, 16* i+10, 23);           }            system.out.println (SRand);            request.getsession (True). setattribute ("Verifycode",  srand);            g.dispose ();            bytearrayinputstream input = null;            bytearrayoutputstream Output = new bytearrayoutputstream ();            try {                imageoutputstream imageout = imageio                        .createimageoutputstream (Output) ;               imageio.write (Image,   "JPEG",  imageout);                imageout.close ();                Inputstream = new bytearrayinputstream (Output.tobytearray ());            } catch  (exception e)  {              &NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Validation code generation failed" + e.tostring ());            }           return inputStream;       }     } 

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.