当前位置: 代码迷 >> Web前端 >> java验证码工具种
  详细解决方案

java验证码工具种

热度:118   发布时间:2012-08-27 21:21:56.0
java验证码工具类
package cn.rx.oamp.util;

import java.awt.Color;   
import java.awt.Font;   
import java.awt.Graphics;   
import java.awt.image.BufferedImage;   
import java.util.Random;   
  
import javax.imageio.ImageIO;   
import javax.servlet.ServletOutputStream;   
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;   
  
public final class ImageCode {   
  
    private static final Random random = new Random();   
  
    private static final Font[] CODEFONT = { new Font("宋体", Font.BOLD, 25), new Font("宋体", Font.BOLD, 25), new Font("宋体", Font.BOLD, 25),   
            new Font("宋体", Font.BOLD, 25) };   
    private static final Color[] FONTCOLOR = { Color.ORANGE, Color.RED, Color.PINK, Color.BLUE, Color.GREEN };   
    private static final Color[] BGCOLOR = { Color.WHITE, Color.GREEN };   
    private static final Color LINECOLOR = new Color(242, 234, 15);   
    private static final String[] CODE = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",   
            "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };   
    private static StringBuffer CONDENUMBER = null;   
    private static final int WIDTH = 100, HEIGHT = 25;   
  
    public final String image(HttpServletRequest request, HttpServletResponse response) {   
        CONDENUMBER = new StringBuffer();   
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);   
        Graphics g = image.getGraphics();   
        g.setColor(BGCOLOR[random.nextInt(1)]);   
        g.fillRect(0, 0, WIDTH, HEIGHT);   
        for (int i = 0; i < 4; i++) {   
            drawCode(g, i);   
        }   
        drawNoise(g, 8);   
        g.setColor(Color.gray);   
        g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);   
        g.dispose();   
        response.setHeader("Pragma", "No-cache");   
        response.setHeader("Cache-Control", "no-cache");   
        response.setDateHeader("Expires", 0);   
        response.setContentType("image/png");   
        ServletOutputStream sos = null;   
        try {   
            sos = response.getOutputStream();   
            ImageIO.write(image, "png", sos);   
            sos.flush();   
            sos.close();   
        } catch (Exception e) {   
            e.printStackTrace();   
            return null;   
        }   
        return CONDENUMBER.toString();   
    }   
  
    private final void drawCode(Graphics graphics, int i) {   
        String number = CODE[random.nextInt(36)];   
        graphics.setFont(CODEFONT[random.nextInt(4)]);   
        graphics.setColor(FONTCOLOR[random.nextInt(5)]);   
        graphics.drawString(number, 10 + i * 20, 20);   
        CONDENUMBER.append(number);   
    }   
  
    private final void drawNoise(Graphics graphics, int lineNumber) {   
        graphics.setColor(LINECOLOR);   
        int pointX1, pointY1, pointX2, pointY2;   
        for (int i = 0; i < lineNumber; i++) {   
            pointX1 = 1 + (int) (Math.random() * WIDTH);   
            pointY1 = 1 + (int) (Math.random() * HEIGHT);   
            pointX2 = 1 + (int) (Math.random() * WIDTH);   
            pointY2 = 1 + (int) (Math.random() * HEIGHT);   
            graphics.drawLine(pointX1, pointY1, pointX2, pointY2);   
        }   
    }   
  
} 

?

  相关解决方案