Java Image Verification code implementation Codes _java

Source: Internet
Author: User

Copy Code code as follows:

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 {
The character set that can appear in the verification code picture can be modified as appropriate
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 width of the generated picture, the parameter height 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 Graphics context
Graphics g = image.getgraphics ();
Set Background color
G.setcolor (New Color (0xDCDCDC));
G.fillrect (0, 0, width, height);
Draw a border
G.setcolor (Color.Black);
G.drawrect (0,0,width-1,height-1);
Take a randomly generated authentication code
String strensure = "";
4 represents a 4-bit captcha, and if you want to generate more bits of authentication code, increase the number
for (int i=0; i<4; ++i) {
strensure+=maptable[(int) (Maptable.length*math.random ())];
}
Display the authentication code to the image, if you want to generate more bits of authentication code, add the 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 (1,2);
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 jamming 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, you first create an instance object of the memory image, then get the graphics context object for the memory image, and then draw the background and border with the context object. 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 of the generated captcha picture and is displayed on the client side with the source code as follows:
makecertpic.jsp

Copy Code code as follows:

<% @page contenttype= "Image/jpeg"%>
<jsp:usebean id= "image" scope= "page" class= "Pic.makecertpic"/>
<%
String Str=image.getcertpic (0,0,response.getoutputstream ());
Save authentication Code in session
Session.setattribute ("Certcode", str);

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

Here the generated verification code is written as a session variable, so in the data page that receives the login page, the validation code entered by the user can be compared with the session variable, if the same is the validation pass.
loginpic.jsp

Copy Code code as follows:

<%@ 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> User name: <input type= "text" name= "username"/></td></tr>
<tr><td> Password: <input type= "password" name= "password"/></td></tr>
<tr><td> Verification Code </td></tr>
<tralign= "left" ><td>
<input type= "Submit" value= "OK"/></td></tr>
</form>
</table>
</body>

Verify that the input of the verification code is correct and can be validated with the following statement:

Copy Code code as follows:

String certcode=request.getparameter ("Certcode");
if (Certcode.equals ((String) Session.getattribute ("Certcode"))
Out.print ("Verify code input is correct");
Else
Out.print ("Validation code input error");

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.