Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Images random creator and validator (with number and caracters) (Like hotmail)

Hi..
I'm looking for something in jsp and java, that create a image ( with caracters and number), like hotmail does.

I want to create a inscription form, and at the end the new user have to reenter what he see on the create image to be able to precess the request.
This will ensure that a user calling my service and not a automatic system from someone else..

Anybody can help me..
Thanks
0
jabcoco
Asked:
jabcoco
  • 7
  • 6
1 Solution
 
bloodredsunCommented:
Sounds like a good idea

Here's an article showing you how to create images for servlets (and it's equally applicable for JSPs): http://www.javaworld.com/javaworld/jw-05-2000/jw-0505-servlets.html
0
 
jabcocoAuthor Commented:
I've already read this page, but that simply explain how to genera a simple image, that not explain how to generate an image code (Image with a code text) that the user has to type in an input text. And that not explain how can i valid if the image code fit with what the user entered in the text field....

Any other idea..
Any library or util to use as a black box to generate the image? then valif if image match with the enteres text field.....

Thanks
0
 
bloodredsunCommented:
It's just a matter of writing to the graphic objects before you output it to the ServletOutputStream.

I don't normally do peoples work for them but this is quite intresting. Here's a servlet that produces an image with text on. In this case, the text is defaults to "Test Me" but you could pass it through in the session or request object and validate it from there.

If you call imgServlet.do --> Image says "Test Me"
If you call imgServlet.do?str=Banana --> Image says "Banana"

as the servlet checks the request parameters for String to write into the image.

----------------
package com.bloodredsun.servlet;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
 * @author bloodredsun
 * @return
 */

public class ImageServlet extends HttpServlet {

      public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

            ServletOutputStream sos = resp.getOutputStream();
            resp.setContentType("image/jpg");
            
            String imgString = req.getParameter("str")!=null ? req.getParameter("str") : "Test Me" ;

            // Create an image to output
            RenderedImage rendImage = myCreateImage( imgString );

            try {
                  ImageIO.write(rendImage, "jpg", sos);
            } catch (IOException e) {
                  System.out.println("Error:" + e.getMessage());
            }
      }

      public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            doGet(req, resp);
      }

      /**
       * @return RenderedImage
       */
      private RenderedImage myCreateImage( final String pString ) {
            int width = 100;
            int height = 50;

            // Create a buffered image in which to draw
            BufferedImage bufferedImage =
                  new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            // Create a graphics contents on the buffered image
            Graphics2D g2d = bufferedImage.createGraphics();

            // Draw graphics
            g2d.setColor(Color.pink);
            g2d.fillRect(0, 0, width, height);
            g2d.setColor(Color.black);
            g2d.drawString(pString, 50, 25);

            // Graphics context no longer needed so dispose it
            g2d.dispose();

            return bufferedImage;
      }
}


0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
jabcocoAuthor Commented:
Great thanks bloodredsun..
Basic idea is there... thats what i need. now i'm gonna try to fine tuning a little bit this for more securiting image with line and things like thanks......

Big thanks!
0
 
jabcocoAuthor Commented:
But..... can i change the font style of the caractere i print.. or the color of each lettre i print... and create a kind of more hard to read image... to get a bettre security image...
???
0
 
bloodredsunCommented:
>>can i change the font style of the caractere i print.. or the color of each lettre i print... and create a kind of more hard to read image... to get a bettre security image...

Yes. Draw each letter as an individual String and use these two methods to change the color and font

            g2d.setColor(Color color);
            g2d.setFont(Font font) ;

You'll have to reset the location of each string you write as they are all written in relationto  the same starting location. ALthough it might be a good idea to over-lap them slightly as auto-recognition is very tough then

>>Big thanks!
No probs dude! Just remember me when it comes to point and grades :-)
0
 
bloodredsunCommented:
FYI: here's a class that will print out your avialable fonts:
-------------
public class GetAvailableFonts {
            
      public static void main (String[] args){
      
            // Get all font family names
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            String fontNames[] = ge.getAvailableFontFamilyNames();
            
            // Iterate the font family names
            for (int i=0; i<fontNames.length; i++) {
                  System.out.println(fontNames[i]) ;
            }
      }
}
0
 
jabcocoAuthor Commented:
Nice.. real thanks.. then if i want to generate "tricky line" like hotmail.. is it possible the Graphics2D?!?!

Thanks..
0
 
bloodredsunCommented:
Any thing is possible in G2D. It's just not the most intuative graphics format....:-(

And what do you mean by "tricky line" exactly (I've never used hotmail)
0
 
jabcocoAuthor Commented:
Heu.. something like Yahoo resgitration or something like any other site where you need to add in a text box what caracters you see in the image, to valid your registration...
0
 
bloodredsunCommented:
Though so, but I wanted to make sure.
0
 
jabcocoAuthor Commented:
thanks for the time you pass bloodredsun!!!!
0
 
bloodredsunCommented:
cheers
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now