Generate a Captcha picture

Source: Internet
Author: User
Tags flush valid stringbuffer

1 Picture.java

-------------------------------------------------------------

Package com.util;

Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics2D;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.util.Random;

Import Javax.imageio.ImageIO;

/**
* @author Ysma
* @version 1.0.0, 2012-0413
* @since 1.0
*/
public class picture
{
private static random random; Random number
private static char[] chars;//store a-z,0-9, used when generating keys randomly, minus 0 and O
private static final int length = 32;//c[] is 32

Static
{
Init ();
}

/**
* init.
*/
private static void Init ()
{
Random = new Random (System.currenttimemillis ());

chars = new Char[length];

int i = 0;
for (char b = ' A '; b <= ' Z '; b++)
{
if (b! = ' O ')
{
chars[i++] = b;
}
Else
{
i--;
}
}
for (char B = ' 0 '; b <= ' 9 '; b++)
{
if (b! = ' 0 ')
{
chars[i++] = b;
}
Else
{
i--;
}
}
}

/**
* Next.
*
* @return int
*/
public static int Nextint ()
{
return Random.nextint ();
}

/**
* Next N.
*
* @param n
* INT
* @return int
*/
public static int Nextint (int n)
{
return Random.nextint (n);
}

/**
* Generate Keylength bit random string, string containing only a-z,0-9, minus O and 0.
*
* @param keylength
* INT
* @return String
*/
public static String GenerateKey (int keylength)
{
StringBuffer buffer = new StringBuffer ();
int index;
for (int i = 0; i < keylength; i++)
{
index = Random.nextint (LENGTH);
Buffer.append (Chars[index]);
}
return buffer.tostring ();
}

/**
* Generates a 32-bit random string, and the string contains only a-z,0-9.
*
* @return String
*/
public static String GenerateKey ()
{
Return GenerateKey (integer.valueof ("32"));
}

 /**
  *
  * Output CAPTCHA image
  *
  * @param os
  *    & nbsp;       output Stream
  * @param code
  *             Verification Code on the image
  * @param width
  *             Image Width
  * @param height
  *             Image Height
  * @throws ioexception
  *              Exception
  */
 public static void Generateverifycode ( OutputStream OS, String code, int width, int height) throws IOException
 {
  bufferedimage buffimg = new BufferedImage (width, height, bufferedimage.type_int_bgr);
  graphics2d g = buffimg.creategraphics ();

G.setcolor (Color.White);
G.fillrect (0, 0, width, height);
Font font = new Font ("Times New Roma", Font.plain, integer.valueof ("18"));
G.setfont (font);

G.setcolor (Color.Black);
G.drawrect (0, 0, width-1, height-1);

G.setcolor (New Color (Integer.valueof ("192"), Integer.valueof ("192"), Integer.valueof ("192"));
int x, y, x1, y1;
for (int i = 0; I < (width + height); i++)
{
x = random.nextint (width);
y = random.nextint (height);
X1 = Random.nextint (integer.valueof ("12"));
Y1 = Random.nextint (integer.valueof ("12"));
G.drawline (x, y, x + x1, y + y1);
}

int red = 0, green = 0, blue = 0;
for (int i = 0, n = code.length (); i < n; i++)
{
Red = Random.nextint (integer.valueof ("110"));
Green = Random.nextint (integer.valueof ("110"));
Blue = Random.nextint (integer.valueof ("110"));

G.setcolor (New Color (red, green, blue));
g.DrawString (Code.charat (i) + "", integer.valueof ("+") * i + integer.valueof ("6"), integer.valueof ("16"));
}

Imageio.write (buffimg, "JPEG", OS);
Os.flush ();
}
}

---------------------------------------------------------Big Cut-------------------------------------------------------------- --

2,createimage.java

----------------------------------------------

Package com.util;

Import java.io.IOException;
Import Java.io.OutputStream;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;

/**
* @author Ysma
* @version 1.0.0
* @since 2012-04-03
*/
public class CreateImage extends HttpServlet
{
public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException
{
Doprocessor (request, response);
}

public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException
{
Doprocessor (request, response);
}

private void Doprocessor (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException
{
HttpSession session = Request.getsession ();
Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expries", 0);
Response.setcontenttype ("Image/jpeg");
String code = Picture.generatekey (4); Generate 4 Random numbers

OutputStream OS = null;
Try
{
OS = Response.getoutputstream ();
Picture.generateverifycode (OS, code, 60, 20); Generate a Captcha picture
Session.setattribute ("Realcode", code);
Os.flush ();
}
catch (IOException E)
{
Throw e;
}
Finally
{
Try
{
Os.close ();
}
catch (Exception e)
{
}
}
}
}

--------------------------------------------------Big Cut----------------------------------------------------------

3. Web. XML configuration

-------------------------

<!--valid code--
<servlet>
<servlet-name>createImage</servlet-name>
<servlet-class>com.util.CreateImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>createImage</servlet-name>
<url-pattern>/servlet/CreateImage.jpeg</url-pattern>
</servlet-mapping>
<!--valid code end--

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.