Got from http://blog.jeffhaynie.us/image-verification-utility-for-form-submission.html
Thanks for the idea author.
Pasting the same below :
Image Verification Utility for Form submission
June 22nd, 2006 · No Comments
image verify screenshot Ever wanted one of those cool HTML form submission pages where you can make sure that only humans are submitting form data, and not machines (like spam bots)? Well, no you can have one. I created a simple Java utility class for generating the dynamic image in PNG format and then a simple servlet and web page for testing. You can download the source and demo here. Good luck!
Utility class
1: /**
2: * Copyright (c) 2006 by Jeff Haynie
3: *
4: * Licensed under the Apache License, Version 2.0 (the “License”);
5: * you may not use this file except in compliance with the License.
6: *
7: * You may obtain a copy of the License at
8: * http://www.apache.org/licenses/LICENSE-2.0
9: *
10: * Unless required by applicable law or agreed to in writing,
11: * software distributed under the License is distributed on an “AS IS” BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package us.jeffhaynie.image;
17:
18: import java.awt.Color;
19: import java.awt.Font;
20: import java.awt.Graphics2D;
21: import java.awt.geom.AffineTransform;
22: import java.awt.image.BufferedImage;
23: import java.io.IOException;
24: import java.io.OutputStream;
25: import java.util.Random;
26: import java.util.UUID;
27:
28: import javax.imageio.ImageIO;
29:
30: /**
31: * ImageVerification is a simple utility class for
32: * creating an image verification PNG file that will
33: * allow you to make sure that only a human can read
34: * the alphanumeric values and enter them into a text
35: * field during verification.
36: *
37: * Make sure that when you can getVerificationCode
38: * you don’t encode the value in the URL or inside the
39: * HTML form - otherwise, this whole excerise is pointless
40: * (dummy!).
41: *
42: * @author Jeff Haynie
43: * @copyright Copyright (c) by Jeff Haynie. All Rights Reserved.
44: */
45: public class ImageVerification
46: {
47: private String value;
48:
49: public ImageVerification (OutputStream out) throws IOException
50: {
51: this(50,120,out);
52: }
53: public ImageVerification (int height, int width, OutputStream out) throws IOException
54: {
55: BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
56: Random rand=new Random(System.currentTimeMillis());
57: Graphics2D g = bimage.createGraphics();
58:
59: // create a random color
60: Color color = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
61:
62: // the the background to the random color to fill the
63: // background and make it darker
64: g.setColor(color.darker());
65: g.fillRect(0, 0, width, height);
66:
67: // set the font
68: g.setFont(new Font(“arial”,Font.BOLD,36));
69:
70: // generate a random value
71: this.value = UUID.randomUUID().toString().replace(“-”,“”).substring(0,5);
72:
73: int w = (g.getFontMetrics()).stringWidth(value);
74: int d = (g.getFontMetrics()).getDescent();
75: int a = (g.getFontMetrics()).getMaxAscent();
76:
77: int x = 0, y =0;
78:
79: // randomly set the color and draw some straight lines through it
80: for (int i = 0; i < x="0;" y="0;" i =" 0;" x =" width/2" y =" height/2" fontat =" new" xp =" x-2;" c="0;c
109: {
110: // apply a random radian either left or right (left is half since it’s too far back)
111: int rotate = rand.nextInt(20);
112: fontAT.rotate(rand.nextBoolean() ? Math.toRadians(rotate) : -Math.toRadians(rotate/2));
113: Font fx = new Font(“arial”, Font.BOLD, 36).deriveFont(fontAT);
114: g.setFont(fx);
115: String ch = String.valueOf(value.charAt(c));
116: int ht = rand.nextInt(3);
117: // draw the string and move the y either up or down slightly
118: g.drawString(ch, xp, y + (rand.nextBoolean()?-ht:ht));
119: // move our pointer
120: xp+=g.getFontMetrics().stringWidth(ch) + 2;
121: }
122: // write out the PNG file
123: ImageIO.write(bimage, “png”, out);
124:
125: // make sure your clean up the graphics object
126: g.dispose();
127: }
128: /**
129: * return the value to check for when the user enters it in. Make sure you
130: * store this off in the session or something like a database and NOT in the
131: * form of the webpage since the whole point of this exercise is to ensure that
132: * only humans and not machines are entering the data.
133: *
134: * @return
135: */
136: public String getVerificationValue ()
137: {
138: return this.value;
139: }
140: }
Wednesday, July 11, 2007
Image Verification Utility for Form submission(Captcha)
Posted on 2:02 AM by Unknown
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment