Results 1 to 2 of 2
  1. #1
    Crest.Boy is offline Member
    Join Date
    Nov 2008
    Rep Power

    Thumbs down how to extract the number of the image which looks like a number

    Now,I can generate a image which contains a random number on web page with Servlet, but I CAN NOT extract the number of the image with one common Java Application which I want to write.

    Any one can help me?

    My Code to generate image on web page (looks disorder, please format in your own IDE)
    package com.hib.img;

    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.util.Date;
    import java.util.Random;

    import javax.imageio.ImageIO;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    public class ValidateCodeImage extends HttpServlet {

    private static final long serialVersionUID = 1L;

    private static String name;

    private int width;

    private int height;

    private int length;

    private int interlinenum;

    private int maxfontsize;

    private int minfontsize;

    private String randomCode;

    public void init(ServletConfig config) throws ServletException {
    name = config.getInitParameter("name");
    width = Integer.parseInt(config.getInitParameter("width")) ;
    height = Integer.parseInt(config.getInitParameter("height") );
    length = Integer.parseInt(config.getInitParameter("length") );
    maxfontsize = Integer.parseInt(config.getInitParameter("maxfonts ize"));
    minfontsize = Integer.parseInt(config.getInitParameter("minfonts ize"));
    interlinenum = Integer.parseInt(config.getInitParameter("interlin enum"));

    public void doGet(HttpServletRequest req, HttpServletResponse res) {
    //set necessary response infomation
    res.setHeader("Pragma", "No-cache");
    res.setHeader("Cache-Control", "No-cache");
    res.setDateHeader("Expires", 0L);

    BufferedImage image = new BufferedImage(width, height,
    Graphics graph = image.getGraphics();
    //set background color and fill the rectangle in specified color
    graph.fillRect(0, 0, width, height);
    //set interrupt line's color and draw random line in specifed color
    for (int i = 0; i < interlinenum; i++) {
    ImageUtil.drawRandomLine(graph, width, height);
    //set the random code color and draw random code
    //set session value
    HttpSession session = req.getSession();
    session.setAttribute(name, this.randomCode);
    //release the resource of the image
    //output the image to the page
    try {
    ImageIO.write(image, "JPEG", res.getOutputStream());
    } catch (IOException e) {

    public void doPost(HttpServletRequest req, HttpServletResponse res){

    * @param graph graphics context
    * @param width width of the range of the graphics context
    * @param height height of the range of the graphics context
    * @param length length of the random code
    private void drawRandomCode(Graphics graph,int width,int height,int length){
    StringBuffer sb = new StringBuffer();
    String eachCode = null;
    int fontsize = minfontsize;
    int y = 0;
    Random r = new Random(new Date().getTime());
    for(int index=0;index<length;index++){
    //random code
    eachCode = String.valueOf(r.nextInt(10));
    //constrct complete random code
    //random fontsize
    fontsize = minfontsize + r.nextInt(maxfontsize-minfontsize);
    //random y axes
    y = height/2 + r.nextInt(height/2);
    //set font and draw random code
    graph.setFont(new Font("Times New Roman", Font.BOLD, fontsize));
    graph.drawString(eachCode, (int)(index*((double)width/(double)length)), y);
    this.randomCode = sb.toString();

    * @return the name
    public static String getName() {
    return name;
    package com.hib.img;

    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.Date;
    import java.util.Random;

    public class ImageUtil {
    public static final int MAX_COLOR_VALUE = 255;
    private static Random r = new Random(new Date().getTime());

    public static Color getRandomColor(int base,int range) {
    if(range > MAX_COLOR_VALUE){
    range = MAX_COLOR_VALUE;
    int red = base + r.nextInt(range);
    int green = base + r.nextInt(range);
    int blue = base + r.nextInt(range);
    return new Color(red, green, blue);

    * @param graph graphics context
    * @param width width of the range of the graphics context
    * @param height height of the range of the graphics context
    public static void drawRandomLine(Graphics graph, int width, int height) {
    int sx = r.nextInt(width);
    int ex = r.nextInt(width);
    int sy = r.nextInt(height);
    int ey = r.nextInt(height);
    graph.drawLine(sx, sy, ex, ey);

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    please format in your own ID
    This forum has tags to do that. Look at the icons above the input box.
    CAN NOT extract the number of the image
    What is "the number of the image"?
    Are you talking about the RGB values used for each pixel in an image?

Similar Threads

  1. how to use Scanner with a number
    By cew27 in forum New To Java
    Replies: 10
    Last Post: 04-03-2009, 06:23 PM
  2. The highest number
    By I-Shine in forum Java Applets
    Replies: 3
    Last Post: 02-13-2008, 05:05 AM
  3. how to know the number of the line
    By simon in forum New To Java
    Replies: 3
    Last Post: 08-01-2007, 04:59 PM
  4. Help with setting number as even or odd
    By fegiflu in forum New To Java
    Replies: 8
    Last Post: 07-24-2007, 06:07 PM
  5. Random Image on Refresh (FROM Folder not set number)
    By QuinnMal in forum Java Servlet
    Replies: 1
    Last Post: 07-06-2007, 07:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts