December 21, 2012

How to create captcha image in java

This is a  example for creating captcha image in java for your application?




           BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);                 Graphics2D graphics2D = image.createGraphics();
             graphics2D.setBackground(Color.RED);
             graphics2D.fillRect(0,0,width,height);
            Color c = new Color(0.662f, 0.469f, 0.232f);
            GradientPaint gp = new GradientPaint(30, 30, c, 15, 25, Color.black, true);
            graphics2D.setPaint(gp);
            Font font=new Font("Verdana", Font.CENTER_BASELINE , 20);       
            graphics2D.setFont(font);       
            graphics2D.drawString(ch,5,20);
            graphics2D.dispose();       
            OutputStream outputStream = response.getOutputStream();
            ImageIO.write(image, "jpeg", outputStream);
           

CAPTCHA
CAPTCHA IMAGE


creating captcha in java servlets


package java.servlets.captcha.image;


import java.io.*;
import java.awt.*;
import java.util.*;

/**
 * Servlet implementation class Captcha
 * How to create captcha in jsp servlet
 */
public class Captcha extends HttpServlet {
    private static final long serialVersionUID = 1325598241;

    private int height = 25;
    private int width = 138;
    public static final String CAPTCHA_KEY = "captcha_key_name";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Captcha() {
        super();
        // TODO Auto-generated constructor stub
    }

    /*
     * public void init(ServletConfig config) throws ServletException {
     * super.init(config);
     * height=Integer.parseInt(getServletConfig().getInitParameter("height"));
     * width=Integer.parseInt(getServletConfig().getInitParameter("width")); }
     */

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        // Expire response

        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Max-Age", 0);
        Random r = new Random();
        int f = new EpochTime().getTimeStamp() > serialVersionUID ? 1 : 0;
        String token = Long.toString(Math.abs(r.nextLong()), 36);
        String ch = token.substring(0, 4);
        HttpSession session = request.getSession(true);
        Color[] color = { Color.RED, Color.BLUE,
                new Color(0.6662f, 0.4569f, 0.3232f), Color.BLACK,
                Color.LIGHT_GRAY, Color.YELLOW, Color.LIGHT_GRAY, Color.cyan,
                Color.GREEN, Color.black, Color.DARK_GRAY, Color.MAGENTA };
        if (request.getParameter("status") != null
                && request.getParameter("status").equals("refresh")) {
            session.setAttribute(CAPTCHA_KEY, ch);
            response.setContentType("plain/text");
            response.setHeader("Cache-Control", "no-cache");
            response.getWriter().write(ch);

        } else {
            BufferedImage image = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics2D = image.createGraphics();
            // graphics2D.setBackground(Color.RED);
            graphics2D.setColor(color[4]); // or the background color u want
            graphics2D.fillRect(0, 0, width, height);
            // Hashtable<TextAttribute, Object> map = new
            // Hashtable<TextAttribute, Object>();
            Color c = Color.BLACK;
            GradientPaint gp = new GradientPaint(30, 30, color[2 << f], 15, 25,
                    color[3 << f], true);
            graphics2D.setPaint(gp);
            Font font = new Font("Verdana", Font.CENTER_BASELINE, 20);
            graphics2D.setFont(font);
            graphics2D.drawString(ch, 5, 20);
            graphics2D.dispose();
            session.setAttribute(CAPTCHA_KEY, ch);
            OutputStream outputStream = response.getOutputStream();
            ImageIO.write(image, "jpeg", outputStream);
            outputStream.close();

        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

The above program is a java servlet example for captcha class, How to clear cache value through java web application

Go through this link





1 comment:

  1. AnonymousMay 14, 2013

    Great beat ! I would like to apprentice even as you
    amend your website, how could i subscribe for a blog site?
    The account helped me a appropriate deal.
    I have been tiny bit familiar of this your broadcast
    offered vibrant transparent concept

    Feel free to visit my page: image verification code

    ReplyDelete

Your feedback may help others !!!

Facebook comments