网页版图片验证码

 package com.tarena.dang.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageUtil{
	private static int WIDTH=200;
	private  static int HEIGHT=80;
	private static int NUM=4;
	private static char[] seq={'A','B','C','D','E','F','G','H'	,
		'I','G','K','L','M','N','O','P','Q','R','S','T','U','V',
		'W','X','Y','Z','1','2','3','4','5','6','7','8','9','0'};
	/**
	 * 随机图片生成工具类
	 * @return    Map
	 */
	public static Map<String, BufferedImage> randomImage(){
		Random r = new Random();
		//获得一个图片
		BufferedImage image  = new  BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
		//得到画笔
		Graphics g = image.getGraphics();
		g.setColor(randomColor(r));
		g.fillRect(0, 0, WIDTH, HEIGHT);
		g.setColor(new Color(0,0,0));
		
		StringBuffer number = new StringBuffer();
		int h=0;
		//画出验证码
		for(int i=0;i<NUM;i++){
			h=(int)((HEIGHT*60/100)*r.nextDouble()+(HEIGHT*30/100));
			g.setFont(new Font(null,Font.BOLD|Font.ITALIC,h));
			String ch = String.valueOf(seq[r.nextInt(seq.length)]);
			g.drawString(ch,i*WIDTH/NUM*90/100,h);
			number.append(ch);	
		}	
		//画干扰线
		for(int i=0;i<10;i++){
			g.setColor(randomColor(r));
			g.drawLine(r.nextInt(WIDTH), r.nextInt(HEIGHT), r.nextInt(WIDTH),r.nextInt(HEIGHT));
		}
	
		try {
			Map<String,BufferedImage> map = new HashMap<String,BufferedImage>();
			map.put(number.toString(), image);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}		
	}
	public static Color randomColor(Random r){
		return new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255));
	}
	/**
	 * 将图片转换为inputStream工具类
	 * @param image 参数是BufferedImage
	 * @return   返回输入流
	 */
	public static InputStream imageToStream(BufferedImage image){
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
			InputStream imageStream=null;
			try {
				encoder.encode(image);
				byte[] bytes = bos.toByteArray();
				imageStream = new ByteArrayInputStream(bytes);
			}  catch (Exception e) {
				e.printStackTrace();
			}
			return imageStream;
	}
	
}

package com.tarena.dang.action.user;

import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.Map;

import com.tarena.dang.util.ImageUtil;

public class ImageAction extends BaseAction{
	private InputStream imageStream;
	public InputStream getImageStream() {
		return imageStream;
	}
	public void setImageStream(InputStream imageStream) {
		this.imageStream = imageStream;
	}
	public String execute(){
		Map<String,BufferedImage> map =ImageUtil.randomImage();
		String code = map.keySet().iterator().next();
		session.put("code", code);
		BufferedImage image = map.get(code);
		imageStream=ImageUtil.imageToStream(image);
		return "success";
	}
	
}

 

<td>
    <img class="yzm_img" id='imgVcode' src="imageCode.action" />
    <input name="number" type="text" id="txtVerifyCode"
class="yzm_input"/>
   <div class="text_left t1">
	<p class="t1">
	<span id="vcodeValidMsg">请输入图片中的四个字母。</span>	   <span id="number.info" style="color:red"></span>
	 <a href="#" id="noSee">看不清楚?换个图片</a>
	</p>
    </div>
</td>

//下面是js,主要功能实现验证码的更新

$("#noSee").click(function(){
	$("#imgVcode").attr("src","imageCode.action?dt="+ new Date().getTime());
	return false;
	});

你可能感兴趣的:(图片验证码)