
生成图片的java代码如下:
package imagevalidate;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
public class RandImgCreater {
private static final String CODE_LIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
private HttpServletResponse response = null;
private static final int HEIGHT = 20;
private static final int FONT_NUM = 4;
private int width = 0;
private int iNum = 0;//code length
private String codeList = "";//code value range
private boolean drawBgFlag = false;
private int rBg = 0;
private int gBg = 0;
private int bBg = 0;
//could changer the image code length and code range

public RandImgCreater(HttpServletResponse response) {
this.response = response;
this.width = 13 * FONT_NUM + 12;
this.iNum = FONT_NUM;
this.codeList = CODE_LIST;
}
public RandImgCreater(HttpServletResponse response,int iNum,String codeList) {
this.response = response;
this.width = 13 * iNum + 12;
this.iNum = iNum;
this.codeList = codeList;
}
public String createRandImage(){
BufferedImage image = new BufferedImage(width, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();//random function
if ( drawBgFlag ){
g.setColor(new Color(rBg,gBg,bBg));
g.fillRect(0, 0, width, HEIGHT);
}else{
g.setColor(getRandColor(200, 250));
g.fillrect(0, 0, width, height)。 ctx.fillrect(0,0, canvas.width,canvas.height)。 context.fillrect(0, 0, canvas.width, canvas.height)。

// random generate 155 piece jam line
for (int i = 0; i < 155; i++) {
g.setColor(getRandColor(140, 200));
//g.setColor(new Color(0x99,0x99,0x99));
int x = random.nextInt(width);
int y = random.nextInt(HEIGHT);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
}
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
String sRand="";
for (int i=0;i<iNum;i++){
int rand=random.nextInt(codeList.length());//from 0 to codeList.length get the value
String strRand=codeList.substring(rand,rand+1);
sRand+=strRand;
g.setcolor(new color(254, 215, 124))。 thread.sleep((new random().nextint(10)+1)*1000)。 data.set(new random().nextint(30))。
//g.setColor(new Color(0x99,0x99,0x99));
g.drawString(strRand,13*i+6,16);
}
g.dispose();
try{

imageio.write(image, "png", response.getoutputstream())。//response.contenttype = "image/jpeg"。 // response.setcontenttype("image/jpeg")。
}catch(IOException e){
}
return sRand;
}
public void setBgColor(int r,int g,int b){
drawBgFlag = true;
this.rBg = r;
this.gBg = g;
this.bBg = b;
}
private Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
}

2.编写一个jsp文件 我命名为img.jsp
<%@ page language="java" contentType="image/jpeg; charset=gb2312" import="imagevalidate.*"
pageEncoding="gb2312"%>
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
//调用上面的java方法jsp表单验证jsp表单验证,绘制一张验证码,验证的数字保存在session中 rand属性
RandImgCreater rc = new RandImgCreater(response);
//RandImgCreater rc = new RandImgCreater(response,8,"abcdef");
rc.setBgColor(0xC0,0xC0,0xC0);
String rand = rc.createRandImage();
session.setAttribute("rand",rand);
//没有以下两句刷新会出现异常
out.clear();
out = pageContext.pushBody();
%>
3.在登录页面就可以这样调用
<img src="img.jsp">便可生成
2008年8月12日 11:26
感恩之云
8
000
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-110031-1.html
那么为什么我们要忍耐