Java Generate Verification Code

Source: Internet
Author: User
Tags set background

Makecertpic.java
Package pic;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.util.Random;
Import Javax.imageio.ImageIO;
/**
* @author Dzy
* Generate Verification Code picture
*/
public class Makecertpic {
A character set that can appear in a captcha picture and can be modified as needed
Private Char maptable[]={
' A ', ' B ', ' C ', ' d ', ' e ', ' f ',
' G ', ' h ', ' I ', ' j ', ' K ', ' l ',
' m ', ' n ', ' o ', ' P ', ' Q ', ' R ',
' s ', ' t ', ' u ', ' V ', ' w ', ' X ',
' Y ', ' z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ',
' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};
/**
* Function: Generate color Verification code picture
* parameter width is the height of the generated picture, the parameter OS is the output stream of the page.
*/
Public String getcertpic (int width, int height, outputstream os) {
if (width<=0) width=60;
if (height<=0) height=20;
BufferedImage image = new BufferedImage (width, height,
BUFFEREDIMAGE.TYPE_INT_RGB);
Get the graphics context
Graphics g = image.getgraphics ();
Set Background color
G.setcolor (New Color (0xDCDCDC));
G.fillrect (0, 0, width, height);
Draw Border
G.setcolor (Color.Black);
G.drawrect (0,0,width-1,height-1);
Take the randomly generated authentication code
String strensure = "";
4 for 4-bit verification code, if you want to generate more bits of the authentication code, then increase the value
for (int i=0; i<4; ++i) {
strensure+=maptable[(int) (Maptable.length*math.random ())];
}
Display the authentication code in the image, if you want to generate more bits of the authentication code, add drawstring statement
G.setcolor (Color.Black);
G.setfont (New Font ("Atlantic Inline", font.plain,18));
String str = strensure.substring (0,1);
g.DrawString (str,8,17);
str = strensure.substring;
g.DrawString (str,20,15);
str = strensure.substring (2,3);
g.DrawString (str,35,18);
str = strensure.substring (3,4);
g.DrawString (str,45,15);
Randomly generates 10 interference points
Random rand = new Random ();
for (int i=0;i<10;i++) {
int x = rand.nextint (width);
int y = rand.nextint (height);
G.drawoval (x,y,1,1);
}
Releasing the graphics context
G.dispose ();
try {
Output image to Page
Imageio.write (Image, "JPEG", OS);
} catch (IOException e) {
Return "";
}
return strensure;
}
}

In the Getcertpic () method, we first create an instance object of the memory image, then get the graphics context object of this memory image, then use this context object to draw the background and border. Next, randomly generated 4 in the maptable[] array of characters, composed of strings as a validation string, and output in memory, in order to cause some interference, randomly draw 10 interference points, if you want to increase the interference effect, you can draw some more points.
The makecertpic.jsp page is used to invoke the JavaBean that generated the captcha picture, and the source code is displayed on the client side as follows:
makecertpic.jsp
<% @page contenttype= "Image/jpeg"%>
<jsp:usebean id= "image" scope= "page" class= "Pic.makecertpic"/>
<%
String Str=image.getcertpic (0,0,response.getoutputstream ());
Save authentication Code to session
Session.setattribute ("Certcode", str);

Out.clear ();
out = Pagecontext.pushbody ();
%>

Here the generated verification code as a session variable is written, so in the receiving login page input data page, the user input verification code and the session variable for comparison, if the same means that validation passed.
loginpic.jsp
<%@ page contenttype= "text/html;charset=gb2312"%>

<script type= "Text/javascript" >
function Reloadcode () {
var Verify=document.getelementbyid (' Code ');
Verify.setattribute (' src ', ' makecertpic.jsp?it= ' +math.random ());
}
</script>
<body>
<table align= "center" border= "0" >
<tralign= "center" ><td><fontcolor= "Red" >&LT;TR align= "center" ><td> System login </td></tr>
<form. action= "logincheck.jsp" method= "POST" focus= "username" >
<tr><td> Username: <input type= "text" name= "username"/></td></tr>
<tr><td> Secret &nbsp;&nbsp; Code: <input type= "password" name= "password"/></td></tr>
<tr><td> captcha </td></tr>
<tralign= "left" ><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp
<input type= "Submit" value= "OK"/></td></tr>
</form>
</table>
</body>



Verify that the input of the verification code is correct using the following statement:
String certcode=request.getparameter ("Certcode");
if (Certcode.equals (String) session.getattribute ("Certcode"))
Out.print ("Verification code input is correct");
Else
Out.print ("Verification Code input error");

Java Generate Verification Code

Related Article

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.