Sergeonclear

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, July 11, 2007

Image Verification Utility for Form submission(Captcha)

Posted on 2:02 AM by Unknown
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: }


Email ThisBlogThis!Share to XShare to Facebook
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Cobertura- junit coverage tool
    First, you need to add a task definition to the build.xml file. This top-level taskdef element specifies that the cobertura.jar file is i...
  • Texter - An auto text expander
    I just cant imagine how many times I would have typed the same sentence again and again... Texter to the rescue.... thanks to  Adam Pash !! ...
  • setting up a static ip with SKY broadband
    Setting up a static ip on SKY broadband log into the router Navigate to LAN IP SET UP under advanced settings. Click add under address reser...
  • Troubleshooting Oracle connections related problem
    /* Display the number of sessions created for this db connection */ select username,schemaname, osuser,machine, terminal, program, type, mod...
  • Xpath
    XPath uses path expressions to select nodes or node-sets in an XML document. It contains a library of standard functions and is a major elem...
  • Textpad tricks
    Textpad tricks Find and replace Input : 66 Urology & Nephrology 737 Spermatogenesis Replace : the bold text in the above input Find Patt...
  • Oracle fix for schema table prefix and running scripts from sql developer
    In oracle it appears like a NON_OWNER user though granted access(select, insert, delete, update) to tables created by OWNER of the table ca...
  • SCRUM
    A mind map : http://www.mp3monster.org/new/techie/MindMaps/SCRUM.shtml
  • External Javascript from Java Servlets
    Copied from http://myappsecurity.blogspot.com/2007/01 Like to thank anurag for the content. /breaking-same-origin-barrier-of.html External ...
  • Spring method interceptors
    An excellent feature of spring AOP http://www.javalobby.org/java/forums/t44746.html Excellent podcast spanning from spring framework, AJAX t...

Categories

  • AJAX javascript
  • All about UK Visa HSMP VS H1
  • All about UK Visa HSMP VS H1 hsmp assistance
  • amazon
  • android apps ship control radio hindi
  • Apache 2.x setup Quick guide for Linux
  • apache commons configurator usage
  • apache commons usage
  • arsenals for developers
  • article
  • Batch script to load developer environment
  • Blind folded chess
  • Castor castor convert dtd to xsd
  • cloud comparision price
  • cloud comparison blog
  • cloud computing monthly price
  • cloud usage
  • Cobertura- junit coverage tool
  • Code generators
  • cron jobs expressions
  • cruisecontrol cruise control
  • debugging eclipse tips
  • developer tools
  • document library
  • easy mock jmock vs mocking java tdd
  • External Javascript from Java Servlets
  • fedora 9 lenovo 3000 n200 windows xp dual boot problem
  • Fire fox plugins and tweaks
  • Free PHP hosting
  • gods debris the religion war scott adams dilbert
  • google app engine
  • gwt javascript
  • hibernate second level cache
  • i18n locale localization internationalization spring liferay portlet locale
  • ibatis sybase mapping
  • image gallery
  • iphone apps bri8 apple
  • iphone shsh 3gs ipsw downgrade ifaith tinyumbrella ios5.1.1 to ios5.0
  • java
  • java JDBC
  • javascript junit testing
  • Javascript trouble shooting tool
  • Jboss overview
  • jmeter load testing custom java sampler javasamplerclient xml test
  • JNDI test JSP page
  • Joomla CMS
  • JProfiler setup jprofiler on linux
  • jquery IE AJAX issues
  • jquery spring AJAX
  • keyboard music java typing soothing notes auto suggest
  • liferay kids version
  • liferay web 2.0 java/j2ee
  • linux - the difference between hard and soft links
  • linux mysql
  • linux mysql setup quick start
  • Linux ssh autologin with putty
  • LINUX usefuls
  • linux)
  • LinuxPerformance Tuning(apache
  • log4j setup useful
  • lucene solr
  • mac m701 android skype not working crash
  • maven
  • maven cut reduce build time
  • microsoft ODBC oracle dll connection issues
  • Mobile Ad Services (adwhirl)
  • opsourcecloud
  • oracle connect by hierarchy
  • oracle table previlege
  • Oracle thin vs OCI(type II/thick) drivers
  • pdf 2 text
  • Pega PRPC
  • php
  • rackspacecloud
  • Rewrite rules in apache and IIS
  • scaffold
  • setting up a static ip on SKY broadband
  • setup quick start
  • Single Sign on - OpenSSO with Liferay
  • smart gwt
  • smtp email test mock server james
  • SMTP MAIL telnet windows
  • Software tools mind map freemind j2ee tools
  • Speed typing tips.
  • Spring + Hibernate Usefuls BaseDAOHibernate
  • Spring annotations with spring-mock not working 2.0 2.5.6
  • spring jndi datasource lookup
  • Spring portlet mvc and spring servlet mvc validation
  • Spyware trojan and virus removal tools
  • struts magic
  • SVN/ Subversion Tips and traps
  • tabbed ms dos console cygwin console
  • Texter - An auto text expander autotyper
  • Textpad tricks
  • The art of debugging
  • tomcat
  • Tomcat Exploded war - cut deployment time
  • Tomcat on linux tips
  • Tomcat on linux tips commands
  • TypeIt4Me
  • Typinator
  • Useful Eclipse Plugins
  • Useful Eclipse Plugins eclipse shortcuts keys
  • Useful tools
  • web content
  • xcode cocos2d iphone box2d
  • xpath xml xquery

Blog Archive

  • ►  2013 (19)
    • ►  August (17)
    • ►  July (1)
    • ►  January (1)
  • ►  2012 (7)
    • ►  August (1)
    • ►  June (4)
    • ►  April (2)
  • ►  2011 (20)
    • ►  November (1)
    • ►  October (1)
    • ►  August (1)
    • ►  July (3)
    • ►  June (1)
    • ►  April (2)
    • ►  March (4)
    • ►  February (1)
    • ►  January (6)
  • ►  2010 (27)
    • ►  December (3)
    • ►  July (2)
    • ►  May (3)
    • ►  April (2)
    • ►  March (5)
    • ►  February (10)
    • ►  January (2)
  • ►  2009 (32)
    • ►  December (5)
    • ►  November (2)
    • ►  September (3)
    • ►  August (6)
    • ►  June (4)
    • ►  May (4)
    • ►  April (3)
    • ►  March (2)
    • ►  February (3)
  • ►  2008 (28)
    • ►  December (1)
    • ►  October (2)
    • ►  September (2)
    • ►  August (4)
    • ►  July (7)
    • ►  June (1)
    • ►  April (2)
    • ►  March (2)
    • ►  February (2)
    • ►  January (5)
  • ▼  2007 (24)
    • ►  December (3)
    • ►  November (2)
    • ►  October (6)
    • ►  September (1)
    • ►  August (3)
    • ▼  July (8)
      • SVN/ Subversion Tips and traps
      • apache commons configurator usage
      • setting datasource lookup from tomcat JNDI using s...
      • External Javascript from Java Servlets
      • Image Verification Utility for Form submission(Cap...
      • PMD integration -For better quality
      • Javascript trouble shooting tools:
      • Banging with cruiseControl
    • ►  June (1)
Powered by Blogger.

About Me

Unknown
View my complete profile