Simple Captch Using javascript
Captch generated class.This class Generated captche 0-9 numbers
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
/**
* The Class CaptchaImage.java.
*
* @author Raja Maragani
* @created on 25-Dec-2013 12:33:53 PM
*/
public class CaptchaImage {
public CaptchaImage() {
// TODO Auto-generated constructor stub
}
/**
* This Class written for the generate the CaptchaImage
*
*/
String captchaString = "";
// Function to generate random captcha image and returns the BufferedImage
public BufferedImage getCaptchaImage() {
try {
Color backgroundColor = Color.white;
// Color borderColor = Color.black;
Color textColor = Color.black;
Color circleColor = new Color(190, 160, 150);
Font textFont = new Font("Arial", Font.BOLD, 16);
int charsToPrint = 4;
int width = 50;
int height = 23;
// int circlesToDraw = 25;
float horizMargin = 10.0f;
double rotationRange = 0.2;
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
g.setColor(backgroundColor);
g.fillRect(0, 0, width, height);
// lets make some noisey circles
g.setColor(circleColor);
// The below code for the background small rectangles
/*
* for (int i = 0; i < circlesToDraw; i++) { int L = (int)
* (Math.random() * height / 2.0); int X = (int) (Math.random() *
* width - L); int Y = (int) (Math.random() * height - L);
* g.draw3DRect(X, Y, L * 2, L * 2, true); }
*/
g.setColor(textColor);
g.setFont(textFont);
FontMetrics fontMetrics = g.getFontMetrics();
int maxAdvance = fontMetrics.getMaxAdvance();
int fontHeight = fontMetrics.getHeight();
// i removed 1 and l and i because there are confusing to users...
// Z, z, and N also get confusing when rotated
// this should ideally be done for every language...
// 0, O and o removed because there are confusing to users...
// i like controlling the characters though because it helps prevent
// confusion
String elegibleChars = "1234567890";
char[] chars = elegibleChars.toCharArray();
float spaceForLetters = -horizMargin * 2 + width;
float spacePerChar = spaceForLetters / (charsToPrint - 1.0f);
StringBuffer finalString = new StringBuffer();
int first = 0;
int second = 0;
for (int i = 0; i < charsToPrint; i++) {
double randomValue = Math.random();
int randomIndex = (int) Math.round(randomValue
* (chars.length - 1));
char characterToShow = chars[randomIndex];
if (finalString.length() == 1) {
characterToShow = '+';
first = Integer.parseInt(finalString.toString());
} else if (finalString.length() == 3) {
characterToShow = '=';
second = Integer.parseInt(finalString.toString().substring(
2));
}
finalString.append(characterToShow);
// System.out.println(finalString.toString() );
System.out.println("first=" + first);
System.out.println("second=" + second);
// this is a separate canvas used for the character so that
// we can rotate it independently
int charWidth = fontMetrics.charWidth(characterToShow);
int charDim = Math.max(maxAdvance, fontHeight);
int halfCharDim = (int) (charDim / 2);
BufferedImage charImage = new BufferedImage(charDim, charDim,
BufferedImage.TYPE_INT_ARGB);
Graphics2D charGraphics = charImage.createGraphics();
charGraphics.translate(halfCharDim, halfCharDim);
double angle = (Math.random() - 0.7) * rotationRange;
charGraphics
.transform(AffineTransform.getRotateInstance(angle));
charGraphics.translate(-halfCharDim, -halfCharDim);
charGraphics.setColor(textColor);
charGraphics.setFont(textFont);
int charX = (int) (0.5 * charDim - 0.5 * charWidth);
charGraphics
.drawString(
"" + characterToShow,
charX,
(int) ((charDim - fontMetrics.getAscent()) / 2 + fontMetrics
.getAscent()));
float x = horizMargin + spacePerChar * (i) - charDim / 2.0f;
int y = (int) ((height - charDim) / 2);
g.drawImage(charImage, (int) x, y, charDim, charDim, null, null);
charGraphics.dispose();
}
first = first + second;
// g.setColor(borderColor);
// g.drawRect(0, 0, width - 1, height - 1);
g.dispose();
captchaString = first + "";
System.out.println("captchaString=" + captchaString);
return bufferedImage;
} catch (Exception ioe) {
throw new RuntimeException("Unable to build image", ioe);
}
}
// Function to return the Captcha string
public String getCaptchaString() {
return captchaString;
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
/**
* The Class CaptchaImage.java.
*
* @author Raja Maragani
* @created on 25-Dec-2013 12:33:53 PM
*/
public class CaptchaImage {
public CaptchaImage() {
// TODO Auto-generated constructor stub
}
/**
* This Class written for the generate the CaptchaImage
*
*/
String captchaString = "";
// Function to generate random captcha image and returns the BufferedImage
public BufferedImage getCaptchaImage() {
try {
Color backgroundColor = Color.white;
// Color borderColor = Color.black;
Color textColor = Color.black;
Color circleColor = new Color(190, 160, 150);
Font textFont = new Font("Arial", Font.BOLD, 16);
int charsToPrint = 4;
int width = 50;
int height = 23;
// int circlesToDraw = 25;
float horizMargin = 10.0f;
double rotationRange = 0.2;
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
g.setColor(backgroundColor);
g.fillRect(0, 0, width, height);
// lets make some noisey circles
g.setColor(circleColor);
// The below code for the background small rectangles
/*
* for (int i = 0; i < circlesToDraw; i++) { int L = (int)
* (Math.random() * height / 2.0); int X = (int) (Math.random() *
* width - L); int Y = (int) (Math.random() * height - L);
* g.draw3DRect(X, Y, L * 2, L * 2, true); }
*/
g.setColor(textColor);
g.setFont(textFont);
FontMetrics fontMetrics = g.getFontMetrics();
int maxAdvance = fontMetrics.getMaxAdvance();
int fontHeight = fontMetrics.getHeight();
// i removed 1 and l and i because there are confusing to users...
// Z, z, and N also get confusing when rotated
// this should ideally be done for every language...
// 0, O and o removed because there are confusing to users...
// i like controlling the characters though because it helps prevent
// confusion
String elegibleChars = "1234567890";
char[] chars = elegibleChars.toCharArray();
float spaceForLetters = -horizMargin * 2 + width;
float spacePerChar = spaceForLetters / (charsToPrint - 1.0f);
StringBuffer finalString = new StringBuffer();
int first = 0;
int second = 0;
for (int i = 0; i < charsToPrint; i++) {
double randomValue = Math.random();
int randomIndex = (int) Math.round(randomValue
* (chars.length - 1));
char characterToShow = chars[randomIndex];
if (finalString.length() == 1) {
characterToShow = '+';
first = Integer.parseInt(finalString.toString());
} else if (finalString.length() == 3) {
characterToShow = '=';
second = Integer.parseInt(finalString.toString().substring(
2));
}
finalString.append(characterToShow);
// System.out.println(finalString.toString() );
System.out.println("first=" + first);
System.out.println("second=" + second);
// this is a separate canvas used for the character so that
// we can rotate it independently
int charWidth = fontMetrics.charWidth(characterToShow);
int charDim = Math.max(maxAdvance, fontHeight);
int halfCharDim = (int) (charDim / 2);
BufferedImage charImage = new BufferedImage(charDim, charDim,
BufferedImage.TYPE_INT_ARGB);
Graphics2D charGraphics = charImage.createGraphics();
charGraphics.translate(halfCharDim, halfCharDim);
double angle = (Math.random() - 0.7) * rotationRange;
charGraphics
.transform(AffineTransform.getRotateInstance(angle));
charGraphics.translate(-halfCharDim, -halfCharDim);
charGraphics.setColor(textColor);
charGraphics.setFont(textFont);
int charX = (int) (0.5 * charDim - 0.5 * charWidth);
charGraphics
.drawString(
"" + characterToShow,
charX,
(int) ((charDim - fontMetrics.getAscent()) / 2 + fontMetrics
.getAscent()));
float x = horizMargin + spacePerChar * (i) - charDim / 2.0f;
int y = (int) ((height - charDim) / 2);
g.drawImage(charImage, (int) x, y, charDim, charDim, null, null);
charGraphics.dispose();
}
first = first + second;
// g.setColor(borderColor);
// g.drawRect(0, 0, width - 1, height - 1);
g.dispose();
captchaString = first + "";
System.out.println("captchaString=" + captchaString);
return bufferedImage;
} catch (Exception ioe) {
throw new RuntimeException("Unable to build image", ioe);
}
}
// Function to return the Captcha string
public String getCaptchaString() {
return captchaString;
}
}
Based on the above class servlet class will generates the Captcha image and store the answer in seeion
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession session = request.getSession();
response.setContentType("image/jpg");
CaptchaImage obj = new CaptchaImage();
BufferedImage ima = obj.getCaptchaImage();
OutputStream osImage = response.getOutputStream();
ImageIO.write(ima, "jpeg", osImage);
String captchaStr = obj.getCaptchaString();
session.setAttribute("captchaStr", captchaStr);
}
JSP tag
<img alt="Captcha" src="captchaimage.jpg" align="right"/>
web.xml file configuration
<!-- Captcha Image servlet START from Here -->
<servlet>
<description></description>
<display-name>CaptchaServlet</display-name>
<servlet-name>CaptchaServlet</servlet-name>
<servlet-class>com.ifl.rapid.common.CaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CaptchaServlet</servlet-name>
<url-pattern>/captchaimage.jpg</url-pattern>
</servlet-mapping>
<!-- Captcha Image servlet END Here -->
Comments
Post a Comment