- package com.tliu.case2.web;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Random;
- import javax.imageio.ImageIO;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- @Controller
- public class SimpleCaptchaController {
- private static final int WIDTH = 80;
- private static final int HEIGHT = 21;
- private static final int SIZE = 4;
- private static final int LINES = 100;
- private static final String[] CHARS = {"1","2","3","4","5","6","7","8","9",
- "A","B","C","D","E","F","G","H","I","J","K","L","M","N"};
- private static final int FONT_SIZE = 22;
- private static Random in = new Random();
- private static final Color[] colors = {new Color(0,0,0),
- new Color(240,124,21),new Color(112,112,112),
- new Color(88,88,255),new Color(255,88,88)};
- private static Map<String,Object> randomImage(){
- BufferedImage image=new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
- StringBuffer number = new StringBuffer();
- Graphics g = image.getGraphics();
- g.setColor(new Color(234,234,234));
- // 画出矩形区域
- g.fillRect(0, 0, WIDTH,HEIGHT);
- // 随机画字符
- for(int i = 1 ; i <= SIZE ; i++){
- g.setColor(randomCharColor());
- int j = in.nextInt(CHARS.length);
- g.setFont(new Font("宋体",Font.BOLD+Font.ITALIC,FONT_SIZE));
- g.drawString(CHARS[j], (i-1)*WIDTH/SIZE, HEIGHT/2+6);
- number.append(CHARS[j]);
- }
- // 随机画干扰点
- for(int i = 1 ; i <= LINES ; i++){
- int x = in.nextInt(WIDTH) ;
- int y = in.nextInt(HEIGHT) ;
- g.setColor(randomColor());
- g.setFont(new Font("Arial",Font.ITALIC,1));
- g.drawLine(x , y , x , y);
- }
- Map<String,Object> map = new HashMap<String, Object>();
- map.put("code", number.toString()) ;
- map.put("image",image) ;
- return map;
- }
- /**
- * 获取随机颜色的方法;
- * @return
- */
- private static Color randomColor(){
- return new Color(in.nextInt(256),in.nextInt(256),in.nextInt(256));
- }
- private static Color randomCharColor(){
- return colors[in.nextInt(colors.length)] ;
- }
- @RequestMapping(value = "/showImageCode.do")
- public void showImageCode(HttpSession session,HttpServletResponse response)throws Exception{
- response.setContentType("image/jpeg");
- Map<String, Object> map = randomImage();
- session.setAttribute("imageCode",map.get("code"));
- BufferedImage image = (BufferedImage)map.get("image");
- ImageIO.write(image, "JPEG", response.getOutputStream());
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0202201614525.html
来源: http://www.codesnippet.cn/detail/0202201614525.html