BlogJava 联系 聚合 管理  

Blog Stats

随笔档案

文章档案

Infernu的Google site


Infernus-JXH

package com.tsinghuait.beans;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

public class DrawImageBean {
    
    
public DrawImageBean() {
        
    }

    
    
public String drawImage(HttpServletResponse response) {
        String code 
= "";
        BufferedImage image 
= new BufferedImage(6430, BufferedImage.TYPE_INT_RGB);//内存中的图片(宽, 高,颜色)
        Graphics g = image.getGraphics();//得到画笔
        
//Add your code here and complete your image
        g.setColor(Color.WHITE);//将颜色设置为白色
        g.fillRect(00400300);//填充整个区域
        
//        g.setColor(new Color(100, 100, 100));
//        g.drawString("Hello, world", 100, 100);
        
        
for(int i = 0; i< 100; i++{//画出验证码干扰的线,防止识别软件读取进行恶意登录
            g.setColor(new Color((int)(Math.random() * 256),
                    (
int)(Math.random() * 256), (int)(Math.random() * 256)));//画笔的颜色
            int x1 = 10 + (int)(Math.random() * 54);
            
int y1 = 30 - (int)(Math.random() * 30);
            
int x2 = x1 + 10;
            
int y2 = y1 + 10;
            g.drawLine(x1, y1, x2, y2);
//位置
        }

        
        
for(int i = 0; i < 4; i++{//画出验证码
            g.setColor(new Color((int)(Math.random() * 80),
                    (
int)(Math.random() * 80), (int)(Math.random() * 80)));//颜色较深,能看得清楚些
            g.setFont(new Font("Times New Roman", Font.BOLD, 16));//字体
            String s = String.valueOf((char)('A' + (int)(Math.random() * 58)));//生成验证码,范围是所有大写到所有小写,保存到字符串s
            code += s;
            g.drawString(s, 
10 + i * 1215);
        }

                
        g.dispose();
        
try {
            ImageIO.write(image, 
"JPEG", response.getOutputStream());//得到输出流 ,保存为jpg
        }

        
catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        
catch (IOException e) {
            e.printStackTrace();
        }

        
return code;
    }

}

posted on 2009-10-24 10:17 Infernus 阅读(240) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: