码迷,mamicode.com
首页 > 其他好文 > 详细

随机验证码

时间:2014-05-14 23:24:51      阅读:423      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

bubuko.com,布布扣
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResponseDemo4 extends HttpServlet {

    public static final int WIDTH = 120;
    public static final int HEIGHT = 35;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
                BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        // 设置背景色
        setBackGround(g);
        // 设置边框
        setBored(g);
        // 画干扰线
        drawRandomLine(g);
        // 写随机数字
        drawRandowNum((Graphics2D) g);
        // 图形写给浏览器
        response.setContentType("image/jpeg");
        // 控制浏览器不要拿缓存
        response.setDateHeader("expries", -1);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");

        ImageIO.write(image, "jpg", response.getOutputStream());

    }

    public void drawRandowNum(Graphics2D g) {
        g.setColor(Color.RED);
        g.setFont(new Font("黑体", Font.BOLD, 20));
        String base = "\u7684\u4e00\u662f\u4e86\u6211\u4e0d\u4eba\u5728\u4ed6\u6709\u8fd9\u4e2a\u4e0a\u4eec\u6765 \u5230\u65f6\u5927\u5730\u4e3a\u5b50\u4e2d\u4f60\u8bf4\u751f\u56fd\u5e74\u7740\u5c31\u90a3 \u548c\u8981\u5979\u51fa\u4e5f\u5f97\u91cc\u540e\u81ea\u4ee5\u4f1a\u5bb6\u53ef\u4e0b\u800c\u8fc7\u5929\u53bb\u80fd\u5bf9\u5c0f\u591a\u7136\u4e8e\u5fc3\u5b66\u4e48\u4e4b\u90fd\u597d\u770b\u8d77\u53d1\u5f53\u6ca1\u6210\u53ea\u5982\u4e8b\u628a\u8fd8\u7528\u7b2c\u6837\u9053 \u60f3\u4f5c\u79cd\u5f00\u7f8e\u603b\u4ece\u65e0\u60c5\u5df1\u9762\u6700\u5973\u4f46\u73b0 \u524d\u4e9b\u6240\u540c\u65e5\u624b\u53c8\u884c\u610f\u52a8\u65b9\u671f\u5b83\u5934\u7ecf \u957f\u513f\u56de\u4f4d\u5206\u7231\u8001\u56e0\u5f88\u7ed9\u540d\u6cd5\u95f4\u65af\u77e5";
        int x = 5;
        for (int i = 0; i < 4; i++) {
            int degree = new Random().nextInt() % 30; // 旋转-30°到30°
            String ch = base.charAt(new Random().nextInt(base.length())) + "";
            g.rotate(degree * Math.PI / 180, x, 20); // 设置旋转的弧度
            g.drawString(ch, x, 20);
            g.rotate(-degree * Math.PI / 180, x, 20);
            x += 30;

        }

    }

    public void setBored(Graphics g) {
        g.setColor(Color.GRAY);
        g.fillRect(1, 1, WIDTH - 2, HEIGHT - 2);

    }

    public void drawRandomLine(Graphics g) {
        g.setColor(Color.GREEN);
        for (int i = 0; i < 4; i++) {

            int x1 = new Random().nextInt(WIDTH);
            int y1 = new Random().nextInt(HEIGHT);

            int x2 = new Random().nextInt(WIDTH);
            int y2 = new Random().nextInt(HEIGHT);

            g.drawLine(x1, y1, x2, y2);
        }

    }

    public void setBackGround(Graphics g) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WIDTH, HEIGHT);

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);

    }

}
bubuko.com,布布扣

随机验证码,布布扣,bubuko.com

随机验证码

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/stonewu/p/3728796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!