Generate Capatcha in JAVA


package com.project.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

/**
* Action implementation class CaptchaAction
*/
public class CaptchaGeneration {
private static final Logger log = Logger.getLogger(CaptchaGeneration.class);

private String captchValue;
private String prevImageName;
private String currentImageName;

public CaptchaGeneration(){

}
public CaptchaGeneration(String prevImageName){
       this.prevImageName = prevImageName;
}

public String getCaptchValue() {
        return captchValue;
}

public void setCaptchValue(String captchValue) {
        this.captchValue = captchValue;
}
public String getPrevImageName() {
return prevImageName;
}
public void setPrevImageName(String prevImageName) {
this.prevImageName = prevImageName;
}
public String getCurrentImageName() {
return currentImageName;
}
public void setCurrentImageName(String currentImageName) {
this.currentImageName = currentImageName;
}
public void refresh(String tempFolderPath) throws Exception {
log.info("Inside execute of CaptchaAction");
int height = 30;
int width = 120;
File f = null;
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = image.createGraphics();
Random r = new Random();
String token = Long.toString(Math.abs(r.nextLong()), 36);
String ch = token.substring(0, 6);
GradientPaint gp = new GradientPaint(30, 30, Color.gray, 15, 25, Color.GRAY, true);
graphics2D.setPaint(gp);
Font font = new Font("Verdana", Font.CENTER_BASELINE, 26);
graphics2D.setFont(font);
graphics2D.drawString(ch, 2, 24);
graphics2D.dispose();
setCaptchValue(ch);
StringBuffer fileName = null;
//String path = null;
FileOutputStream fout = null;
try {
fileName = new StringBuffer("Captcha").append(System.nanoTime()).append(".jpeg");
//path = req.getRealPath(CommonConstant.TEMPFILES_FOLDER_NAME);
log.info("path : " + tempFolderPath);
if(getPrevImageName()!=null){
f = new File(tempFolderPath + "/" + getPrevImageName());
log.info("File to delete : " + getPrevImageName());
if(f.exists()){
boolean isDeletd = f.delete();
log.info("FIle deleted ::::::::::::: " + isDeletd);
}
}
f = null;
f = new File(tempFolderPath + "/" + fileName.toString());
fout = new FileOutputStream(f);
ImageIO.write(image, "jpeg", fout);
log.info("file name is :: " + fileName.toString());
setCurrentImageName(fileName.toString());
setPrevImageName(fileName.toString());
} catch (Exception e) {
log.error("CaptchaGeneration : refresh() : Exception : " , e);
throw e;
}finally{
fileName = null;
f = null;
if(fout!=null){
fout.close();
}
fout = null;
}

}

}

Advertisements

About Sanju
I am Software Programmer. I am working in JAVA/J2EE Technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: