码迷,mamicode.com
首页 > 编程语言 > 详细

Java生成图片验证码2

时间:2015-07-24 14:14:15      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

Photo类

package com.baowei.utils;

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 Photo {

    private static final int width = 120;
    private static final int height = 25;
    private HttpServletResponse response;

    private String randomData = "";

    public Photo(HttpServletResponse response) {
        this.response = response;
    }

    public void createImage() throws IOException {

        // 1、在内存里面创建一个图片
        BufferedImage bimage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        // 2.1得到图片
        Graphics g = bimage.getGraphics();
        // 2.2设置图片的背景颜色
        setBackground(g);
        // 3、设置图片边框
        setBorder(g);
        // 4、设置图片干扰线
        setLine(g);
        // 5、向图片写数据
        setRandomData(g);
        // 6、将图片写给客户机
        response.setContentType("image/jpeg");
        ImageIO.write(bimage, "jpg", response.getOutputStream());
    }

    public void setBackground(Graphics g) {
        Color color = g.getColor();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);
        g.setColor(color);

    }

    public void setBorder(Graphics g) {

        Color color = g.getColor();
        g.setColor(Color.BLUE);
        g.drawRect(1, 1, width - 2, height - 2);
        g.setColor(color);

    }

    public void setLine(Graphics g) {

        for (int i = 0; i < 4; i++) {
            // 此处获得到的x,y应该与上面的图片的宽高关联起来
            int x1 = new Random().nextInt(width);
            int x2 = new Random().nextInt(width);
            int y1 = new Random().nextInt(height);
            int y2 = new Random().nextInt(height);
            Color color = g.getColor();
            g.setColor(Color.GREEN);
            g.drawLine(x1, y1, x2, y2);
            g.setColor(color);
        }

    }

    public void setRandomData(Graphics g) {

        // 获取随机的数据
        // String data = "";
        for (int i = 0; i < 4; i++) {
            randomData += new Random().nextInt(10) + " ";
        }

        // 设置数据的字体和颜色
        Color color = g.getColor();
        g.setColor(Color.RED);

        g.setFont(new Font("宋体", Font.BOLD, 20));

        g.drawString(randomData, 20, 20);

        g.setColor(color);

    }
    
    public String getRandomData() {

        String data[] = randomData.split(" ");
        String returnData = "";
        for (int i = 0; i < data.length; i++) {
            returnData += data[i];
        }
        
        return returnData;
    }


}

Demo01测试用的servlet

package com.baowei.servlet;

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.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.baowei.utils.Photo;

public class Demo01 extends HttpServlet {

    

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

        doPost(request, response);

    }

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

        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");

        Photo pt = new Photo(response);
        pt.createImage();
        System.out.println(pt.getRandomData());
        String data = pt.getRandomData();

        // 存入会话session
        //HttpSession session = request.getSession(true);
        //session.setAttribute("data", data);
        getServletContext().setAttribute("data", data);

    }

}

 

Java生成图片验证码2

标签:

原文地址:http://www.cnblogs.com/zhangbaowei/p/4673320.html

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