Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-03-2008, 12:31 PM
Member
 
Join Date: Nov 2008
Posts: 1
Rep Power: 0
Crest.Boy is on a distinguished road
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.io.IOException;
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 {
super.init(config);
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.setContentType("image/jpeg");
res.setHeader("Pragma", "No-cache");
res.setHeader("Cache-Control", "No-cache");
res.setDateHeader("Expires", 0L);

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

public void doPost(HttpServletRequest req, HttpServletResponse res){
doGet(req,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
sb.append(eachCode);
//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);
}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-03-2008, 03:38 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 2,229
Rep Power: 4
Norm is on a distinguished road
Default
Quote:
please format in your own ID
This forum has tags to do that. Look at the icons above the input box.
Quote:
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?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +2. The time now is 06:45 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org